Search in sources :

Example 21 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project gocd by gocd.

the class GoHttpClientHttpInvokerRequestExecutor method doExecuteRequest.

@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
    HttpPost postMethod = new HttpPost(config.getServiceUrl());
    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    entity.setContentType(getContentType());
    postMethod.setEntity(entity);
    BasicHttpContext context = null;
    if (environment.useSslContext()) {
        context = new BasicHttpContext();
        context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal());
    }
    try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) {
        validateResponse(response);
        InputStream responseBody = getResponseBody(response);
        return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 22 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project gitblit by gitblit.

the class FilestoreServletTest method testUpload.

@Test
public void testUpload() throws Exception {
    FileUtils.delete(filestore().getStorageFolder());
    filestore().clearFilestoreCache();
    RepositoryModel r = gitblit().getRepositoryModel(repoName);
    UserModel u = new UserModel("admin");
    u.canAdmin = true;
    //No upload limit
    settings().overrideSetting(Keys.filestore.maxUploadSize, FilestoreManager.UNDEFINED_SIZE);
    final BlobInfo blob = new BlobInfo(512 * FileUtils.KB);
    final String expectedUploadURL = GitBlitSuite.url + repoLfs + blob.hash;
    final String initialUploadURL = GitBlitSuite.url + repoLfs + "batch";
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(initialUploadURL);
    // add request header
    request.addHeader(HttpHeaders.ACCEPT, FilestoreServlet.GIT_LFS_META_MIME);
    request.addHeader(HttpHeaders.CONTENT_ENCODING, FilestoreServlet.GIT_LFS_META_MIME);
    String content = String.format("{%s:%s,%s:[{%s:%s,%s:%d}]}", "\"operation\"", "\"upload\"", "\"objects\"", "\"oid\"", "\"" + blob.hash + "\"", "\"size\"", blob.length);
    HttpEntity entity = new ByteArrayEntity(content.getBytes("UTF-8"));
    request.setEntity(entity);
    HttpResponse response = client.execute(request);
    String responseMessage = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    assertEquals(200, response.getStatusLine().getStatusCode());
    String expectedContent = String.format("{%s:[{%s:%s,%s:%d,%s:{%s:{%s:%s}}}]}", "\"objects\"", "\"oid\"", "\"" + blob.hash + "\"", "\"size\"", blob.length, "\"actions\"", "\"upload\"", "\"href\"", "\"" + expectedUploadURL + "\"");
    assertEquals(expectedContent, responseMessage);
    //Now try to upload the binary download
    HttpPut putRequest = new HttpPut(expectedUploadURL);
    putRequest.setEntity(new ByteArrayEntity(blob.blob));
    response = client.execute(putRequest);
    responseMessage = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    assertEquals(200, response.getStatusLine().getStatusCode());
    //Confirm behind the scenes that it is available
    ByteArrayOutputStream savedBlob = new ByteArrayOutputStream();
    assertEquals(Status.Available, filestore().downloadBlob(blob.hash, u, r, savedBlob));
    assertArrayEquals(blob.blob, savedBlob.toByteArray());
}
Also used : UserModel(com.gitblit.models.UserModel) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) RepositoryModel(com.gitblit.models.RepositoryModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 23 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project iosched by google.

the class HttpClientStack method setEntityIfNonEmptyBody.

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 24 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project XobotOS by xamarin.

the class HttpService method handleException.

protected void handleException(final HttpException ex, final HttpResponse response) {
    if (ex instanceof MethodNotSupportedException) {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    } else if (ex instanceof UnsupportedHttpVersionException) {
        response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
    } else if (ex instanceof ProtocolException) {
        response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
    } else {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
    byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
}
Also used : ProtocolException(org.apache.http.ProtocolException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) UnsupportedHttpVersionException(org.apache.http.UnsupportedHttpVersionException)

Example 25 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project nhin-d by DirectProject.

the class UnsecuredServiceRequestBase method buildEntityRequest.

///CLOVER:ON
/**
     * Generic logic for building an HttpEntityEnclosingRequest.
     * 
     * @param <R>
     *            the type of the request object.
     * @param request
     *            the request object to modify.
     * @param contents
     *            the HTTP entity to enclose.
     * @param contentType
     *            the content type of the request.
     * @param contentEncoding
     *            the content encoding of the request.
     * @return the modified request object.
     * @throws OAuthMessageSignerException
     *             see {@link OAuthConsumer#sign(Object)}.
     * @throws OAuthExpectationFailedException
     *             see {@link OAuthConsumer#sign(Object)}.
     * @throws OAuthCommunicationException
     *             see {@link OAuthConsumer#sign(Object)}.
     * @see OAuthConsumer#sign(Object)
     */
///CLOVER:OFF
protected final <R extends HttpEntityEnclosingRequest> R buildEntityRequest(R request, byte[] contents, String contentType, String contentEncoding) {
    final ByteArrayEntity entity = new ByteArrayEntity(contents);
    entity.setContentType(contentType);
    entity.setContentEncoding(contentEncoding);
    request.setEntity(entity);
    return request;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)74 HttpEntity (org.apache.http.HttpEntity)25 HttpPost (org.apache.http.client.methods.HttpPost)22 HttpResponse (org.apache.http.HttpResponse)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 JSONObject (org.json.JSONObject)10 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)8 JSONArray (org.json.JSONArray)7 InputStream (java.io.InputStream)6 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5