Example usage for org.apache.http.entity.mime.content InputStreamBody InputStreamBody

List of usage examples for org.apache.http.entity.mime.content InputStreamBody InputStreamBody

Introduction

In this page you can find the example usage for org.apache.http.entity.mime.content InputStreamBody InputStreamBody.

Prototype

public InputStreamBody(final InputStream in, final String mimeType, final String filename) 

Source Link

Usage

From source file:com.liferay.mobile.sample.util.UploadFileUtil.java

public static void upload(Context context, long groupId) throws Exception {
    Session session = SettingsUtil.getSession();
    session.setCallback(_getCallback());

    DLAppService service = new DLAppService(session);

    String filename = "logo.png";
    String mimeType = "image/png";

    InputStream is = context.getAssets().open(filename);

    InputStreamBody file = new InputStreamBody(is, mimeType, filename);
    service.addFileEntry(groupId, 0, "", mimeType, filename, "", "", file, null);
}

From source file:com.tenmiles.helpstack.model.HSUploadAttachment.java

public InputStreamBody generateStreamToUpload() throws FileNotFoundException {
    InputStream stream = generateInputStreamToUpload();

    InputStreamBody body = new InputStreamBody(stream, attachment.getMime_type(),
            attachment.getFileName() == null ? "attachment" : attachment.getFileName());

    return body;//from w ww  .ja v  a  2 s. c  om
}

From source file:ch.ralscha.extdirectspring_itest.FileUploadServiceTest.java

@Test
public void testUpload() throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    CloseableHttpResponse response = null;
    try {// ww w .  ja va  2 s  . co m

        HttpPost post = new HttpPost("http://localhost:9998/controller/router");

        InputStream is = getClass().getResourceAsStream("/UploadTestFile.txt");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        ContentBody cbFile = new InputStreamBody(is, ContentType.create("text/plain"), "UploadTestFile.txt");
        builder.addPart("fileUpload", cbFile);
        builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT));
        builder.addPart("extAction", new StringBody("fileUploadService", ContentType.DEFAULT_TEXT));
        builder.addPart("extMethod", new StringBody("uploadTest", ContentType.DEFAULT_TEXT));
        builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT));
        builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT));

        builder.addPart("name",
                new StringBody("Jim", ContentType.create("text/plain", Charset.forName("UTF-8"))));
        builder.addPart("firstName", new StringBody("Ralph", ContentType.DEFAULT_TEXT));
        builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT));
        builder.addPart("email", new StringBody("test@test.ch", ContentType.DEFAULT_TEXT));

        post.setEntity(builder.build());
        response = client.execute(post);
        HttpEntity resEntity = response.getEntity();

        assertThat(resEntity).isNotNull();
        String responseString = EntityUtils.toString(resEntity);

        String prefix = "<html><body><textarea>";
        String postfix = "</textarea></body></html>";
        assertThat(responseString).startsWith(prefix);
        assertThat(responseString).endsWith(postfix);

        String json = responseString.substring(prefix.length(), responseString.length() - postfix.length());

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> rootAsMap = mapper.readValue(json, Map.class);
        assertThat(rootAsMap).hasSize(5);
        assertThat(rootAsMap.get("method")).isEqualTo("uploadTest");
        assertThat(rootAsMap.get("type")).isEqualTo("rpc");
        assertThat(rootAsMap.get("action")).isEqualTo("fileUploadService");
        assertThat(rootAsMap.get("tid")).isEqualTo(2);

        @SuppressWarnings("unchecked")
        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        assertThat(result).hasSize(7);
        assertThat(result.get("name")).isEqualTo("Jim");
        assertThat(result.get("firstName")).isEqualTo("Ralph");
        assertThat(result.get("age")).isEqualTo(25);
        assertThat(result.get("e-mail")).isEqualTo("test@test.ch");
        assertThat(result.get("fileName")).isEqualTo("UploadTestFile.txt");
        assertThat(result.get("fileContents")).isEqualTo("contents of upload file");
        assertThat(result.get("success")).isEqualTo(Boolean.TRUE);

        EntityUtils.consume(resEntity);

        is.close();
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(client);
    }
}

From source file:ch.ralscha.extdirectspring_itest.FileUploadControllerTest.java

