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.subgraph.vega.internal.http.proxy.VegaHttpService.java

private void sendCertificateDownloadResponse(VegaHttpServerConnection connection, HttpContext context)
        throws HttpException, IOException {
    final String pem = sslContextRepository.getCaCertificatePem();
    final byte[] body = EncodingUtils.getAsciiBytes(pem);
    ByteArrayEntity entity = new ByteArrayEntity(body);
    entity.setContentType("application/x-x509-ca-cert; charset=US-ASCII");
    sendResponseOk(connection, context, entity);
}

From source file:org.eobjects.analyzer.cluster.http.HttpClusterManager.java

private LazyRef<AnalysisResult> sendRequest(final String slaveEndpoint, final byte[] bytes,
        final List<Throwable> errors) {
    return new LazyRef<AnalysisResult>() {
        @Override//from   ww w.j  a  v  a2  s  .c  o  m
        protected AnalysisResult fetch() throws Throwable {
            // send the HTTP request
            final HttpPost request = new HttpPost(slaveEndpoint);
            request.setEntity(new ByteArrayEntity(bytes));
            final HttpResponse response = _httpClient.execute(request);

            // handle the response
            final StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() != 200) {
                throw new IllegalStateException("Slave server responded with an error: "
                        + statusLine.getReasonPhrase() + " (" + statusLine.getStatusCode() + ")");
            }

            final InputStream inputStream = response.getEntity().getContent();
            try {
                AnalysisResult result = readResult(inputStream, errors);
                return result;
            } finally {
                FileHelper.safeClose(inputStream);
            }
        }
    };
}

From source file:com.netscape.cmsutil.ocsp.OCSPProcessor.java

public OCSPResponse submitRequest(String url, OCSPRequest request) throws Exception {

    if (verbose)/*from  w  w  w .  ja  v  a  2s .c om*/
        System.out.println("URL: " + url);

    HttpClient httpClient = new DefaultHttpClient();

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        request.encode(os);
        byte[] requestData = os.toByteArray();

        if (verbose) {
            System.out.println("Data Length: " + requestData.length);
            System.out.println("Data: " + Utils.base64encode(requestData));
        }

        ByteArrayEntity requestEntity = new ByteArrayEntity(requestData);
        requestEntity.setContentType(ContentType.APPLICATION_OCTET_STREAM.getMimeType());

        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(requestEntity);

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        try (InputStream is = responseEntity.getContent()) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int b;
            while ((b = is.read()) != -1) {
                buffer.write(b);
            }

            // construct OCSP response
            return (OCSPResponse) OCSPResponse.getTemplate()
                    .decode(new ByteArrayInputStream(buffer.toByteArray()));

        } finally {
            EntityUtils.consume(responseEntity);
        }

    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:com.arcbees.vcs.stash.StashApi.java

@Override
public Comment postComment(Integer pullRequestId, String comment) throws IOException {
    String requestUrl = apiPaths.addComment(repositoryOwner, repositoryName, pullRequestId);

    HttpPost request = new HttpPost(requestUrl);
    request.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()));
    request.setEntity(new ByteArrayEntity(gson.toJson(new StashComment(comment)).getBytes(Charsets.UTF_8)));

    return processResponse(httpClient, request, credentials, gson, StashComment.class);
}

From source file:co.uk.alt236.restclient4android.net.Connection.java

private HttpRequestBase setupHttpRequest(NetworkRequest request, URI uri) {
    String method = request.getAction().toUpperCase();
    String body = request.getRequestBody();

    HttpRequestBase httpReq = null;//from  w  ww  .j av a 2  s  .  c o m
    HttpEntity entity = null;

    if (body != null) {
        body = body.trim();
        if (body.length() > 0) {
            entity = new ByteArrayEntity(body.getBytes());
        }
    }

    if ("GET".equals(method)) {
        httpReq = new HttpGet(uri);

    } else if ("POST".equals(method)) {
        httpReq = new HttpPost(uri);

        if (entity != null) {
            ((HttpPost) httpReq).setEntity(entity);
        }

    } else if ("PUT".equals(method)) {
        httpReq = new HttpPut(uri);
        if (entity != null) {
            ((HttpPut) httpReq).setEntity(entity);
        }

    } else if ("DELETE".equals(method)) {
        httpReq = new HttpDelete(uri);
    } else if ("HEAD".equals(method)) {
        httpReq = new HttpHead(uri);
    } else if ("TRACE".equals(method)) {
        httpReq = new HttpTrace(uri);
    } else if ("OPTIONS".equals(method)) {
        httpReq = new HttpOptions(uri);
    }

    setAuthentication(httpReq, request);
    setHeaders(httpReq, request);

    return httpReq;
}

