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:org.vuphone.assassins.android.http.HTTPPoster.java

public static void doGameAreaPost(double lat, double lon, float rad) {
    final HttpPost post = new HttpPost(VUphone.SERVER + PATH);
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    StringBuffer params = new StringBuffer();
    params.append("type=gameAreaPost&lat=" + lat + "&lon=" + lon + "&radius=" + rad);

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

    // Do it//  w  ww . j ava2  s .  com
    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.fcrepo.camel.FcrepoClientTest.java

@Test(expected = FcrepoOperationFailedException.class)
public void testGet100() throws Exception {
    final int status = 100;
    final URI uri = create(baseUrl);
    final ByteArrayEntity entity = new ByteArrayEntity(rdfXml.getBytes());
    entity.setContentType(RDF_XML);//ww w  .ja v  a 2s  .  co m

    doSetupMockRequest(RDF_XML, entity, status);
    testClient.get(uri, RDF_XML, null);
}

From source file:org.osmsurround.ae.osmrequest.OsmSignedRequestTemplate.java

private int openChangeset(String comment) throws JAXBException, HttpException, IOException {

    DefaultHttpClient httpClient = createHttpClient();

    HttpEntityEnclosingRequestBase request = new HttpPut(osmApiBaseUrl + "/api/0.6/changeset/create");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OsmRoot root = osmConvertService.createOsmRoot();
    osmEditorService.addChangeset(root, comment);

    schemaService.createOsmMarshaller().marshal(osmConvertService.toJaxbElement(root), baos);
    request.setEntity(new ByteArrayEntity(baos.toByteArray()));

    oAuthService.signRequest(request);/*w  w  w  .jav  a2 s  . c o  m*/
    HttpResponse httpResponse = httpClient.execute(request);
    log.info("Create result: " + httpResponse.getStatusLine().getStatusCode());
    if (httpResponse.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(httpResponse.getEntity().getContent()));
        return Integer.parseInt(reader.readLine());
    } else {
        throw RequestUtils.createExceptionFromHttpResponse(httpResponse);
    }

}

From source file:com.ninehcom.userinfo.util.HttpUtils.java

/**
 * Post stream/*  w  w  w  .ja  v  a2  s.  c  o  m*/
 * 
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers,
        Map<String, String> querys, byte[] body) throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (body != null) {
        request.setEntity(new ByteArrayEntity(body));
    }

    return httpClient.execute(request);
}

From source file:org.envirocar.app.network.HTTPClient.java

public static HttpEntity createEntity(byte[] data) throws IOException {
    AbstractHttpEntity entity;/*  w w  w  . j  av a 2s.c  o  m*/
    if (data.length < MIN_GZIP_SIZE) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:com.longle1.facedetection.TimedAsyncHttpResponseHandler.java

public void executePut(String putURL, RequestParams params, byte[] bb) {
    try {/*from w w w.  jav a 2  s .  c o  m*/
        AsyncHttpClient client = new AsyncHttpClient();
        ByteArrayEntity bae = null;
        bae = new ByteArrayEntity(bb);
        bae.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/octet-stream"));

        // Add SSL
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(mContext.getResources().openRawResource(R.raw.truststore), "changeit".toCharArray());
        SSLSocketFactory sf = new SSLSocketFactory(trustStore);
        client.setSSLSocketFactory(sf);

        client.setTimeout(30000);

        client.put(null, putURL + "?" + params.toString(), bae, null, this);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.i("executePut", "done");
}

From source file:com.androidex.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from   w w w  . jav a  2  s. c o m*/
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError, IOException {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards
        // compatibility.
        // If the request's post body is null, then the assumption is that
        // the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:eu.betaas.betaasandroidapp.rest.RestClient.java

public String[] postResource(String path, Map<HeaderType, String> headers, String body) {
    HttpHost target = new HttpHost(endpointUrl, port);

    String result[];/*from www  .j a v  a  2s  . c  o m*/

    try {
        // specify the get request
        HttpPost postRequest = new HttpPost(path);
        for (HeaderType header : headers.keySet()) {
            postRequest.setHeader(HEADERS.get(header), headers.get(header));
        }

        if (body != null) {
            HttpEntity b = new ByteArrayEntity(body.getBytes("UTF-8"));
            postRequest.setEntity(b);
        }

        HttpResponse httpResponse = httpclient.execute(target, postRequest);

        result = getResponse(httpResponse);

    } catch (Exception e) {
        result = new String[2];
        result[0] = "clientException";
        result[1] = e.getMessage();
    }

    return result;
}

From source file:ru.bozaro.protobuf.client.ProtobufClient.java

@NotNull
public HttpUriRequest createRequest(@NotNull Descriptors.MethodDescriptor method, @NotNull Message request)
        throws IOException, URISyntaxException {
    final HttpPost post = new HttpPost(baseUri.resolve(method.getService().getName().toLowerCase() + "/"
            + method.getName().toLowerCase() + format.getSuffix()));
    post.setHeader(HttpHeaders.CONTENT_TYPE, format.getMimeType());
    post.setHeader(HttpHeaders.CONTENT_ENCODING, StandardCharsets.UTF_8.name());
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    format.write(request, stream, StandardCharsets.UTF_8);
    post.setEntity(new ByteArrayEntity(stream.toByteArray()));
    return post;/*from   w ww  . j  av a2  s.c om*/
}

From source file:com.farru.android.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///w ww .j  a  v  a2s  .  co m
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards
        // compatibility.
        // If the request's post body is null, then the assumption is that
        // the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}