/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you under the Apache License, version
 *  2.0 (the "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 */
package io.fabric8.maven.impl;

import io.fabric8.api.RuntimeProperties;
import io.fabric8.deployer.ProjectDeployer;
import io.fabric8.deployer.dto.DeployResults;
import io.fabric8.deployer.dto.ProjectRequirements;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MavenUploadProxyServlet extends MavenDownloadProxyServlet {

    public MavenUploadProxyServlet(RuntimeProperties runtimeProperties, String localRepository, List<String> remoteRepositories, boolean appendSystemRepos, String updatePolicy, String checksumPolicy, String proxyProtocol, String proxyHost, int proxyPort, String proxyUsername, String proxyPassword, String proxyNonProxyHosts, ProjectDeployer projectDeployer) {
        super(runtimeProperties, localRepository, remoteRepositories, appendSystemRepos, updatePolicy, checksumPolicy, proxyProtocol, proxyHost, proxyPort, proxyUsername, proxyPassword, proxyNonProxyHosts, projectDeployer);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String path = req.getPathInfo();
            //Make sure path is valid
            if (path == null) {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                return;
            }
            if (path.startsWith("/")) {
                path = path.substring(1);
            }

            // handle move
            String location = req.getHeader(LOCATION_HEADER);
            if (location != null) {
                UploadContext result = move(location, path);
                addHeaders(resp, result.headers());
                resp.setStatus(HttpServletResponse.SC_ACCEPTED);
                return;
            }

            UploadContext result = doUpload(req.getInputStream(), path);
            if (result.status()) {
                addHeaders(resp, result.headers());

                String profile = req.getParameter("profile");
                String version = req.getParameter("version");
                if (profile != null && version != null) {
                    ProjectRequirements requirements = toProjectRequirements(result);
                    requirements.setProfileId(profile);
                    requirements.setVersion(version);

                    DeployResults deployResults = addToProfile(requirements);
                    LOGGER.info(String.format("Deployed artifact %s to profile: %s", result.toArtifact(), deployResults));
                }

                resp.setStatus(HttpServletResponse.SC_ACCEPTED);
            } else {
                resp.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
            }
        } catch (InvalidMavenArtifactRequest ex) {
            // must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.setContentLength(0);
            resp.flushBuffer();
        } catch (Exception ex) {
            // must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            resp.setContentLength(0);
            resp.flushBuffer();
        }

    }

    private static void addHeaders(HttpServletResponse resp, Map<String, String> headers) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            resp.addHeader(entry.getKey(), entry.getValue());
        }
    }
}

@@ -2175,32 +2175,66 @@
 %0A            %7D%0A%0A
+            UploadContext result;%0A
             // h
@@ -2354,38 +2354,24 @@
             
-UploadContext 
 result = mov
@@ -2405,136 +2405,16 @@
     
-    addHeaders(resp, result.headers());%0A                resp.setStatus(HttpServletResponse.SC_ACCEPTED);%0A                return;
+%7D else %7B
 %0A   
@@ -2426,37 +2426,12 @@
     
-%7D%0A%0A
     
-        UploadContext 
 resu
@@ -2481,24 +2481,39 @@
             
+%7D%0A%0A            
 if (result.s
@@ -2520,67 +2520,14 @@
 tatu
-s()) %7B%0A                addHeaders(resp, result.header
 s())
-;%0A
+ %7B
 %0A   
@@ -2543,589 +2543,87 @@
     
-String profile = req.getParameter(%22profile%22);%0A                String version = req.getParameter(%22version%22);%0A                if (profile != null && version != null) %7B%0A                    ProjectRequirements requirements = toProjectRequirements(result);%0A                    requirements.setProfileId(profile);%0A                    requirements.setVersion(version);%0A%0A                    DeployResults deployResults = addToProfile(requirements);%0A                    LOGGER.info(String.format(%22Deployed artifact %25s to profile: %25s%22, result.toArtifact(), deployResults));%0A                %7D
+handleDeploy(req, result);%0A%0A                addHeaders(resp, result.headers());
 %0A%0A  
@@ -3429,24 +3429,652 @@
   %7D%0A%0A    %7D%0A%0A
+    private void handleDeploy(HttpServletRequest req, UploadContext result) throws Exception %7B%0A        String profile = req.getParameter(%22profile%22);%0A        String version = req.getParameter(%22version%22);%0A        if (profile != null && version != null) %7B%0A            ProjectRequirements requirements = toProjectRequirements(result);%0A            requirements.setProfileId(profile);%0A            requirements.setVersion(version);%0A%0A            DeployResults deployResults = addToProfile(requirements);%0A            LOGGER.info(String.format(%22Deployed artifact %25s to profile: %25s%22, result.toArtifact(), deployResults));%0A        %7D%0A    %7D%0A%0A
     private 

Fix move handling.