Example usage for org.apache.http.entity ContentType DEFAULT_BINARY

List of usage examples for org.apache.http.entity ContentType DEFAULT_BINARY

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType DEFAULT_BINARY.

Prototype

ContentType DEFAULT_BINARY

To view the source code for org.apache.http.entity ContentType DEFAULT_BINARY.

Click Source Link

Usage

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void testFormDataParamWithMultipleFiles() throws IOException, URISyntaxException {
    HttpURLConnection connection = request("/test/v1/multipleFiles", HttpMethod.POST);
    File file1 = new File(
            Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt").toURI());
    File file2 = new File(
            Thread.currentThread().getContextClassLoader().getResource("testPngFile.png").toURI());
    HttpEntity reqEntity = MultipartEntityBuilder.create()
            .addBinaryBody("files", file1, ContentType.DEFAULT_BINARY, file1.getName())
            .addBinaryBody("files", file2, ContentType.DEFAULT_BINARY, file2.getName()).build();

    connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue());
    try (OutputStream out = connection.getOutputStream()) {
        reqEntity.writeTo(out);/*ww w. ja  v a  2s.  c om*/
    }

    InputStream inputStream = connection.getInputStream();
    String response = StreamUtil.asString(inputStream);
    IOUtils.closeQuietly(inputStream);
    connection.disconnect();
    assertEquals(response, "2");
}

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void testFormDataParamWithFileStream() throws IOException, URISyntaxException {
    HttpURLConnection connection = request("/test/v1/streamFile", HttpMethod.POST);
    File file = new File(Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt").toURI());
    HttpEntity reqEntity = MultipartEntityBuilder.create()
            .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()).build();

    connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue());
    try (OutputStream out = connection.getOutputStream()) {
        reqEntity.writeTo(out);//from   www. j a  va 2s .  co  m
    }

    InputStream inputStream = connection.getInputStream();
    String response = StreamUtil.asString(inputStream);
    IOUtils.closeQuietly(inputStream);
    connection.disconnect();
    StringBuilder stringBuilder = new StringBuilder();
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
        while (bufferedReader.ready()) {
            stringBuilder.append(bufferedReader.readLine());
        }
    }
    assertEquals(response, stringBuilder.toString() + "-" + file.getName());
}

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void getAllFormItemsMultipart() throws IOException, URISyntaxException {
    HttpURLConnection connection = request("/test/v1/getAllFormItemsMultipart", HttpMethod.POST);
    StringBody companyText = new StringBody("{\"type\": \"Open Source\"}", ContentType.APPLICATION_JSON);
    StringBody personList = new StringBody(
            "[{\"name\":\"Richard Stallman\",\"age\":63}, {\"name\":\"Linus Torvalds\",\"age\":46}]",
            ContentType.APPLICATION_JSON);
    HttpEntity reqEntity = MultipartEntityBuilder.create().addTextBody("id", "1")
            .addPart("company", companyText).addPart("people", personList)
            .addBinaryBody("file",
                    new File(Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt")
                            .toURI()),//from   w  ww.  j ava2  s.  co  m
                    ContentType.DEFAULT_BINARY, "testTxtFile.txt")
            .addBinaryBody(
                    "file", new File(Thread.currentThread().getContextClassLoader()
                            .getResource("testPngFile.png").toURI()),
                    ContentType.DEFAULT_BINARY, "testPngFile.png")
            .build();

    connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue());
    try (OutputStream out = connection.getOutputStream()) {
        reqEntity.writeTo(out);
    }

    InputStream inputStream = connection.getInputStream();
    String response = StreamUtil.asString(inputStream);
    IOUtils.closeQuietly(inputStream);
    connection.disconnect();
    assertEquals("FileCount-2 SecondFileName-testPngFile.png FirstPerson-Richard Stallman", response);

    connection = request("/test/v1/getAllFormItemsXFormUrlEncoded", HttpMethod.POST);
    String rawData = "names=WSO2&names=IBM&type=Software";
    ByteBuffer encodedData = Charset.defaultCharset().encode(rawData);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    connection.setRequestProperty("Content-Length", String.valueOf(encodedData.array().length));
    try (OutputStream os = connection.getOutputStream()) {
        os.write(Arrays.copyOf(encodedData.array(), encodedData.limit()));
    }

    inputStream = connection.getInputStream();
    response = StreamUtil.asString(inputStream);
    IOUtils.closeQuietly(inputStream);
    connection.disconnect();
    assertEquals("Type = Software No of names = 2 First name = IBM", response);
}