Search in sources :

Example 11 with FilePart

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

the class AbstractBundleDeployMojo method post.

private void post(String targetURL, File file) throws MojoExecutionException {
    PostMethod filePost = new PostMethod(targetURL);
    try {
        Part[] parts = { new FilePart(file.getName(), new FilePartSource(file.getName(), file)), new StringPart("_noredir_", "_noredir_") };
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        int status = client.executeMethod(filePost);
        if (status == HttpStatus.SC_OK) {
            getLog().info("Bundle deployed");
        } else {
            String msg = "Deployment failed, cause: " + HttpStatus.getStatusText(status);
            if (failOnError) {
                throw new MojoExecutionException(msg);
            } else {
                getLog().error(msg);
            }
        }
    } catch (Exception ex) {
        throw new MojoExecutionException("Deployment 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) 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) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 12 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart 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 13 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart in project h2o-2 by h2oai.

the class WebAPI method importModel.

/**
   * Imports a model from a JSON file.
   */
public static void importModel() throws Exception {
    // Upload file to H2O
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(URL + "/Upload.json?key=" + JSON_FILE.getName());
    Part[] parts = { new FilePart(JSON_FILE.getName(), JSON_FILE) };
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
    if (200 != client.executeMethod(post))
        throw new RuntimeException("Request failed: " + post.getStatusLine());
    post.releaseConnection();
    // Parse the key into a model
    GetMethod get = new GetMethod(//
    URL + "/2/ImportModel.json?" + //
    "destination_key=MyImportedNeuralNet&" + //
    "type=NeuralNetModel&" + "json=" + JSON_FILE.getName());
    if (200 != client.executeMethod(get))
        throw new RuntimeException("Request failed: " + get.getStatusLine());
    get.releaseConnection();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Example 14 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart in project pinot by linkedin.

the class ServerSegmentCompletionProtocolHandler method segmentCommit.

public SegmentCompletionProtocol.Response segmentCommit(long offset, final String segmentName, final File segmentTarFile) throws FileNotFoundException {
    SegmentCompletionProtocol.Request.Params params = new SegmentCompletionProtocol.Request.Params();
    params.withInstanceId(_instance).withOffset(offset).withSegmentName(segmentName);
    SegmentCompletionProtocol.SegmentCommitRequest request = new SegmentCompletionProtocol.SegmentCommitRequest(params);
    final InputStream inputStream = new FileInputStream(segmentTarFile);
    Part[] parts = { new FilePart(segmentName, new PartSource() {

        @Override
        public long getLength() {
            return segmentTarFile.length();
        }

        @Override
        public String getFileName() {
            return "fileName";
        }

        @Override
        public InputStream createInputStream() throws IOException {
            return new BufferedInputStream(inputStream);
        }
    }) };
    return doHttp(request, parts);
}
Also used : PartSource(org.apache.commons.httpclient.methods.multipart.PartSource) SegmentCompletionProtocol(com.linkedin.pinot.common.protocols.SegmentCompletionProtocol) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) FileInputStream(java.io.FileInputStream) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) BufferedInputStream(java.io.BufferedInputStream) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part)

Example 15 with FilePart

use of org.apache.commons.httpclient.methods.multipart.FilePart 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)

Aggregations

FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)27 Part (org.apache.commons.httpclient.methods.multipart.Part)25 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)23 PostMethod (org.apache.commons.httpclient.methods.PostMethod)20 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)18 HttpClient (org.apache.commons.httpclient.HttpClient)12 File (java.io.File)10 IOException (java.io.IOException)7 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 HttpState (org.apache.commons.httpclient.HttpState)4 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)4 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)4 ServiceException (com.zimbra.common.service.ServiceException)3 Element (com.zimbra.common.soap.Element)3 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)3 HeaderElement (org.apache.commons.httpclient.HeaderElement)3 HttpException (org.apache.commons.httpclient.HttpException)3 PartSource (org.apache.commons.httpclient.methods.multipart.PartSource)3