Search in sources :

Example 11 with MultipartRequestEntity

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

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

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

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

the class ServerSegmentCompletionProtocolHandler method doHttp.

private SegmentCompletionProtocol.Response doHttp(SegmentCompletionProtocol.Request request, Part[] parts) {
    SegmentCompletionProtocol.Response response = SegmentCompletionProtocol.RESP_NOT_SENT;
    HttpClient httpClient = new HttpClient();
    ControllerLeaderLocator leaderLocator = ControllerLeaderLocator.getInstance();
    final String leaderAddress = leaderLocator.getControllerLeader();
    if (leaderAddress == null) {
        LOGGER.error("No leader found {}", this.toString());
        return SegmentCompletionProtocol.RESP_NOT_LEADER;
    }
    final String url = request.getUrl(leaderAddress);
    HttpMethodBase method;
    if (parts != null) {
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
        method = postMethod;
    } else {
        method = new GetMethod(url);
    }
    LOGGER.info("Sending request {} for {}", url, this.toString());
    try {
        int responseCode = httpClient.executeMethod(method);
        if (responseCode >= 300) {
            LOGGER.error("Bad controller response code {} for {}", responseCode, this.toString());
            return response;
        } else {
            response = new SegmentCompletionProtocol.Response(method.getResponseBodyAsString());
            LOGGER.info("Controller response {} for {}", response.toJsonString(), this.toString());
            if (response.getStatus().equals(SegmentCompletionProtocol.ControllerResponseStatus.NOT_LEADER)) {
                leaderLocator.refreshControllerLeader();
            }
            return response;
        }
    } catch (IOException e) {
        LOGGER.error("IOException {}", this.toString(), e);
        leaderLocator.refreshControllerLeader();
        return response;
    }
}
Also used : SegmentCompletionProtocol(com.linkedin.pinot.common.protocols.SegmentCompletionProtocol) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)

Example 15 with MultipartRequestEntity

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

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)31 Part (org.apache.commons.httpclient.methods.multipart.Part)29 PostMethod (org.apache.commons.httpclient.methods.PostMethod)27 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)26 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)24 HttpClient (org.apache.commons.httpclient.HttpClient)15 IOException (java.io.IOException)11 File (java.io.File)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)5 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)5 HttpState (org.apache.commons.httpclient.HttpState)4 FilePartSource (org.apache.commons.httpclient.methods.multipart.FilePartSource)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ServiceException (com.zimbra.common.service.ServiceException)3 Element (com.zimbra.common.soap.Element)3 SoapHttpTransport (com.zimbra.common.soap.SoapHttpTransport)3 Map (java.util.Map)3 HeaderElement (org.apache.commons.httpclient.HeaderElement)3