Search in sources :

Example 46 with FileBody

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

the class MultipartFormDataParserTestCase method testFileUploadWithSmallFileSizeThreshold.

@Test
public void testFileUploadWithSmallFileSizeThreshold() throws Exception {
    DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10)));
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
        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());
        String resp = HttpClientUtils.readResponse(result);
        Map<String, String> parsedResponse = parse(resp);
        Assert.assertEquals("false", parsedResponse.get("in_memory"));
        Assert.assertEquals("uploadfile.txt", parsedResponse.get("file_name"));
        Assert.assertEquals(DigestUtils.md5Hex(new FileInputStream(new File(MultipartFormDataParserTestCase.class.getResource("uploadfile.txt").getFile()))), parsedResponse.get("hash"));
    } 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) FileInputStream(java.io.FileInputStream) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 47 with FileBody

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

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 48 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project tutorials by eugenp.

the class HttpClientMultipartLiveTest method givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions.

// tests
@Test
public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException {
    final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + TEXTFILENAME);
    final File file = new File(url.getPath());
    final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
    final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
    final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
    // 
    final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("upfile", fileBody);
    builder.addPart("text1", stringBody1);
    builder.addPart("text2", stringBody2);
    final HttpEntity entity = builder.build();
    // 
    post.setEntity(entity);
    response = client.execute(post);
    final int statusCode = response.getStatusLine().getStatusCode();
    final String responseString = getContent();
    final String contentTypeInHeader = getContentTypeHeader();
    assertThat(statusCode, equalTo(HttpStatus.SC_OK));
    // assertTrue(responseString.contains("Content-Type: multipart/form-data;"));
    assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
    System.out.println(responseString);
    System.out.println("POST Content Type: " + contentTypeInHeader);
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) HttpEntity(org.apache.http.HttpEntity) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 49 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project questdb by bluestreak01.

the class HttpTestUtils method upload.

public static int upload(String resource, String url, String schema, StringBuilder response) throws IOException {
    HttpPost post = new HttpPost(url);
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        MultipartEntityBuilder b = MultipartEntityBuilder.create();
        if (schema != null) {
            b.addPart("schema", new StringBody(schema, ContentType.TEXT_PLAIN));
        }
        b.addPart("data", new FileBody(resourceFile(resource)));
        post.setEntity(b.build());
        HttpResponse r = client.execute(post);
        if (response != null) {
            InputStream is = r.getEntity().getContent();
            int n;
            while ((n = is.read()) > 0) {
                response.append((char) n);
            }
            is.close();
        }
        return r.getStatusLine().getStatusCode();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) FileBody(org.apache.http.entity.mime.content.FileBody) StringBody(org.apache.http.entity.mime.content.StringBody) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse)

Example 50 with FileBody

use of org.apache.http.entity.mime.content.FileBody in project selenium_java by sergueik.

the class RestClient method request.

private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments) throws RestException, IOException {
    if (attachments != null) {
        req.setHeader("X-Atlassian-Token", "nocheck");
        MultipartEntity ent = new MultipartEntity();
        for (Issue.NewAttachment attachment : attachments) {
            String filename = attachment.getFilename();
            Object content = attachment.getContent();
            if (content instanceof byte[]) {
                ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
            } else if (content instanceof InputStream) {
                ent.addPart("file", new InputStreamBody((InputStream) content, filename));
            } else if (content instanceof File) {
                ent.addPart("file", new FileBody((File) content, filename));
            } else if (content == null) {
                throw new IllegalArgumentException("Missing content for the file " + filename);
            } else {
                throw new IllegalArgumentException("Expected file type byte[], java.io.InputStream or java.io.File but provided " + content.getClass().getName() + " for the file " + filename);
            }
        }
        req.setEntity(ent);
    }
    return request(req);
}
Also used : FileBody(org.apache.http.entity.mime.content.FileBody) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) InputStream(java.io.InputStream) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) JSONObject(net.sf.json.JSONObject) File(java.io.File)

Aggregations

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