Search in sources :

Example 6 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project pinpoint by naver.

the class DefaultClientExchangeHandlerImplStartMethodInterceptor method recordEntity.

protected void recordEntity(HttpMessage httpMessage, SpanEventRecorder recorder) {
    if (httpMessage instanceof HttpEntityEnclosingRequest) {
        final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpMessage;
        try {
            final HttpEntity entity = entityRequest.getEntity();
            if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) {
                if (entitySampler.isSampling()) {
                    final String entityString = entityUtilsToString(entity, "UTF8", 1024);
                    recorder.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, entityString);
                }
            }
        } catch (Exception e) {
            logger.debug("HttpEntityEnclosingRequest entity record fail. Caused:{}", e.getMessage(), e);
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 7 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project openhab1-addons by openhab.

the class StreamClientImpl method createHttpRequest.

protected HttpUriRequest createHttpRequest(UpnpMessage upnpMessage, UpnpRequest upnpRequestOperation) throws MethodNotSupportedException {
    switch(upnpRequestOperation.getMethod()) {
        case GET:
            return new HttpGet(upnpRequestOperation.getURI());
        case SUBSCRIBE:
            return new HttpGet(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.SUBSCRIBE.getHttpName();
                }
            };
        case UNSUBSCRIBE:
            return new HttpGet(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.UNSUBSCRIBE.getHttpName();
                }
            };
        case POST:
            HttpEntityEnclosingRequest post = new HttpPost(upnpRequestOperation.getURI());
            post.setEntity(createHttpRequestEntity(upnpMessage));
            // Fantastic API
            return (HttpUriRequest) post;
        case NOTIFY:
            HttpEntityEnclosingRequest notify = new HttpPost(upnpRequestOperation.getURI()) {

                @Override
                public String getMethod() {
                    return UpnpRequest.Method.NOTIFY.getHttpName();
                }
            };
            notify.setEntity(createHttpRequestEntity(upnpMessage));
            // Fantastic API
            return (HttpUriRequest) notify;
        default:
            throw new MethodNotSupportedException(upnpRequestOperation.getHttpMethodName());
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) MethodNotSupportedException(org.apache.http.MethodNotSupportedException)

Example 8 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project robovm by robovm.

the class HttpRequestExecutor method doSendRequest.

/**
     * Send a request over a connection.
     * This method also handles the expect-continue handshake if necessary.
     * If it does not have to handle an expect-continue handshake, it will
     * not use the connection for reading or anything else that depends on
     * data coming in over the connection.
     *
     * @param request   the request to send, already
     *                  {@link #preProcess preprocessed}
     * @param conn      the connection over which to send the request,
     *                  already established
     * @param context   the context for sending the request
     *
     * @return  a terminal response received as part of an expect-continue
     *          handshake, or
     *          <code>null</code> if the expect-continue handshake is not used
     *
     * @throws HttpException      in case of a protocol or processing problem
     * @throws IOException        in case of an I/O problem
     */
protected HttpResponse doSendRequest(final HttpRequest request, final HttpClientConnection conn, final HttpContext context) throws IOException, HttpException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (conn == null) {
        throw new IllegalArgumentException("HTTP connection may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    HttpResponse response = null;
    context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.FALSE);
    conn.sendRequestHeader(request);
    if (request instanceof HttpEntityEnclosingRequest) {
        // Check for expect-continue handshake. We have to flush the
        // headers and wait for an 100-continue response to handle it.
        // If we get a different response, we must not send the entity.
        boolean sendentity = true;
        final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        if (((HttpEntityEnclosingRequest) request).expectContinue() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
            conn.flush();
            // As suggested by RFC 2616 section 8.2.3, we don't wait for a
            // 100-continue response forever. On timeout, send the entity.
            int tms = request.getParams().getIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 2000);
            if (conn.isResponseAvailable(tms)) {
                response = conn.receiveResponseHeader();
                if (canResponseHaveBody(request, response)) {
                    conn.receiveResponseEntity(response);
                }
                int status = response.getStatusLine().getStatusCode();
                if (status < 200) {
                    if (status != HttpStatus.SC_CONTINUE) {
                        throw new ProtocolException("Unexpected response: " + response.getStatusLine());
                    }
                    // discard 100-continue
                    response = null;
                } else {
                    sendentity = false;
                }
            }
        }
        if (sendentity) {
            conn.sendRequestEntity((HttpEntityEnclosingRequest) request);
        }
    }
    conn.flush();
    context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
    return response;
}
Also used : ProtocolException(java.net.ProtocolException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 9 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project spring-framework by spring-projects.

the class HttpComponentsAsyncClientHttpRequest method executeInternal.

@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
    Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
    return new ClientHttpResponseFuture(futureResponse, callback);
}
Also used : HttpEntity(org.apache.http.HttpEntity) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse)

Example 10 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequest method executeInternal.

@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
    return new HttpComponentsClientHttpResponse(httpResponse);
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse)

Aggregations

HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)38 HttpEntity (org.apache.http.HttpEntity)24 HttpResponse (org.apache.http.HttpResponse)14 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 Header (org.apache.http.Header)11 ProtocolVersion (org.apache.http.ProtocolVersion)11 HttpRequest (org.apache.http.HttpRequest)10 URI (java.net.URI)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 RequestWrapper (org.apache.http.impl.client.RequestWrapper)5 IOException (java.io.IOException)4 HttpException (org.apache.http.HttpException)4 InputStreamEntity (org.apache.http.entity.InputStreamEntity)4 ProtocolException (java.net.ProtocolException)3 Iterator (java.util.Iterator)3 HttpHost (org.apache.http.HttpHost)3 ProtocolException (org.apache.http.ProtocolException)3 HttpPost (org.apache.http.client.methods.HttpPost)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)3