Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

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

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:com.facebook.stetho.dumpapp.RawDumpappHandler.java

private static HttpEntity createResponseEntity(byte[] data) {
    ByteArrayEntity entity = new ByteArrayEntity(data);
    if (isProbablyBinaryData(data)) {
        entity.setContentType(HTTP.OCTET_STREAM_TYPE);
    } else {//ww w  .j av  a 2  s  .  com
        entity.setContentType(HTTP.PLAIN_TEXT_TYPE);
    }
    return entity;
}

From source file:com.nominanuda.hyperapi.InputStreamEntityEncoder.java

@Override
protected HttpEntity encodeInternal(AnnotatedType p, InputStream value) {
    try {/*from w w w  .  ja  va2s  . c o  m*/
        byte[] barr = IO.read(value, true);
        String ct = Check.ifNullOrBlank(p.mediaType(), defaultContentType);
        ByteArrayEntity entity = new ByteArrayEntity(barr);
        entity.setContentType(ct);
        return entity;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.vuphone.assassins.android.http.HTTPPoster.java

public static void doLandMinePost(LandMine lm) {

    final HttpPost post = new HttpPost(VUphone.SERVER + PATH);
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    StringBuffer params = new StringBuffer();
    params.append("type=landMinePost&lat=" + lm.getLatitude() + "&lon=" + lm.getLongitude() + "&radius="
            + lm.getRadius());/*  w ww  .j av a2s  .  c o  m*/

    Log.v(VUphone.tag, pre + "Created parameter string: " + params);
    post.setEntity(new ByteArrayEntity(params.toString().getBytes()));

    // Do it
    Log.i(VUphone.tag, pre + "Executing post to " + VUphone.SERVER + PATH);

    HttpResponse resp = null;
    try {
        resp = c.execute(post);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        resp.getEntity().writeTo(bao);
        Log.d(VUphone.tag, pre + "Response from server: " + new String(bao.toByteArray()));
    } catch (ClientProtocolException e) {
        Log.e(VUphone.tag, pre + "ClientProtocolException executing post: " + e.getMessage());
    } catch (IOException e) {
        Log.e(VUphone.tag, pre + "IOException writing to ByteArrayOutputStream: " + e.getMessage());
    } catch (Exception e) {
        Log.e(VUphone.tag, pre + "Other Exception of type:" + e.getClass());
        Log.e(VUphone.tag, pre + "The message is: " + e.getMessage());
    }
}

From source file:org.wso2.dss.integration.test.odata.ODataTestUtils.java

public static Object[] sendPOST(String endpoint, String content, Map<String, String> headers)
        throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(endpoint);
    for (String headerType : headers.keySet()) {
        httpPost.setHeader(headerType, headers.get(headerType));
    }/*w  w w.  j  a  va  2  s .c om*/
    if (null != content) {
        HttpEntity httpEntity = new ByteArrayEntity(content.getBytes("UTF-8"));
        if (headers.get("Content-Type") == null) {
            httpPost.setHeader("Content-Type", "application/json");
        }
        httpPost.setEntity(httpEntity);
    }
    HttpResponse httpResponse = httpClient.execute(httpPost);
    if (httpResponse.getEntity() != null) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(httpResponse.getEntity().getContent()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();
        return new Object[] { httpResponse.getStatusLine().getStatusCode(), response.toString() };
    } else {
        return new Object[] { httpResponse.getStatusLine().getStatusCode() };
    }
}

From source file:org.jboss.aerogear.windows.mpns.internal.AbstractMpnsService.java

protected HttpPost postMessage(String subscriptionUri, byte[] requestBody,
        Collection<? extends Entry<String, String>> headers) {
    HttpPost method = new HttpPost(subscriptionUri);
    method.setEntity(new ByteArrayEntity(requestBody));

    for (Entry<String, String> header : headers) {
        method.addHeader(header.getKey(), header.getValue());
    }//w ww  .  ja v a 2 s . c o m

    return method;
}

From source file:org.fdroid.enigtext.mms.MmsSendHelper.java

private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms)
        throws ClientProtocolException, IOException {
    AndroidHttpClient client = null;//from  ww w  .j a v a  2 s  .  com

    try {
        Log.w("MmsSender", "Sending MMS1 of length: " + (mms != null ? mms.length : "null"));
        client = constructHttpClient(context, parameters);
        URI targetUrl = new URI(parameters.getMmsc());

        if (Util.isEmpty(targetUrl.getHost()))
            throw new IOException("Invalid target host: " + targetUrl.getHost() + " , " + targetUrl);

        HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpPost request = new HttpPost(parameters.getMmsc());
        ByteArrayEntity entity = new ByteArrayEntity(mms);

        entity.setContentType("application/vnd.wap.mms-message");

        request.setEntity(entity);
        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
        request.addHeader("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml");
        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsSendHelper", use);
        throw new IOException("Couldn't parse URI.");
    } finally {
        if (client != null)
            client.close();
    }
}

From source file:com.subgraph.vega.internal.model.requests.LazyEntityLoader.java

private HttpEntity createEntityCopy(RequestLogEntity entity) {
    final ByteArrayEntity copy = new ByteArrayEntity(entity.getContentArray());
    copy.setContentType(entity.getContentType());
    copy.setContentEncoding(copy.getContentEncoding());
    return copy;//from  www . ja  va  2s .  c  o  m
}

From source file:com.deployd.Deployd.java

public static JSONObject put(JSONObject input, String uri)
        throws ClientProtocolException, IOException, JSONException {

    HttpPut post = new HttpPut(endpoint + uri);
    HttpClient client = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(input.toString().getBytes("UTF8")));
    HttpResponse response = client.execute(post);
    ByteArrayEntity e = (ByteArrayEntity) response.getEntity();
    InputStream is = e.getContent();
    String data = new Scanner(is).next();
    JSONObject result = new JSONObject(data);
    return result;
}

From source file:org.fcrepo.mint.HttpPidMinterTest.java

@Test
public void testMintPid() throws Exception {
    final HttpPidMinter testMinter = new HttpPidMinter("http://localhost/minter", "POST", "", "", ".*/", "");

    final HttpClient mockClient = mock(HttpClient.class);
    final HttpResponse mockResponse = mock(HttpResponse.class);
    final ByteArrayEntity entity = new ByteArrayEntity("/foo/bar/baz".getBytes());
    testMinter.client = mockClient;/*from  w w w.j a v  a 2s.  c o m*/

    when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
    when(mockResponse.getEntity()).thenReturn(entity);

    final String pid = testMinter.get();
    verify(mockClient).execute(isA(HttpUriRequest.class));
    assertEquals(pid, "baz");
}

From source file:org.fcrepo.kernel.impl.identifiers.HttpPidMinterTest.java

@Test
public void testMintPid() throws Exception {
    final HttpPidMinter testMinter = new HttpPidMinter("http://localhost/minter", "POST", "", "", ".*/", "");

    final HttpClient mockClient = mock(HttpClient.class);
    final HttpResponse mockResponse = mock(HttpResponse.class);
    final ByteArrayEntity entity = new ByteArrayEntity("/foo/bar/baz".getBytes());
    testMinter.client = mockClient;// w ww .j  a va 2  s. c o m

    when(mockClient.execute(isA(HttpUriRequest.class))).thenReturn(mockResponse);
    when(mockResponse.getEntity()).thenReturn(entity);

    final String pid = testMinter.mintPid();
    verify(mockClient).execute(isA(HttpUriRequest.class));
    assertEquals(pid, "baz");
}