Search in sources :

Example 1 with BufferedHttpEntity

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Example 2 with BufferedHttpEntity

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();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpResponse(org.apache.http.HttpResponse)

Example 3 with BufferedHttpEntity

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));
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.Test)

Example 4 with BufferedHttpEntity

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpPut(org.apache.http.client.methods.HttpPut) CmisConnectionException(org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) IOException(java.io.IOException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) Output(org.apache.chemistry.opencmis.client.bindings.spi.http.Output) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) SenderException(nl.nn.adapterframework.core.SenderException) Map(java.util.Map)

Example 5 with BufferedHttpEntity

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()));
        }
    });
}
Also used : NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull) Exceptions(rx.exceptions.Exceptions) URISyntaxException(java.net.URISyntaxException) RequestConfig(org.apache.http.client.config.RequestConfig) StringUtils(org.apache.commons.lang3.StringUtils) ILogger(com.microsoft.azure.hdinsight.common.logger.ILogger) Observable(rx.Observable) WebHdfsParamsBuilder(com.microsoft.azure.hdinsight.sdk.storage.webhdfs.WebHdfsParamsBuilder) URI(java.net.URI) IClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.IClusterDetail) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) URIBuilder(org.apache.http.client.utils.URIBuilder) JobUtils(com.microsoft.azure.hdinsight.spark.jobs.JobUtils) ContentType(org.apache.http.entity.ContentType) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Observer(rx.Observer) File(java.io.File) HttpObservable(com.microsoft.azure.hdinsight.sdk.common.HttpObservable) List(java.util.List) HttpPut(org.apache.http.client.methods.HttpPut) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) InputStreamEntity(org.apache.http.entity.InputStreamEntity) NameValuePair(org.apache.http.NameValuePair) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) SparkLogLine(com.microsoft.azure.hdinsight.spark.common.log.SparkLogLine) UnknownServiceException(java.net.UnknownServiceException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) UnknownServiceException(java.net.UnknownServiceException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) FileInputStream(java.io.FileInputStream) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)42 HttpEntity (org.apache.http.HttpEntity)30 IOException (java.io.IOException)21 HttpResponse (org.apache.http.HttpResponse)16 HttpGet (org.apache.http.client.methods.HttpGet)11 Header (org.apache.http.Header)9 InputStream (java.io.InputStream)8 HttpClient (org.apache.http.client.HttpClient)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 HttpException (org.apache.http.HttpException)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 URISyntaxException (java.net.URISyntaxException)5 HttpHost (org.apache.http.HttpHost)4 HttpRequest (org.apache.http.HttpRequest)4 StatusLine (org.apache.http.StatusLine)4 AuthenticationException (org.apache.http.auth.AuthenticationException)4 CredentialsProvider (org.apache.http.client.CredentialsProvider)4