Example usage for org.apache.http.entity BufferedHttpEntity BufferedHttpEntity

List of usage examples for org.apache.http.entity BufferedHttpEntity BufferedHttpEntity

Introduction

In this page you can find the example usage for org.apache.http.entity BufferedHttpEntity BufferedHttpEntity.

Prototype

public BufferedHttpEntity(HttpEntity httpEntity) throws IOException 

Source Link

Usage

From source file:org.springframework.extensions.webscripts.connector.RemoteClient.java

/**
 * Service a remote URL and write the the result into an output stream.
 * If an InputStream is provided then a POST will be performed with the content
 * pushed to the url. Otherwise a standard GET will be performed.
 * /*from   ww w .  jav a  2 s.  c  om*/
 * @param url    The URL to open and retrieve data from
 * @param in     The optional InputStream - if set a POST or similar will be performed
 * @param out    The OutputStream to write result to
 * @param res    Optional HttpServletResponse - to which response headers will be copied - i.e. proxied
 * @param status The status object to apply the response code too
 * 
 * @return encoding specified by the source URL - may be null
 * 
 * @throws IOException
 */
private String service(URL url, InputStream in, OutputStream out, HttpServletRequest req,
        HttpServletResponse res, ResponseStatus status) throws IOException {
    final boolean trace = logger.isTraceEnabled();
    final boolean debug = logger.isDebugEnabled();
    if (debug) {
        logger.debug("Executing " + "(" + requestMethod + ") " + url.toString());
        if (in != null)
            logger.debug(" - InputStream supplied - will push...");
        if (out != null)
            logger.debug(" - OutputStream supplied - will stream response...");
        if (req != null && res != null)
            logger.debug(" - Full Proxy mode between servlet request and response...");
    }

    // aquire and configure the HttpClient
    HttpClient httpClient = createHttpClient(url);

    URL redirectURL = url;
    HttpResponse response;
    HttpRequestBase method = null;
    int retries = 0;
    // Only process redirects if we are not processing a 'push'
    int maxRetries = in == null ? this.maxRedirects : 1;
    try {
        do {
            // Release a previous method that we processed due to a redirect
            if (method != null) {
                method.reset();
                method = null;
            }

            switch (this.requestMethod) {
            default:
            case GET:
                method = new HttpGet(redirectURL.toString());
                break;
            case PUT:
                method = new HttpPut(redirectURL.toString());
                break;
            case POST:
                method = new HttpPost(redirectURL.toString());
                break;
            case DELETE:
                method = new HttpDelete(redirectURL.toString());
                break;
            case HEAD:
                method = new HttpHead(redirectURL.toString());
                break;
            case OPTIONS:
                method = new HttpOptions(redirectURL.toString());
                break;
            }

            // proxy over any headers from the request stream to proxied request
            if (req != null) {
                Enumeration<String> headers = req.getHeaderNames();
                while (headers.hasMoreElements()) {
                    String key = headers.nextElement();
                    if (key != null) {
                        key = key.toLowerCase();
                        if (!this.removeRequestHeaders.contains(key) && !this.requestProperties.containsKey(key)
                                && !this.requestHeaders.containsKey(key)) {
                            method.setHeader(key, req.getHeader(key));
                            if (trace)
                                logger.trace("Proxy request header: " + key + "=" + req.getHeader(key));
                        }
                    }
                }
            }

            // apply request properties, allows for the assignment and override of specific header properties
            // firstly pre-configured headers are applied and overridden/augmented by runtime request properties 
            final Map<String, String> headers = (Map<String, String>) this.requestHeaders.clone();
            headers.putAll(this.requestProperties);
            if (headers.size() != 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    String headerName = entry.getKey();
                    String headerValue = headers.get(headerName);
                    if (headerValue != null) {
                        method.setHeader(headerName, headerValue);
                    }
                    if (trace)
                        logger.trace("Set request header: " + headerName + "=" + headerValue);
                }
            }

            // Apply cookies
            if (this.cookies != null && !this.cookies.isEmpty()) {
                StringBuilder builder = new StringBuilder(128);
                for (Map.Entry<String, String> entry : this.cookies.entrySet()) {
                    if (builder.length() != 0) {
                        builder.append(';');
                    }
                    builder.append(entry.getKey());
                    builder.append('=');
                    builder.append(entry.getValue());
                }

                String cookieString = builder.toString();

                if (debug)
                    logger.debug("Setting Cookie header: " + cookieString);
                method.setHeader(HEADER_COOKIE, cookieString);
            }

            // HTTP basic auth support
            if (this.username != null && this.password != null) {
                String auth = this.username + ':' + this.password;
                method.addHeader("Authorization",
                        "Basic " + Base64.encodeBytes(auth.getBytes(), Base64.DONT_BREAK_LINES));
                if (debug)
                    logger.debug("Applied HTTP Basic Authorization for user: " + this.username);
            }

            // prepare the POST/PUT entity data if input supplied
            if (in != null) {
                method.setHeader(HEADER_CONTENT_TYPE, getRequestContentType());
                if (debug)
                    logger.debug("Set Content-Type=" + getRequestContentType());

                boolean urlencoded = getRequestContentType().startsWith(X_WWW_FORM_URLENCODED);
                if (!urlencoded) {
                    // apply content-length here if known (i.e. from proxied req)
                    // if this is not set, then the content will be buffered in memory
                    long contentLength = -1L;
                    if (req != null) {
                        String contentLengthStr = req.getHeader(HEADER_CONTENT_LENGTH);
                        if (contentLengthStr != null) {
                            try {
                                long actualContentLength = Long.parseLong(contentLengthStr);
                                if (actualContentLength > 0) {
                                    contentLength = actualContentLength;
                                }
                            } catch (NumberFormatException e) {
                                logger.warn("Can't parse 'Content-Length' header from '" + contentLengthStr
                                        + "'. The contentLength is set to -1");
                            }
                        }
                    }

                    if (debug)
                        logger.debug(requestMethod + " entity Content-Length=" + contentLength);

                    // remove the Content-Length header as the setEntity() method will perform this explicitly
                    method.removeHeaders(HEADER_CONTENT_LENGTH);

                    try {
                        // Apache doc for AbstractHttpEntity states:
                        // HttpClient must use chunk coding if the entity content length is unknown (== -1).
                        HttpEntity entity = new InputStreamEntity(in, contentLength);
                        ((HttpEntityEnclosingRequest) method)
                                .setEntity(contentLength == -1L || contentLength > 16384L ? entity
                                        : new BufferedHttpEntity(entity));
                        ((HttpEntityEnclosingRequest) method).setHeader(HTTP.EXPECT_DIRECTIVE,
                                HTTP.EXPECT_CONTINUE);
                    } catch (IOException e) {
                        // During the creation of the BufferedHttpEntity the underlying stream can be closed by the client,
                        // this happens if the request is discarded by the browser - we don't log this IOException as INFO
                        // as that would fill the logs with unhelpful noise - enable DEBUG logging to see these messages.
                        throw new RuntimeException(e.getMessage(), e);
                    }
                } else {
                    if (req != null) {
                        // apply any supplied request parameters
                        Map<String, String[]> postParams = req.getParameterMap();
                        if (postParams != null) {
                            List<NameValuePair> params = new ArrayList<NameValuePair>(postParams.size());
                            for (String key : postParams.keySet()) {
                                String[] values = postParams.get(key);
                                for (int i = 0; i < values.length; i++) {
                                    params.add(new BasicNameValuePair(key, values[i]));
                                }
                            }
                        }
                        // ensure that the Content-Length header is not directly proxied - as the underlying
                        // HttpClient will encode the body as appropriate - cannot assume same as the original client sent
                        method.removeHeaders(HEADER_CONTENT_LENGTH);
                    } else {
                        // Apache doc for AbstractHttpEntity states:
                        // HttpClient must use chunk coding if the entity content length is unknown (== -1).
                        HttpEntity entity = new InputStreamEntity(in, -1L);
                        ((HttpEntityEnclosingRequest) method).setEntity(entity);
                        ((HttpEntityEnclosingRequest) method).setHeader(HTTP.EXPECT_DIRECTIVE,
                                HTTP.EXPECT_CONTINUE);
                    }
                }
            }

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Execute the method to get the response
            response = httpClient.execute(method);

            redirectURL = processResponse(redirectURL, response);
        } while (redirectURL != null && ++retries < maxRetries);

        // record the status code for the internal response object
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode >= HttpServletResponse.SC_INTERNAL_SERVER_ERROR && this.exceptionOnError) {
            buildProxiedServerError(response);
        } else if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
            // Occurs when server is down and likely an ELB response 
            throw new ConnectException(response.toString());
        }
        boolean allowResponseCommit = (responseCode != HttpServletResponse.SC_UNAUTHORIZED
                || commitResponseOnAuthenticationError);
        status.setCode(responseCode);
        if (debug)
            logger.debug("Response status code: " + responseCode);

        // walk over headers that are returned from the connection
        // if we have a servlet response, push the headers back to the existing response object
        // otherwise, store headers on status
        Header contentType = null;
        Header contentLength = null;
        for (Header header : response.getAllHeaders()) {
            // NOTE: Tomcat does not appear to be obeying the servlet spec here.
            //       If you call setHeader() the spec says it will "clear existing values" - i.e. not
            //       add additional values to existing headers - but for Server and Transfer-Encoding
            //       if we set them, then two values are received in the response...
            // In addition handle the fact that the key can be null.
            final String key = header.getName();
            if (key != null) {
                if (!key.equalsIgnoreCase(HEADER_SERVER) && !key.equalsIgnoreCase(HEADER_TRANSFER_ENCODING)) {
                    if (res != null && allowResponseCommit
                            && !this.removeResponseHeaders.contains(key.toLowerCase())) {
                        res.setHeader(key, header.getValue());
                    }

                    // store headers back onto status
                    status.setHeader(key, header.getValue());

                    if (trace)
                        logger.trace("Response header: " + key + "=" + header.getValue());
                }

                // grab a reference to the the content-type header here if we find it
                if (contentType == null && key.equalsIgnoreCase(HEADER_CONTENT_TYPE)) {
                    contentType = header;
                    // additional optional processing based on the Content-Type header
                    processContentType(url, res, contentType);
                }
                // grab a reference to the Content-Length header here if we find it
                else if (contentLength == null && key.equalsIgnoreCase(HEADER_CONTENT_LENGTH)) {
                    contentLength = header;
                }
            }
        }

        // locate response encoding from the headers
        String encoding = null;
        String ct = null;
        if (contentType != null) {
            ct = contentType.getValue();
            int csi = ct.indexOf(CHARSETEQUALS);
            if (csi != -1) {
                encoding = ct.substring(csi + CHARSETEQUALS.length());
                if ((csi = encoding.lastIndexOf(';')) != -1) {
                    encoding = encoding.substring(0, csi);
                }
                if (debug)
                    logger.debug("Response charset: " + encoding);
            }
        }
        if (debug)
            logger.debug("Response encoding: " + contentType);

        // generate container driven error message response for specific response codes
        if (res != null && responseCode == HttpServletResponse.SC_UNAUTHORIZED && allowResponseCommit) {
            res.sendError(responseCode, response.getStatusLine().getReasonPhrase());
        } else {
            // push status to existing response object if required
            if (res != null && allowResponseCommit) {
                res.setStatus(responseCode);
            }
            // perform the stream write from the response to the output
            int bufferSize = this.bufferSize;
            if (contentLength != null) {
                long length = Long.parseLong(contentLength.getValue());
                if (length < bufferSize) {
                    bufferSize = (int) length;
                }
            }
            copyResponseStreamOutput(url, res, out, response, ct, bufferSize);
        }

        // if we get here call was successful
        return encoding;
    } catch (ConnectTimeoutException | SocketTimeoutException timeErr) {
        // caught a socket timeout IO exception - apply internal error code
        logger.info("Exception calling (" + requestMethod + ") " + url.toString());
        status.setCode(HttpServletResponse.SC_REQUEST_TIMEOUT);
        status.setException(timeErr);
        status.setMessage(timeErr.getMessage());
        if (res != null) {
            //return a Request Timeout error
            res.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT, timeErr.getMessage());
        }

        throw timeErr;
    } catch (UnknownHostException | ConnectException hostErr) {
        // caught an unknown host IO exception 
        logger.info("Exception calling (" + requestMethod + ") " + url.toString());
        status.setCode(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        status.setException(hostErr);
        status.setMessage(hostErr.getMessage());
        if (res != null) {
            // return server error code
            res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE, hostErr.getMessage());
        }

        throw hostErr;
    } catch (IOException ioErr) {
        // caught a general IO exception - apply generic error code so one gets returned
        logger.info("Exception calling (" + requestMethod + ") " + url.toString());
        status.setCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        status.setException(ioErr);
        status.setMessage(ioErr.getMessage());
        if (res != null) {
            res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ioErr.getMessage());
        }

        throw ioErr;
    } catch (RuntimeException e) {
        // caught an exception - apply generic error code so one gets returned
        logger.debug("Exception calling (" + requestMethod + ") " + url.toString());
        status.setCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        status.setException(e);
        status.setMessage(e.getMessage());
        if (res != null) {
            res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }

        throw e;
    } finally {
        // reset state values
        if (method != null) {
            method.releaseConnection();
        }
        setRequestContentType(null);
        this.requestMethod = HttpMethod.GET;
    }
}

