Search in sources :

Example 26 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project android_frameworks_base by ParanoidAndroid.

the class Request method sendRequest.

/**
     * Send the request line and headers
     */
void sendRequest(AndroidHttpClientConnection httpClientConnection) throws HttpException, IOException {
    // don't send cancelled requests
    if (mCancelled)
        return;
    if (HttpLog.LOGV) {
        HttpLog.v("Request.sendRequest() " + mHost.getSchemeName() + "://" + getHostPort());
        // HttpLog.v(mHttpRequest.getRequestLine().toString());
        if (false) {
            Iterator i = mHttpRequest.headerIterator();
            while (i.hasNext()) {
                Header header = (Header) i.next();
                HttpLog.v(header.getName() + ": " + header.getValue());
            }
        }
    }
    requestContentProcessor.process(mHttpRequest, mConnection.getHttpContext());
    httpClientConnection.sendRequestHeader(mHttpRequest);
    if (mHttpRequest instanceof HttpEntityEnclosingRequest) {
        httpClientConnection.sendRequestEntity((HttpEntityEnclosingRequest) mHttpRequest);
    }
    if (HttpLog.LOGV) {
        HttpLog.v("Request.requestSent() " + mHost.getSchemeName() + "://" + getHostPort() + mPath);
    }
}
Also used : Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) Iterator(java.util.Iterator)

Example 27 with HttpEntityEnclosingRequest

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

the class HttpService method handleRequest.

public void handleRequest(final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException {
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    HttpResponse response = null;
    try {
        HttpRequest request = conn.receiveRequestHeader();
        request.setParams(new DefaultedHttpParams(request.getParams(), this.params));
        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
            // Downgrade protocol version if greater than HTTP/1.1 
            ver = HttpVersion.HTTP_1_1;
        }
        if (request instanceof HttpEntityEnclosingRequest) {
            if (((HttpEntityEnclosingRequest) request).expectContinue()) {
                response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_CONTINUE, context);
                response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
                if (this.expectationVerifier != null) {
                    try {
                        this.expectationVerifier.verify(request, response, context);
                    } catch (HttpException ex) {
                        response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
                        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
                        handleException(ex, response);
                    }
                }
                if (response.getStatusLine().getStatusCode() < 200) {
                    // Send 1xx response indicating the server expections
                    // have been met
                    conn.sendResponseHeader(response);
                    conn.flush();
                    response = null;
                    conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
                }
            } else {
                conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
            }
        }
        if (response == null) {
            response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
            response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
            context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
            this.processor.process(request, context);
            doService(request, response, context);
        }
        // Make sure the request content is fully consumed
        if (request instanceof HttpEntityEnclosingRequest) {
            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
            if (entity != null) {
                entity.consumeContent();
            }
        }
    } catch (HttpException ex) {
        response = this.responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
        handleException(ex, response);
    }
    this.processor.process(response, context);
    conn.sendResponseHeader(response);
    conn.sendResponseEntity(response);
    conn.flush();
    if (!this.connStrategy.keepAlive(response, context)) {
        conn.close();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) ProtocolVersion(org.apache.http.ProtocolVersion) DefaultedHttpParams(org.apache.http.params.DefaultedHttpParams)

Example 28 with HttpEntityEnclosingRequest

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

the class RequestContent method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (request instanceof HttpEntityEnclosingRequest) {
        if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (request.containsHeader(HTTP.CONTENT_LEN)) {
            throw new ProtocolException("Content-Length header already present");
        }
        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity == null) {
            request.addHeader(HTTP.CONTENT_LEN, "0");
            return;
        }
        // Must specify a transfer encoding or a content length 
        if (entity.isChunked() || entity.getContentLength() < 0) {
            if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
            }
            request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
        } else {
            request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
        }
        // Specify a content type if known
        if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE)) {
            request.addHeader(entity.getContentType());
        }
        // Specify a content encoding if known
        if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
            request.addHeader(entity.getContentEncoding());
        }
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 29 with HttpEntityEnclosingRequest

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

the class HttpRequestExecutorExecuteMethodInterceptor method recordEntity.

protected void recordEntity(HttpMessage httpMessage, Trace trace) {
    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);
                    final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
                    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) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 30 with HttpEntityEnclosingRequest

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

the class HttpComponentsStreamingClientHttpRequest method executeInternal.

@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
    HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest && body != null) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new StreamingHttpEntity(getHeaders(), body);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
    return new HttpComponentsClientHttpResponse(httpResponse);
}
Also used : HttpEntity(org.apache.http.HttpEntity) 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