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.mashape.unirest.android.request.HttpRequestWithBody.java

public MultipartBody field(String name, InputStream stream, String contentType, String fileName) {
    InputStreamBody inputStreamBody = new InputStreamBody(stream, contentType, fileName);
    MultipartBody body = new MultipartBody(this).field(name, inputStreamBody, true, contentType);
    this.body = body;
    return body;//  ww  w. j  a  va  2s  .c om
}

From source file:net.ychron.unirestinst.request.body.MultipartBody.java

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

From source file:net.ychron.unirestinst.request.HttpRequestWithBody.java

public MultipartBody field(String name, InputStream stream, ContentType contentType, String fileName) {
    InputStreamBody inputStreamBody = new InputStreamBody(stream, contentType, fileName);
    MultipartBody body = new MultipartBody(httpClientHelper, this).field(name, inputStreamBody, true,
            contentType.toString());/*w  w  w. j a v a 2 s . c o m*/
    this.body = body;
    return body;
}

From source file:anhttpclient.impl.request.HttpPostWebRequest.java

/**
 * {@inheritDoc}//w w  w. ja va 2 s .  co  m
 */
public void addPart(String partName, InputStream inputStream, String name, String mimeType) throws IOException {
    parts.put(partName, new InputStreamBody(inputStream, getMimeTypeOrDefault(mimeType), name));
    formParams.clear();
}

From source file:net.ychron.unirestinst.request.body.MultipartBody.java

public MultipartBody field(String name, InputStream stream, String fileName) {
    return field(name, new InputStreamBody(stream, ContentType.APPLICATION_OCTET_STREAM, fileName), true,
            ContentType.APPLICATION_OCTET_STREAM.getMimeType());
}

From source file:com.woonoz.proxy.servlet.HttpEntityEnclosingRequestHandler.java

private ContentBody buildContentBodyFromFileItem(FileItemStream fileItem) throws IOException {
    final String partName = fileItem.getFieldName();
    final String contentType = getContentTypeForFileItem(fileItem);
    return new InputStreamBody(BufferOnCreateInputStream.create(fileItem.openStream()), contentType, partName);
}

From source file:com.app.precared.utils.MultipartEntityBuilder.java

public MultipartEntityBuilder addBinaryBody(final String name, final InputStream stream,
        final ContentType contentType, final String filename) {
    return addPart(name, new InputStreamBody(stream, contentType, filename));
}

From source file:net.wm161.microblog.lib.backends.statusnet.HTTPAPIRequest.java

protected String getData(URI location) throws APIException {
    Log.d("HTTPAPIRequest", "Downloading " + location);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(location);
    client.addRequestInterceptor(preemptiveAuth, 0);

    client.getParams().setBooleanParameter("http.protocol.expect-continue", false);

    if (!m_params.isEmpty()) {
        MultipartEntity params = new MultipartEntity();
        for (Entry<String, Object> item : m_params.entrySet()) {
            Object value = item.getValue();
            ContentBody data;/*from www. ja v a  2 s  .  c  o  m*/
            if (value instanceof Attachment) {
                Attachment attachment = (Attachment) value;
                try {
                    data = new InputStreamBody(attachment.getStream(), attachment.contentType(),
                            attachment.name());
                    Log.d("HTTPAPIRequest",
                            "Found a " + attachment.contentType() + " attachment named " + attachment.name());
                } catch (FileNotFoundException e) {
                    getRequest().setError(ErrorType.ERROR_ATTACHMENT_NOT_FOUND);
                    throw new APIException();
                } catch (IOException e) {
                    getRequest().setError(ErrorType.ERROR_ATTACHMENT_NOT_FOUND);
                    throw new APIException();
                }
            } else {
                try {
                    data = new StringBody(value.toString());
                } catch (UnsupportedEncodingException e) {
                    getRequest().setError(ErrorType.ERROR_INTERNAL);
                    throw new APIException();
                }
            }
            params.addPart(item.getKey(), data);
        }
        post.setEntity(params);
    }

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(m_api.getAccount().getUser(),
            m_api.getAccount().getPassword());
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

    HttpResponse req;
    try {
        req = client.execute(post);
    } catch (ClientProtocolException e3) {
        getRequest().setError(ErrorType.ERROR_CONNECTION_BROKEN);
        throw new APIException();
    } catch (IOException e3) {
        getRequest().setError(ErrorType.ERROR_CONNECTION_FAILED);
        throw new APIException();
    }

    InputStream result;
    try {
        result = req.getEntity().getContent();
    } catch (IllegalStateException e1) {
        getRequest().setError(ErrorType.ERROR_INTERNAL);
        throw new APIException();
    } catch (IOException e1) {
        getRequest().setError(ErrorType.ERROR_CONNECTION_BROKEN);
        throw new APIException();
    }

    InputStreamReader in = null;
    int status;
    in = new InputStreamReader(result);
    status = req.getStatusLine().getStatusCode();
    Log.d("HTTPAPIRequest", "Got status code of " + status);
    setStatusCode(status);
    if (status >= 300 || status < 200) {
        getRequest().setError(ErrorType.ERROR_SERVER);
        Log.w("HTTPAPIRequest", "Server code wasn't 2xx, got " + m_status);
        throw new APIException();
    }

    int totalSize = -1;
    if (req.containsHeader("Content-length"))
        totalSize = Integer.parseInt(req.getFirstHeader("Content-length").getValue());

    char[] buffer = new char[1024];
    //2^17 = 131072.
    StringBuilder contents = new StringBuilder(131072);
    try {
        int size = 0;
        while ((totalSize > 0 && size < totalSize) || totalSize == -1) {
            int readSize = in.read(buffer);
            size += readSize;
            if (readSize == -1)
                break;
            if (totalSize >= 0)
                getRequest().publishProgress(new APIProgress((size / totalSize) * 5000));
            contents.append(buffer, 0, readSize);
        }
    } catch (IOException e) {
        getRequest().setError(ErrorType.ERROR_CONNECTION_BROKEN);
        throw new APIException();
    }
    return contents.toString();
}

