Search in sources :

Example 11 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project incubator-heron by apache.

the class HttpUploader method uploadPackageAndGetURI.

private URI uploadPackageAndGetURI(final CloseableHttpClient httpclient) throws IOException, URISyntaxException {
    File file = new File(this.topologyPackageLocation);
    String uploaderUri = HttpUploaderContext.getHeronUploaderHttpUri(this.config);
    post = new HttpPost(uploaderUri);
    FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart(FILE, fileBody);
    HttpEntity entity = builder.build();
    post.setEntity(entity);
    HttpResponse response = execute(httpclient);
    String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8.name());
    LOG.fine("Topology package download URI: " + responseString);
    return new URI(responseString);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) URI(java.net.URI)

Example 12 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project ninja by ninjaframework.

the class NinjaTestBrowser method uploadFile.

public String uploadFile(String url, String paramName, File fileToUpload) {
    String response = null;
    try {
        httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpPost post = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        // For File parameters
        entity.addPart(paramName, new FileBody((File) fileToUpload));
        post.setEntity(entity);
        // Here we go!
        response = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
        post.releaseConnection();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) File(java.io.File) IOException(java.io.IOException)

Example 13 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsServiceV1 method submitBuild.

@Override
protected JsonObject submitBuild(CloseableHttpClient httpclient, File archive, String buildName, String originator) throws /*unused*/
IOException {
    String newBuildURL = getBuildsUrl(httpclient) + "?build_name=" + URLEncoder.encode(buildName, StandardCharsets.UTF_8.name());
    HttpPost httppost = new HttpPost(newBuildURL);
    httppost.addHeader("Authorization", getAuthorization());
    FileBody archiveBody = new FileBody(archive, ContentType.create("application/zip"));
    HttpEntity reqEntity = MultipartEntityBuilder.create().addPart(archive.getName(), archiveBody).build();
    httppost.setEntity(reqEntity);
    JsonObject jso = StreamsRestUtils.getGsonResponse(httpclient, httppost);
    JsonObject build = object(jso, "build");
    return build;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) JsonObject(com.google.gson.JsonObject)

Example 14 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.

the class MultiPartTestCase method testMultiPartIndividualFileToLarge.

@Test
public void testMultiPartIndividualFileToLarge() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        String uri = DefaultServer.getDefaultServerURL() + "/servletContext/3";
        HttpPost post = new HttpPost(uri);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("EXCEPTION: class java.lang.IllegalStateException", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 15 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project undertow by undertow-io.

the class MultipartFormDataParserTestCase method testFileUpload.

@Test
public void testFileUpload() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createHandler()));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        // post.setHeader(Headers.CONTENT_TYPE, MultiPartHandler.MULTIPART_FORM_DATA);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
        entity.addPart("file", new FileBody(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile())));
        post.setEntity(entity);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) BlockingHandler(io.undertow.server.handlers.BlockingHandler) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

FileBody (org.apache.http.entity.mime.content.FileBody)61 HttpPost (org.apache.http.client.methods.HttpPost)46 File (java.io.File)41 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)37 HttpResponse (org.apache.http.HttpResponse)31 StringBody (org.apache.http.entity.mime.content.StringBody)30 HttpEntity (org.apache.http.HttpEntity)18 IOException (java.io.IOException)17 Test (org.junit.Test)17 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)15 TestHttpClient (io.undertow.testutils.TestHttpClient)11 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)7 HttpClient (org.apache.http.client.HttpClient)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 JsonObject (com.google.gson.JsonObject)5 JSONObject (org.json.JSONObject)5 BlockingHandler (io.undertow.server.handlers.BlockingHandler)4 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4