From source file:net.networksaremadeofstring.rhybudd.ZenossAPI.java

public Drawable GetGraph(String urlString) throws IOException, URISyntaxException {
    HttpGet httpRequest = new HttpGet(new URL(ZENOSS_INSTANCE + urlString).toURI());
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    final long contentLength = bufHttpEntity.getContentLength();
    //Log.e("GetGraph",Long.toString(contentLength));
    if (contentLength >= 0) {
        InputStream is = bufHttpEntity.getContent();
        Bitmap bitmap = BitmapFactory.decodeStream(is);

        /*ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, out);
                //from  w  w  w  . j a v a2  s  .c  o m
        return new BitmapDrawable(BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())));*/
        is.close();
        return new BitmapDrawable(bitmap);
    } else {
        return null;
    }
}

From source file:org.jets3t.service.impl.rest.httpclient.RestStorageService.java

/**
 * Beware of high memory requirements when creating large S3 objects when the Content-Length
 * is not set in the object./*w w  w  . ja v  a  2 s.  com*/
 */
@Override
protected StorageObject putObjectImpl(String bucketName, StorageObject object) throws ServiceException {
    if (log.isDebugEnabled()) {
        log.debug("Creating Object with key " + object.getKey() + " in bucket " + bucketName);
    }

    HttpEntity requestEntity = null;

    if (object.getDataInputStream() != null) {
        if (object.containsMetadata(StorageObject.METADATA_HEADER_CONTENT_LENGTH)) {
            if (log.isDebugEnabled()) {
                log.debug("Uploading object data with Content-Length: " + object.getContentLength());
            }
            requestEntity = new RepeatableRequestEntity(object.getKey(), object.getDataInputStream(),
                    object.getContentType(), object.getContentLength(), this.jets3tProperties,
                    isLiveMD5HashingRequired(object));
        } else {
            // Use a BufferedHttpEntity for objects with an unknown content length, as the
            // entity will cache the results and doesn't need to know the data length in advance.
            if (log.isWarnEnabled()) {
                log.warn(
                        "Content-Length of data stream not set, will automatically determine data length in memory");
            }
            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent(object.getDataInputStream());
            try {
                requestEntity = new BufferedHttpEntity(basicHttpEntity);
            } catch (IOException ioe) {
                throw new ServiceException("Unable to read data stream of unknown length", ioe);
            }
        }
    }
    putObjectWithRequestEntityImpl(bucketName, object, requestEntity, null);

    return object;
}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Creates a tunnel to the target server.
 * The connection must be established to the (last) proxy.
 * A CONNECT request for tunnelling through the proxy will
 * be created and sent, the response received and checked.
 * This method does <i>not</i> update the connection with
 * information about the tunnel, that is left to the caller.
 *
 * @param route     the route to establish
 * @param context   the context for request execution
 *
 * @return  <code>true</code> if the tunnelled route is secure,
 *          <code>false</code> otherwise.
 *          The implementation here always returns <code>false</code>,
 *          but derived classes may override.
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 *//* www .ja  v a 2 s  . c  o m*/
protected boolean createTunnelToTarget(final HttpRoute route, final HttpContext context)
        throws HttpException, IOException {

    final HttpHost proxy = route.getProxyHost();
    final HttpHost target = route.getTargetHost();
    HttpResponse response = null;

    for (;;) {
        if (!this.managedConn.isOpen()) {
            this.managedConn.open(route, context, this.params);
        }

        final HttpRequest connect = createConnectRequest(route, context);
        connect.setParams(this.params);

        // Populate the execution context
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
        context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
        context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn);
        context.setAttribute(ExecutionContext.HTTP_REQUEST, connect);

        this.requestExec.preProcess(connect, this.httpProcessor, context);

        response = this.requestExec.execute(connect, this.managedConn, context);

        response.setParams(this.params);
        this.requestExec.postProcess(response, this.httpProcessor, context);

        final int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }

        if (HttpClientParams.isAuthenticating(this.params)) {
            if (this.authenticator.isAuthenticationRequested(proxy, response, this.proxyAuthStrategy,
                    this.proxyAuthState, context)) {
                if (this.authenticator.authenticate(proxy, response, this.proxyAuthStrategy,
                        this.proxyAuthState, context)) {
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        final HttpEntity entity = response.getEntity();
                        EntityUtils.consume(entity);
                    } else {
                        this.managedConn.close();
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }

    final int status = response.getStatusLine().getStatusCode();

    if (status > 299) {

        // Buffer response content
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }

        this.managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }

    this.managedConn.markReusable();

    // How to decide on security of the tunnelled connection?
    // The socket factory knows only about the segment to the proxy.
    // Even if that is secure, the hop to the target may be insecure.
    // Leave it to derived classes, consider insecure by default here.
    return false;

}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * Creates a tunnel to the target server.
 * The connection must be established to the (last) proxy.
 * A CONNECT request for tunnelling through the proxy will
 * be created and sent, the response received and checked.
 * This method does <i>not</i> update the connection with
 * information about the tunnel, that is left to the caller.
 *
 * @param route     the route to establish
 * @param context   the context for request execution
 *
 * @return  {@code true} if the tunnelled route is secure,
 *          {@code false} otherwise./* ww  w  . j a  v a  2 s.  co m*/
 *          The implementation here always returns {@code false},
 *          but derived classes may override.
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
protected boolean createTunnelToTarget(final HttpRoute route, final HttpContext context)
        throws HttpException, IOException {

    final HttpHost proxy = route.getProxyHost();
    final HttpHost target = route.getTargetHost();
    HttpResponse response = null;

    for (;;) {
        if (!this.managedConn.isOpen()) {
            this.managedConn.open(route, context, this.params);
        }

        final HttpRequest connect = createConnectRequest(route, context);
        connect.setParams(this.params);

        // Populate the execution context
        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
        context.setAttribute(ClientContext.ROUTE, route);
        context.setAttribute(ExecutionContext.HTTP_PROXY_HOST, proxy);
        context.setAttribute(ExecutionContext.HTTP_CONNECTION, managedConn);
        context.setAttribute(ExecutionContext.HTTP_REQUEST, connect);

        this.requestExec.preProcess(connect, this.httpProcessor, context);

        response = this.requestExec.execute(connect, this.managedConn, context);

        response.setParams(this.params);
        this.requestExec.postProcess(response, this.httpProcessor, context);

        final int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }

        if (HttpClientParams.isAuthenticating(this.params)) {
            if (this.authenticator.isAuthenticationRequested(proxy, response, this.proxyAuthStrategy,
                    this.proxyAuthState, context)) {
                if (this.authenticator.authenticate(proxy, response, this.proxyAuthStrategy,
                        this.proxyAuthState, context)) {
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        final HttpEntity entity = response.getEntity();
                        EntityUtils.consume(entity);
                    } else {
                        this.managedConn.close();
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }

    final int status = response.getStatusLine().getStatusCode();

    if (status > 299) {

        // Buffer response content
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }

        this.managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }

    this.managedConn.markReusable();

    // How to decide on security of the tunnelled connection?
    // The socket factory knows only about the segment to the proxy.
    // Even if that is secure, the hop to the target may be insecure.
    // Leave it to derived classes, consider insecure by default here.
    return false;

}

From source file:org.apache.http.impl.execchain.MainClientExec.java

/**
 * Creates a tunnel to the target server.
 * The connection must be established to the (last) proxy.
 * A CONNECT request for tunnelling through the proxy will
 * be created and sent, the response received and checked.
 * This method does <i>not</i> update the connection with
 * information about the tunnel, that is left to the caller.
 *//*from w  ww .ja va  2s  . c om*/
private boolean createTunnelToTarget(final AuthState proxyAuthState, final HttpClientConnection managedConn,
        final HttpRoute route, final HttpRequest request, final HttpClientContext context)
        throws HttpException, IOException {

    final RequestConfig config = context.getRequestConfig();
    final int timeout = config.getConnectTimeout();

    final HttpHost target = route.getTargetHost();
    final HttpHost proxy = route.getProxyHost();
    HttpResponse response;

    final String authority = target.toHostString();
    final HttpRequest connect = new BasicHttpRequest("CONNECT", authority, request.getProtocolVersion());

    this.requestExecutor.preProcess(connect, this.proxyHttpProcessor, context);

    for (;;) {
        if (!managedConn.isOpen()) {
            this.connManager.connect(managedConn, route, timeout > 0 ? timeout : 0, context);
        }

        connect.removeHeaders(AUTH.PROXY_AUTH_RESP);
        this.authenticator.generateAuthResponse(connect, proxyAuthState, context);

        response = this.requestExecutor.execute(connect, managedConn, context);

        final int status = response.getStatusLine().getStatusCode();
        if (status < 200) {
            throw new HttpException("Unexpected response to CONNECT request: " + response.getStatusLine());
        }

        if (config.isAuthenticationEnabled()) {
            if (this.authenticator.isAuthenticationRequested(proxy, response, this.proxyAuthStrategy,
                    proxyAuthState, context)) {
                if (this.authenticator.handleAuthChallenge(proxy, response, this.proxyAuthStrategy,
                        proxyAuthState, context)) {
                    // Retry request
                    if (this.reuseStrategy.keepAlive(response, context)) {
                        this.log.debug("Connection kept alive");
                        // Consume response content
                        final HttpEntity entity = response.getEntity();
                        EntityUtils.consume(entity);
                    } else {
                        managedConn.close();
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }

    final int status = response.getStatusLine().getStatusCode();

    if (status > 299) {

        // Buffer response content
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            response.setEntity(new BufferedHttpEntity(entity));
        }

        managedConn.close();
        throw new TunnelRefusedException("CONNECT refused by proxy: " + response.getStatusLine(), response);
    }

    // How to decide on security of the tunnelled connection?
    // The socket factory knows only about the segment to the proxy.
    // Even if that is secure, the hop to the target may be insecure.
    // Leave it to derived classes, consider insecure by default here.
    return false;
}

From source file:org.elasticsearch.client.RequestLogger.java

/**
 * Creates curl output for given response
 *///from  ww  w. j a v a2  s.  c  o  m
static String buildTraceResponse(HttpResponse httpResponse) throws IOException {
    StringBuilder responseLine = new StringBuilder();
    responseLine.append("# ").append(httpResponse.getStatusLine());
    for (Header header : httpResponse.getAllHeaders()) {
        responseLine.append("\n# ").append(header.getName()).append(": ").append(header.getValue());
    }
    responseLine.append("\n#");
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        if (entity.isRepeatable() == false) {
            entity = new BufferedHttpEntity(entity);
        }
        httpResponse.setEntity(entity);
        ContentType contentType = ContentType.get(entity);
        Charset charset = StandardCharsets.UTF_8;
        if (contentType != null && contentType.getCharset() != null) {
            charset = contentType.getCharset();
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset))) {
            String line;
            while ((line = reader.readLine()) != null) {
                responseLine.append("\n# ").append(line);
            }
        }
    }
    return responseLine.toString();
}

From source file:xin.nic.sdk.registrar.util.HttpUtil.java

/**
 * ?Entity?//w w w . ja v  a 2 s.  co m
 * 
 * @param httpEntity
 * @return
 * @throws Exception
 */
public static byte[] getData(HttpEntity httpEntity) throws Exception {
    BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bufferedHttpEntity.writeTo(byteArrayOutputStream);
    byte[] responseBytes = byteArrayOutputStream.toByteArray();
    return responseBytes;
}