From source file:nl.nn.adapterframework.http.mime.MultipartEntityBuilder.java

public MultipartEntityBuilder addBinaryBody(String name, InputStream stream, ContentType contentType,
        String filename) {/*from   w w w .  j  ava  2s. c  om*/
    return addPart(name, new InputStreamBody(stream, contentType, filename));
}

From source file:functionaltests.RestSchedulerPushPullFileTest.java

public void testIt(String spaceName, String spacePath, String destPath, boolean encode) throws Exception {
    File testPushFile = RestFuncTHelper.getDefaultJobXmlfile();
    // you can test pushing pulling a big file :
    // testPushFile = new File("path_to_a_big_file");
    File destFile = new File(new File(spacePath, destPath), testPushFile.getName());
    if (destFile.exists()) {
        destFile.delete();/*  www. ja  v a2  s. c  o m*/
    }

    // PUSHING THE FILE
    String pushfileUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));
    // either we encode or we test human readable path (with no special character inside)

    HttpPost reqPush = new HttpPost(pushfileUrl);
    setSessionHeader(reqPush);
    // we push a xml job as a simple test

    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("fileName", new StringBody(testPushFile.getName()));
    multipartEntity.addPart("fileContent", new InputStreamBody(FileUtils.openInputStream(testPushFile),
            MediaType.APPLICATION_OCTET_STREAM, null));
    reqPush.setEntity(multipartEntity);
    HttpResponse response = executeUriRequest(reqPush);

    System.out.println(response.getStatusLine());
    assertHttpStatusOK(response);
    Assert.assertTrue(destFile + " exists for " + spaceName, destFile.exists());

    Assert.assertTrue("Original file and result are equals for " + spaceName,
            FileUtils.contentEquals(testPushFile, destFile));

    // LISTING THE TARGET DIRECTORY
    String pullListUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath, "UTF-8") : destPath.replace("\\", "/")));

    HttpGet reqPullList = new HttpGet(pullListUrl);
    setSessionHeader(reqPullList);

    HttpResponse response2 = executeUriRequest(reqPullList);

    System.out.println(response2.getStatusLine());
    assertHttpStatusOK(response2);

    InputStream is = response2.getEntity().getContent();
    List<String> lines = IOUtils.readLines(is);
    HashSet<String> content = new HashSet<>(lines);
    System.out.println(lines);
    Assert.assertTrue("Pushed file correctly listed", content.contains(testPushFile.getName()));

    // PULLING THE FILE
    String pullfileUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(destPath + "/" + testPushFile.getName(), "UTF-8")
                    : destPath.replace("\\", "/") + "/" + testPushFile.getName()));

    HttpGet reqPull = new HttpGet(pullfileUrl);
    setSessionHeader(reqPull);

    HttpResponse response3 = executeUriRequest(reqPull);

    System.out.println(response3.getStatusLine());
    assertHttpStatusOK(response3);

    InputStream is2 = response3.getEntity().getContent();

    File answerFile = tmpFolder.newFile();
    FileUtils.copyInputStreamToFile(is2, answerFile);

    Assert.assertTrue("Original file and result are equals for " + spaceName,
            FileUtils.contentEquals(answerFile, testPushFile));

    // DELETING THE HIERARCHY
    String rootPath = destPath.substring(0, destPath.contains("/") ? destPath.indexOf("/") : destPath.length());
    String deleteUrl = getResourceUrl("dataspace/" + spaceName + "/"
            + (encode ? URLEncoder.encode(rootPath, "UTF-8") : rootPath.replace("\\", "/")));
    HttpDelete reqDelete = new HttpDelete(deleteUrl);
    setSessionHeader(reqDelete);

    HttpResponse response4 = executeUriRequest(reqDelete);

    System.out.println(response4.getStatusLine());

    assertHttpStatusOK(response4);

    Assert.assertTrue(destFile + " still exist", !destFile.exists());
}