Search in sources :

Example 6 with StringPart

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

the class SlingPostDeployMethod method deploy.

@Override
public void deploy(String targetURL, File file, String bundleSymbolicName, DeployContext context) throws MojoExecutionException {
    /* truncate off trailing slash as this has special behaviorisms in
         * the SlingPostServlet around created node name conventions */
    if (targetURL.endsWith("/")) {
        targetURL = targetURL.substring(0, targetURL.length() - 1);
    }
    // append pseudo path after root URL to not get redirected for nothing
    final PostMethod filePost = new PostMethod(targetURL);
    try {
        Part[] parts = new Part[2];
        // Add content type to force the configured mimeType value
        parts[0] = new FilePart("*", new FilePartSource(file.getName(), file), context.getMimeType(), null);
        // Add TypeHint to have jar be uploaded as file (not as resource)
        parts[1] = new StringPart("*@TypeHint", "nt:file");
        /* Request JSON response from Sling instead of standard HTML, to
             * reduce the payload size (if the PostServlet supports it). */
        filePost.setRequestHeader("Accept", JSON_MIME_TYPE);
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        int status = context.getHttpClient().executeMethod(filePost);
        // SlingPostServlet may return 200 or 201 on creation, accept both
        if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) {
            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 : FilePartSource(org.apache.commons.httpclient.methods.multipart.FilePartSource) 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) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 7 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.

the class HttpBridgeMultipartRouteTest method testHttpClient.

@Test
public void testHttpClient() throws Exception {
    File jpg = new File("src/test/resources/java.jpg");
    String body = "TEST";
    Part[] parts = new Part[] { new StringPart("body", body), new FilePart(jpg.getName(), jpg) };
    PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello");
    MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
    method.setRequestEntity(requestEntity);
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    String responseBody = method.getResponseBodyAsString();
    assertEquals(body, responseBody);
    String numAttachments = method.getResponseHeader("numAttachments").getValue();
    assertEquals(numAttachments, "2");
}
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) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Example 8 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.

the class MultiPartFormTest method createMultipartRequestEntity.

private RequestEntity createMultipartRequestEntity() throws Exception {
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    return new MultipartRequestEntity(parts, new HttpMethodParams());
}
Also used : 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) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Example 9 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.

the class MultiPartFormWithCustomFilterTest method testSendMultiPartFormOverrideEnableMultpartFilterFalse.

@Test
public void testSendMultiPartFormOverrideEnableMultpartFilterFalse() throws Exception {
    HttpClient httpclient = new HttpClient();
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test2");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);
    int status = httpclient.executeMethod(httppost);
    assertEquals("Get a wrong response status", 200, status);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
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) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Example 10 with StringPart

use of org.apache.commons.httpclient.methods.multipart.StringPart in project camel by apache.

the class MultiPartFormWithCustomFilterTest method testSendMultiPartForm.

@Test
public void testSendMultiPartForm() throws Exception {
    HttpClient httpclient = new HttpClient();
    File file = new File("src/main/resources/META-INF/NOTICE.txt");
    PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test");
    Part[] parts = { new StringPart("comment", "A binary file of some kind"), new FilePart(file.getName(), file) };
    MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams());
    httppost.setRequestEntity(reqEntity);
    int status = httpclient.executeMethod(httppost);
    assertEquals("Get a wrong response status", 200, status);
    String result = httppost.getResponseBodyAsString();
    assertEquals("Get a wrong result", "A binary file of some kind", result);
    assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter"));
}
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) HttpClient(org.apache.commons.httpclient.HttpClient) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Test(org.junit.Test)

Aggregations

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)22 Part (org.apache.commons.httpclient.methods.multipart.Part)22 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)19 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)19 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 HttpClient (org.apache.commons.httpclient.HttpClient)7 File (java.io.File)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 Test (org.junit.Test)4 Map (java.util.Map)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)3 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)3 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 RepositoryException (org.apache.sling.ide.transport.RepositoryException)2 PostParameter (weibo4j.model.PostParameter)2 WeiboException (weibo4j.model.WeiboException)2