Search in sources :

Example 16 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project platform_external_apache-http by android.

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 17 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project platform_external_apache-http by android.

the class AndroidHttpClient method toCurl.

/**
     * Generates a cURL command equivalent to the given request.
     */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();
    builder.append("curl ");
    // add in the method
    builder.append("-X ");
    builder.append(request.getMethod());
    builder.append(" ");
    for (Header header : request.getAllHeaders()) {
        if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }
    URI uri = request.getURI();
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }
    builder.append("\"");
    builder.append(uri);
    builder.append("\"");
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                if (isBinaryContent(request)) {
                    String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
                    builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
                    builder.append(" --data-binary @/tmp/$$.bin");
                } else {
                    String entityString = stream.toString();
                    builder.append(" --data-ascii \"").append(entityString).append("\"");
                }
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }
    return builder.toString();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Header(org.apache.http.Header) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestWrapper(org.apache.http.impl.client.RequestWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI)

Example 18 with HttpEntityEnclosingRequest

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

the class EntityhubTest method buildImportRdfData.

/**
     * Imports/updates RDF data of the file to the entityhub with the possibility
     * to restrict imports/updates to the parsed uri
     * @param file the file with the RDF data (needs to be in the classpath)
     * @param create if <code>true</code> the data are created (POST) otherwise
     * updated (PUT). 
     * @param uri if not <code>null</code> only data of this URI are imported by
     * specifying the id parameter
     */
protected Request buildImportRdfData(InputStream in, String contentType, boolean create, String uri) {
    Assert.assertNotNull(in);
    Assert.assertNotNull(contentType);
    Request request;
    String path;
    if (uri != null) {
        path = builder.buildUrl("/entityhub/entity", "id", uri);
    } else {
        path = builder.buildUrl("/entityhub/entity");
    }
    if (create) {
        request = builder.buildOtherRequest(new HttpPost(path));
    } else {
        request = builder.buildOtherRequest(new HttpPut(path));
    }
    //set the HttpEntity (both PUT and POST are HttpEntityEnclosingRequests)
    ((HttpEntityEnclosingRequest) request.getRequest()).setEntity(new InputStreamEntity(in, -1));
    //finally set the correct content-type of the provided data
    //currently fixed to "application/rdf+xml"
    request.getRequest().setHeader("Content-Type", contentType);
    return request;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) Request(org.apache.stanbol.commons.testing.http.Request) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 19 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project baker-android by bakerframework.

the class AndroidHttpClient method toCurl.

/**
     * Generates a cURL command equivalent to the given request.
     */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();
    builder.append("curl ");
    for (Header header : request.getAllHeaders()) {
        if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }
    URI uri = request.getURI();
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }
    builder.append("\"");
    builder.append(uri);
    builder.append("\"");
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                String entityString = stream.toString();
                // TODO: Check the content type, too.
                builder.append(" --data-ascii \"").append(entityString).append("\"");
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }
    return builder.toString();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Header(org.apache.http.Header) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestWrapper(org.apache.http.impl.client.RequestWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI)

Example 20 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project moco by dreamhead.

the class AbstractProxyResponseHandler method prepareRemoteRequest.

private HttpRequestBase prepareRemoteRequest(final FullHttpRequest request, final URL url) {
    HttpRequestBase remoteRequest = createRemoteRequest(request, url);
    RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).build();
    remoteRequest.setConfig(config);
    remoteRequest.setProtocolVersion(createVersion(request));
    long contentLength = HttpUtil.getContentLength(request, -1);
    if (contentLength > 0 && remoteRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) remoteRequest;
        entityRequest.setEntity(createEntity(request.content(), contentLength));
    }
    return remoteRequest;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

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