Search in sources :

Example 36 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.

the class SlingIntegrationTestClient method uploadToFileNode.

/** Upload to an file node structure, see SLING-168 */
public void uploadToFileNode(String url, File localFile, String fieldName, String typeHint) throws IOException {
    final Part[] parts = new Part[typeHint == null ? 1 : 2];
    parts[0] = new FilePart(fieldName, localFile);
    if (typeHint != null) {
        parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
    }
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
    final int status = httpClient.executeMethod(post);
    final int expected = 200;
    if (status != expected) {
        throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post, 0));
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Example 37 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.

the class FelixPostDeployMethod method deploy.

@Override
public void deploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
    // append pseudo path after root URL to not get redirected for nothing
    final PostMethod filePost = new PostMethod(targetURL + "/install");
    try {
        // set referrer
        filePost.setRequestHeader("referer", "about:blank");
        List<Part> partList = new ArrayList<Part>();
        partList.add(new StringPart("action", "install"));
        partList.add(new StringPart("_noredir_", "_noredir_"));
        partList.add(new FilePart("bundlefile", new FilePartSource(file.getName(), file)));
        partList.add(new StringPart("bundlestartlevel", context.getBundleStartLevel()));
        if (context.isBundleStart()) {
            partList.add(new StringPart("bundlestart", "start"));
        }
        if (context.isRefreshPackages()) {
            partList.add(new StringPart("refreshPackages", "true"));
        }
        Part[] parts = partList.toArray(new Part[partList.size()]);
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        int status = context.getHttpClient().executeMethod(filePost);
        if (status == HttpStatus.SC_OK) {
            context.getLog().info("Bundle installed");
        } else {
            String msg = "Installation failed, cause: " + HttpStatus.getStatusText(status);
            if (context.isFailOnError()) {
                throw new MojoExecutionException(msg);
            } else {
                context.getLog().error(msg);
            }
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Installation on " + targetURL + " failed, cause: " + ex.getMessage(), ex);
    } finally {
        filePost.releaseConnection();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FilePartSource(org.apache.commons.httpclient.methods.multipart.FilePartSource) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part)

Example 38 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.

the class SlingPostDeployMethod method undeploy.

@Override
public void undeploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
    final PostMethod post = new PostMethod(getURLWithFilename(targetURL, file.getName()));
    try {
        // Add SlingPostServlet operation flag for deleting the content
        Part[] parts = new Part[1];
        parts[0] = new StringPart(":operation", "delete");
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        // Request JSON response from Sling instead of standard HTML
        post.setRequestHeader("Accept", JSON_MIME_TYPE);
        int status = context.getHttpClient().executeMethod(post);
        if (status == HttpStatus.SC_OK) {
            context.getLog().info("Bundle uninstalled");
        } else {
            context.getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status));
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(), ex);
    } finally {
        post.releaseConnection();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 39 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.

the class DeleteNodeCommand method execute.

@Override
public Result<Void> execute() {
    PostMethod post = new PostMethod(getPath());
    try {
        Part[] parts = { new StringPart(":operation", "delete") };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
        httpClient.getParams().setAuthenticationPreemptive(true);
        int responseStatus = httpClient.executeMethod(post);
        return resultForResponseStatus(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 40 with MultipartRequestEntity

use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project sling by apache.

the class UpdateContentCommand method execute.

@Override
public Result<Void> execute() {
    PostMethod post = new PostMethod(getPath());
    try {
        List<Part> parts = new ArrayList<>();
        for (Map.Entry<String, Object> property : properties.entrySet()) {
            if (ProtectedNodes.exists(property.getKey())) {
                continue;
            }
            Object propValue = property.getValue();
            if (propValue instanceof String) {
                parts.add(new StringPart(property.getKey(), (String) propValue));
            } else if (property != null) {
                // TODO handle multi-valued properties
                System.err.println("Unable to handle property " + property.getKey() + " of type " + property.getValue().getClass());
            }
        }
        File f = new File(fileInfo.getLocation());
        if (f.isFile()) {
            parts.add(new FilePart(fileInfo.getName(), f));
        }
        post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
        httpClient.getParams().setAuthenticationPreemptive(true);
        int responseStatus = httpClient.executeMethod(post);
        return resultForResponseStatus(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) Map(java.util.Map) File(java.io.File)

Aggregations

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)63 Part (org.apache.commons.httpclient.methods.multipart.Part)54 PostMethod (org.apache.commons.httpclient.methods.PostMethod)53 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)52 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)44 IOException (java.io.IOException)24 HttpClient (org.apache.commons.httpclient.HttpClient)20 File (java.io.File)16 ArrayList (java.util.ArrayList)16 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)9 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 Element (org.jdom.Element)6 HttpException (org.apache.commons.httpclient.HttpException)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Map (java.util.Map)4 Header (org.apache.commons.httpclient.Header)4 NameValuePair (org.apache.commons.httpclient.NameValuePair)4