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 filename) 

Source Link

Usage

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????.//from ww  w  . j  a v a 2s .com
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional029() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????./*from   ww w  .j a  va  2  s  .c om*/
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&lang=jp-JP&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional030() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:org.deviceconnect.android.profile.restful.test.NormalNotificationProfileTestCase.java

/**
 * ??????????./*  w w  w  .  jav a  2 s .co  m*/
 * 
 * <pre>
 * ?HTTP
 * Method: POST
 * Path: notification/notify?deviceId=xxxx&type=0&dir=auto&lang=jp-JP&body=test_body&tag=tag1,tag2,tag3
 * </pre>
 * 
 * <pre>
 * ??
 * result?0???????
 * notificationid?1???????
 * </pre>
 */
public void testPostNotifyOptional031() {
    StringBuilder builder = new StringBuilder();
    builder.append(DCONNECT_MANAGER_URI);
    builder.append("/" + NotificationProfileConstants.PROFILE_NAME);
    builder.append("/" + NotificationProfileConstants.ATTRIBUTE_NOTIFY);
    builder.append("?");
    builder.append(DConnectProfileConstants.PARAM_DEVICE_ID + "=" + getDeviceId());
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TYPE + "=0");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_DIR + "=auto");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_LANG + "=jp-JP");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_BODY + "=test_body");
    builder.append("&");
    builder.append(NotificationProfileConstants.PARAM_TAG + "=tag1,tag2,tag3");

    builder.append("&");
    builder.append(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN + "=" + getAccessToken());
    try {
        HttpPost request = new HttpPost(builder.toString());
        String name = "test.png";
        AssetManager manager = getApplicationContext().getAssets();
        MultipartEntity entity = new MultipartEntity();
        entity.addPart(NotificationProfileConstants.PARAM_ICON, new InputStreamBody(manager.open(name), name));
        request.setEntity(entity);

        JSONObject root = sendRequest(request);
        Assert.assertNotNull("root is null.", root);
        Assert.assertEquals(DConnectMessage.RESULT_OK, root.getInt(DConnectMessage.EXTRA_RESULT));
        Assert.assertEquals("notificationid is not equals.",
                TestNotificationProfileConstants.NOTIFICATION_ID[0],
                root.getString(NotificationProfileConstants.PARAM_NOTIFICATION_ID));
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    } catch (IOException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:com.basistech.rosette.api.RosetteAPI.java

private void setupMultipartRequest(final DocumentRequest request, final ObjectWriter finalWriter,
        HttpPost post) {// w ww. ja  v a2  s  .c om

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMimeSubtype("mixed");
    builder.setMode(HttpMultipartMode.STRICT);

    FormBodyPartBuilder partBuilder = FormBodyPartBuilder.create("request",
            // Make sure we're not mislead by someone who puts a charset into the mime type.
            new AbstractContentBody(ContentType.parse(ContentType.APPLICATION_JSON.getMimeType())) {
                @Override
                public String getFilename() {
                    return null;
                }

                @Override
                public void writeTo(OutputStream out) throws IOException {
                    finalWriter.writeValue(out, request);
                }

                @Override
                public String getTransferEncoding() {
                    return MIME.ENC_BINARY;
                }

                @Override
                public long getContentLength() {
                    return -1;
                }
            });

    // Either one of 'name=' or 'Content-ID' would be enough.
    partBuilder.setField(MIME.CONTENT_DISPOSITION, "inline;name=\"request\"");
    partBuilder.setField("Content-ID", "request");

    builder.addPart(partBuilder.build());

    partBuilder = FormBodyPartBuilder.create("content",
            new InputStreamBody(request.getContentBytes(), ContentType.parse(request.getContentType())));
    partBuilder.setField(MIME.CONTENT_DISPOSITION, "inline;name=\"content\"");
    partBuilder.setField("Content-ID", "content");
    builder.addPart(partBuilder.build());
    builder.setCharset(StandardCharsets.UTF_8);

    HttpEntity entity = builder.build();
    post.setEntity(entity);
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * send multipart post request to server.
 *///www.j a v a 2  s .  com

private HttpResponse uploadMultiPart(String path, Map<String, String> reqParams, InputStream ins,
        String fileName) throws Exception {
    Charset utf8 = Charset.availableCharsets().get("UTF-8");
    MultipartEntity reqEntity = new MultipartEntity();
    HttpPost httppost = new HttpPost(getRequestBuilder().buildUrl(path));
    if (reqParams != null) {
        for (Map.Entry<String, String> entry : reqParams.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            reqEntity.addPart(key, new StringBody(value, utf8));
        }
    }
    if (ins != null) {
        ContentBody contentBody = new InputStreamBody(ins, fileName);
        reqEntity.addPart(fileName, contentBody);
    }
    httppost.setEntity(reqEntity);
    HttpResponse response = getRequestExecutor().execute(getRequestBuilder().buildOtherRequest(httppost)
            .withCredentials(getServerUsername(), getServerPassword())).getResponse();

    int status = response.getStatusLine().getStatusCode();
    if (status < 200 || status >= 300) {
        log.debug("response status = " + status);
        log.debug("output=" + getRequestExecutor().getContent());
        throw new Exception(response.getStatusLine().getReasonPhrase());
    }

    return response;

}

From source file:rapture.common.client.BaseHttpApi.java

protected String makeRequest(String fn, String request) throws ClientProtocolException, IOException {
    boolean notInError = true;
    log.trace("Entering makeRequest (" + fn + "," + request + ")");
    while (notInError) {
        try {/*  w w  w  .  j  a va2  s .c  o m*/
            HttpPost httppost = new HttpPost(fullUrl);
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("FUNCTION", new StringBody(fn));
            final byte[] compressedParams = getCompressedParams(request);
            if (compressedParams != null) {
                InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(compressedParams),
                        "params.txt") {
                    @Override
                    public long getContentLength() {
                        return compressedParams.length;
                    }
                };
                entity.addPart("PARAMS", isb);
            } else {
                entity.addPart("PARAMS", new StringBody(request));
            }
            long length = entity.getContentLength(); // will force it to
            // recalculate length

            if (log.isDebugEnabled())
                log.trace("Content Length is " + length);

            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            String responseObjectJson = inputStreamToString(response.getEntity().getContent()).toString();
            return responseObjectJson;
        } catch (IOException e) {
            log.error(String.format("Got exception during makeRequest. Will try to recover."));
            String oldUrl = currentUrl;
            stateManager.markURLBad(currentUrl);
            currentUrl = stateManager.getURL();
            if (currentUrl == null) {
                String message = String.format(
                        "Got exception accessing %s, and there are no available Rapture end points", oldUrl);
                throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, message, e);

            }
            httpclient = null;
            setup();
        }
    }
    return "";
}