use of org.apache.http.entity.BufferedHttpEntity in project elasticsearch by elastic.
the class RequestLogger method buildTraceRequest.
/**
* Creates curl output for given request
*/
static String buildTraceRequest(HttpUriRequest request, HttpHost host) throws IOException {
String requestLine = "curl -iX " + request.getMethod() + " '" + host + getUri(request.getRequestLine()) + "'";
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
if (enclosingRequest.getEntity() != null) {
requestLine += " -d '";
HttpEntity entity = enclosingRequest.getEntity();
if (entity.isRepeatable() == false) {
entity = new BufferedHttpEntity(enclosingRequest.getEntity());
enclosingRequest.setEntity(entity);
}
requestLine += EntityUtils.toString(entity, StandardCharsets.UTF_8) + "'";
}
}
return requestLine;
}
use of org.apache.http.entity.BufferedHttpEntity in project camel by apache.
the class RestletTestSupport method doExecute.
public HttpResponse doExecute(HttpUriRequest method) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
try {
HttpResponse response = client.execute(method);
response.setEntity(new BufferedHttpEntity(response.getEntity()));
return response;
} finally {
client.close();
}
}
use of org.apache.http.entity.BufferedHttpEntity in project knox by apache.
the class PartiallyRepeatableHttpEntityTest method testIsRepeatable.
@Test
public void testIsRepeatable() throws Exception {
String text = "0123456789";
BasicHttpEntity basic;
PartiallyRepeatableHttpEntity replay;
basic = new BasicHttpEntity();
basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
replay = new PartiallyRepeatableHttpEntity(basic);
assertThat(replay.isRepeatable(), is(true));
basic = new BasicHttpEntity();
basic.setContent(new ByteArrayInputStream(text.getBytes(UTF8)));
BufferedHttpEntity buffered = new BufferedHttpEntity(basic);
replay = new PartiallyRepeatableHttpEntity(buffered);
assertThat(replay.isRepeatable(), is(true));
}
use of org.apache.http.entity.BufferedHttpEntity in project iaf by ibissource.
the class CmisHttpSender method getMethod.
@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
HttpRequestBase method = null;
try {
if (getMethodType().equals("GET")) {
method = new HttpGet(uri.build());
} else if (getMethodType().equals("POST")) {
HttpPost httpPost = new HttpPost(uri.build());
// send data
if (pvl.getParameterValue("writer") != null) {
Output writer = (Output) pvl.getParameterValue("writer").getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
httpPost.setHeader("Content-Encoding", "gzip");
writer.write(new GZIPOutputStream(out, 4096));
} else {
writer.write(out);
}
HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
httpPost.setEntity(entity);
out.close();
method = httpPost;
}
} else if (getMethodType().equals("PUT")) {
HttpPut httpPut = new HttpPut(uri.build());
// send data
if (pvl.getParameterValue("writer") != null) {
Output writer = (Output) pvl.getParameterValue("writer").getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
httpPut.setHeader("Content-Encoding", "gzip");
writer.write(new GZIPOutputStream(out, 4096));
} else {
writer.write(out);
}
HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
httpPut.setEntity(entity);
out.close();
method = httpPut;
}
} else if (getMethodType().equals("DELETE")) {
method = new HttpDelete(uri.build());
} else {
throw new MethodNotSupportedException("method [" + getMethodType() + "] not implemented");
}
} catch (Exception e) {
throw new SenderException(e);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
log.debug("append header [" + entry.getKey() + "] with value [" + entry.getValue() + "]");
method.addHeader(entry.getKey(), entry.getValue());
}
// Cmis creates it's own contentType depending on the method and bindingType
method.setHeader("Content-Type", getContentType());
log.debug(getLogPrefix() + "HttpSender constructed " + getMethodType() + "-method [" + method.getURI() + "] query [" + method.getURI().getQuery() + "] ");
return method;
}
use of org.apache.http.entity.BufferedHttpEntity in project azure-tools-for-java by Microsoft.
the class WebHDFSDeploy method deploy.
@Override
public Observable<String> deploy(File src, Observer<SparkLogLine> logSubject) {
// three steps to upload via webhdfs
// 1.put request to create new dir
// 2.put request to get 307 redirect uri from response
// 3.put redirect request with file content as setEntity
final URI dest = getUploadDir();
final HttpPut req = new HttpPut(dest.toString());
return http.request(req, null, this.createDirReqParams, null).doOnNext(resp -> {
if (resp.getStatusLine().getStatusCode() != 200) {
Exceptions.propagate(new UnknownServiceException("Can not create directory to save artifact using webHDFS storage type"));
}
}).map(ignored -> new HttpPut(dest.resolve(src.getName()).toString())).flatMap(put -> http.request(put, null, this.uploadReqParams, null)).map(resp -> resp.getFirstHeader("Location").getValue()).doOnNext(redirectedUri -> {
if (StringUtils.isBlank(redirectedUri)) {
Exceptions.propagate(new UnknownServiceException("Can not get valid redirect uri using webHDFS storage type"));
}
}).map(HttpPut::new).flatMap(put -> {
try {
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(src), -1, ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(true);
return http.request(put, new BufferedHttpEntity(reqEntity), URLEncodedUtils.parse(put.getURI(), "UTF-8"), null);
} catch (IOException ex) {
throw new RuntimeException(new IllegalArgumentException("Can not get local artifact when uploading" + ex.toString()));
}
}).map(ignored -> {
try {
return getArtifactUploadedPath(dest.resolve(src.getName()).toString());
} catch (final URISyntaxException ex) {
throw new RuntimeException(new IllegalArgumentException("Can not get valid artifact upload path" + ex.toString()));
}
});
}
Aggregations