@Test
public void testUpload() throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    InputStream is = null;//from  w  ww . j  a va2  s .com
    CloseableHttpResponse response = null;

    try {
        HttpPost post = new HttpPost("http://localhost:9998/controller/router");
        is = getClass().getResourceAsStream("/UploadTestFile.txt");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        ContentBody cbFile = new InputStreamBody(is, ContentType.create("text/plain"), "UploadTestFile.txt");
        builder.addPart("fileUpload", cbFile);
        builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT));
        builder.addPart("extAction", new StringBody("fileUploadController", ContentType.DEFAULT_TEXT));
        builder.addPart("extMethod", new StringBody("uploadTest", ContentType.DEFAULT_TEXT));
        builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT));
        builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT));

        builder.addPart("name",
                new StringBody("Jim", ContentType.create("text/plain", Charset.forName("UTF-8"))));
        builder.addPart("firstName", new StringBody("Ralph", ContentType.DEFAULT_TEXT));
        builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT));
        builder.addPart("email", new StringBody("test@test.ch", ContentType.DEFAULT_TEXT));

        post.setEntity(builder.build());
        response = client.execute(post);
        HttpEntity resEntity = response.getEntity();

        assertThat(resEntity).isNotNull();
        String responseString = EntityUtils.toString(resEntity);

        String prefix = "<html><body><textarea>";
        String postfix = "</textarea></body></html>";
        assertThat(responseString).startsWith(prefix);
        assertThat(responseString).endsWith(postfix);

        String json = responseString.substring(prefix.length(), responseString.length() - postfix.length());

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> rootAsMap = mapper.readValue(json, Map.class);
        assertThat(rootAsMap).hasSize(5);
        assertThat(rootAsMap.get("method")).isEqualTo("uploadTest");
        assertThat(rootAsMap.get("type")).isEqualTo("rpc");
        assertThat(rootAsMap.get("action")).isEqualTo("fileUploadController");
        assertThat(rootAsMap.get("tid")).isEqualTo(2);

        @SuppressWarnings("unchecked")
        Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result");
        assertThat(result).hasSize(7);
        assertThat(result.get("name")).isEqualTo("Jim");
        assertThat(result.get("firstName")).isEqualTo("Ralph");
        assertThat(result.get("age")).isEqualTo(25);
        assertThat(result.get("email")).isEqualTo("test@test.ch");
        assertThat(result.get("fileName")).isEqualTo("UploadTestFile.txt");
        assertThat(result.get("fileContents")).isEqualTo("contents of upload file");
        assertThat(result.get("success")).isEqualTo(Boolean.TRUE);

        EntityUtils.consume(resEntity);
    } finally {
        IOUtils.closeQuietly(response);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(client);
    }
}

From source file:org.apache.abdera2.test.client.MultipartRelatedEntityTest.java

License:asdf

@Test
public void testMultipartFormat() throws IOException {
    Entry entry = new FOMEntry();
    entry.setTitle("my image");
    entry.addAuthor("david");
    entry.setId("tag:apache.org,2008:234534344");
    entry.setSummary("multipart test");
    entry.setContent(new IRI("cid:234234@example.com"), "image/jpg");
    MultipartRelatedEntity request = new MultipartRelatedEntity(entry,
            new InputStreamBody(this.getClass().getResourceAsStream("/info.png"), "image/jpg", null),
            "image/jpg", "asdfasdfasdf");
    StringWriter sw = new StringWriter();
    WriterOutputStream os = new WriterOutputStream(sw);
    request.writeTo(os);// w  ww .java 2  s . co m

    String multipart = sw.toString();
    // System.out.println(sw.toString());

    assertTrue(multipart.contains("Content-ID: <234234@example.com>"));
    assertTrue(multipart.contains("Content-Type: image/jpg"));
}

From source file:com.surevine.alfresco.connector.SecurityModelConnector.java

public void setSecurityModel(final String securityModel) throws AlfrescoException {
    final Map<String, ContentBody> parts = new HashMap<String, ContentBody>();

    try {//from   w w w. j  a  va2s . com
        parts.put("updateNodeRef", new StringBody("workspace://SpacesStore/enhanced_security_custom_model"));
        parts.put("filedata", new InputStreamBody(new ByteArrayInputStream(securityModel.getBytes()),
                "text/xml", "enhancedSecurityCustomModel.xml"));
    } catch (final UnsupportedEncodingException e) {
        LOG.error("Failed to populate multipart form for updating security model.", e);
    }

    final AlfrescoHttpResponse response = doHttpPost(alfrescoUrlBase + "/wcservice/api/upload.html", parts);

    if (response.getHttpResponse().getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new AlfrescoException("Update of security model failed with status code "
                + response.getHttpResponse().getStatusLine().getStatusCode());
    }
}

From source file:com.mashape.unirest.android.request.body.MultipartBody.java

public MultipartBody field(String name, InputStream stream, String contentType, String fileName) {
    return field(name, new InputStreamBody(stream, contentType, fileName), true, contentType);
}

From source file:com.mashape.unirest.android.request.body.MultipartBody.java

public MultipartBody field(String name, InputStream stream, String fileName) {
    return field(name, new InputStreamBody(stream, "application/octet-stream", fileName), true,
            "application/octet-stream");
}