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());
}
}
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());
}
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);
}
}
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);
}
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;
}
Aggregations