Search in sources :

Example 11 with Part

use of org.apache.commons.httpclient.methods.multipart.Part in project zm-mailbox by Zimbra.

the class TestFileUpload method testMissingCsrfAdminUpload.

@Test
public void testMissingCsrfAdminUpload() throws Exception {
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
    com.zimbra.soap.admin.message.AuthRequest req = new com.zimbra.soap.admin.message.AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
    req.setCsrfSupported(true);
    Element response = transport.invoke(JaxbUtil.jaxbToElement(req, SoapProtocol.SoapJS.getFactory()));
    com.zimbra.soap.admin.message.AuthResponse authResp = JaxbUtil.elementToJaxb(response);
    String authToken = authResp.getAuthToken();
    int port = 7071;
    try {
        port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    } catch (ServiceException e) {
        ZimbraLog.test.error("Unable to get admin SOAP port", e);
    }
    String Url = "https://localhost:" + port + ADMIN_UPLOAD_URL;
    PostMethod post = new PostMethod(Url);
    FilePart part = new FilePart(FILE_NAME, new ByteArrayPartSource(FILE_NAME, "some file content".getBytes()));
    String contentType = "application/x-msdownload";
    part.setContentType(contentType);
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    HttpState state = new HttpState();
    state.addCookie(new org.apache.commons.httpclient.Cookie("localhost", ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    client.setState(state);
    post.setRequestEntity(new MultipartRequestEntity(new Part[] { part }, post.getParams()));
    int statusCode = HttpClientUtil.executeMethod(client, post);
    Assert.assertEquals("This request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK, statusCode);
    String resp = post.getResponseBodyAsString();
    Assert.assertNotNull("Response should not be empty", resp);
    Assert.assertTrue("Incorrect HTML response", resp.contains(RESP_STR));
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HeaderElement(org.apache.commons.httpclient.HeaderElement) Element(com.zimbra.common.soap.Element) HttpState(org.apache.commons.httpclient.HttpState) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) ServiceException(com.zimbra.common.service.ServiceException) 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) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 12 with Part

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

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

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

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

Aggregations

Part (org.apache.commons.httpclient.methods.multipart.Part)31 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)29 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)28 PostMethod (org.apache.commons.httpclient.methods.PostMethod)25 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)24 HttpClient (org.apache.commons.httpclient.HttpClient)13 File (java.io.File)10 IOException (java.io.IOException)10 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