From source file:com.guardtime.ksi.service.client.http.apache.ApacheHttpClient.java

private ApacheHttpPostRequestFuture post(InputStream request, URL url) throws KSIClientException {
    try {//from w w w.j a  va  2 s  .  c o m
        HttpPost httpRequest = new HttpPost(url.toURI());
        httpRequest.setHeader(AbstractHttpClient.HEADER_NAME_CONTENT_TYPE,
                AbstractHttpClient.HEADER_APPLICATION_KSI_REQUEST);
        ByteArrayEntity entity = new ByteArrayEntity(Util.toByteArray(request));
        entity.setChunked(false);
        httpRequest.setEntity(entity);
        Future<HttpResponse> future = this.apacheClient.execute(httpRequest, null);
        return new ApacheHttpPostRequestFuture(future);
    } catch (URISyntaxException e) {
        throw new KSIClientException("Invalid URI " + settings.getSigningUrl(), e);
    } catch (IOException e) {
        throw new KSIClientException("Reading data from stream failed", e);
    }
}

From source file:brooklyn.networking.cloudstack.HttpUtil.java

public static HttpToolResponse httpPost(HttpClient httpClient, URI uri, Multimap<String, String> headers,
        byte[] body) {
    HttpPost httpPost = new HttpPost(uri);
    for (Map.Entry<String, String> entry : headers.entries()) {
        httpPost.addHeader(entry.getKey(), entry.getValue());
    }// ww w .  j a  v  a 2  s . c  om
    if (body != null) {
        HttpEntity httpEntity = new ByteArrayEntity(body);
        httpPost.setEntity(httpEntity);
    }

    long startTime = System.currentTimeMillis();
    try {
        HttpResponse httpResponse = httpClient.execute(httpPost);

        try {
            return new HttpToolResponse(httpResponse, startTime);
        } finally {
            EntityUtils.consume(httpResponse.getEntity());
        }
    } catch (Exception e) {
        throw Exceptions.propagate(e);
    }
}

From source file:org.fcrepo.integration.connector.file.FileConnectorIT.java

/**
 * I should be able to link to content on a federated filesystem.
 *
 * @throws IOException thrown during this function
 **///from   w w  w.j  a  v  a  2s  .  c  om
@Test
public void testFederatedDatastream() throws IOException {
    final String federationAddress = serverAddress + "files/FileSystem1/ds1";
    final String linkingAddress;
    try (final CloseableHttpResponse response = client.execute(new HttpPost(serverAddress))) {
        EntityUtils.consume(response.getEntity());
        linkingAddress = response.getFirstHeader("Location").getValue();
    }

    // link from the object to the content of the file on the federated filesystem
    final HttpPatch patch = new HttpPatch(linkingAddress);
    patch.addHeader("Content-Type", "application/sparql-update");
    patch.setEntity(new ByteArrayEntity(("INSERT DATA { <> <http://some-vocabulary#hasExternalContent> " + "<"
            + federationAddress + "> . }").getBytes()));
    assertEquals("Couldn't link to external datastream!", NO_CONTENT.getStatusCode(), getStatus(patch));
}

From source file:bbd.basesimplenet.net.httpstacks.HttpClientStack.java

/**
 * ?HttpEntity/*  w  w w . j a  v  a  2  s  . c om*/
 *
 * @param httpRequest
 * @param request
 */
private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}

From source file:guru.nidi.ramltester.ServletRamlMessageTest.java

@Test
public void content() throws Exception {
    final HttpPost post = new HttpPost(url("test/more"));
    final HttpEntity entity = new ByteArrayEntity(new byte[] { 65, 66, 67 });
    post.setEntity(entity);//  w  w w.j  a  va2  s  . co  m

    execute(post, new MessageTester() {
        @Override
        public void test(HttpServletRequest servletRequest, HttpServletResponse servletResponse,
                RamlRequest ramlRequest, RamlResponse ramlResponse) throws IOException {
            assertEquals("ABC", stringOf(ramlRequest.getContent()));
            assertEquals("ABC", stringOf(ramlResponse.getContent()));
        }
    });
}