List of usage examples for org.apache.http.client.entity DeflateDecompressingEntity DeflateDecompressingEntity
public DeflateDecompressingEntity(final HttpEntity entity)
From source file:au.com.borner.salesforce.client.rest.HttpCompressionResponseInterceptor.java
@Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) { HeaderElement[] codecs = contentEncoding.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase(GZIP)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; }//from w w w . ja va2 s. c o m if (codec.getName().equalsIgnoreCase(DEFLATE)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } } } } }
From source file:org.sonatype.nexus.plugins.crowd.client.rest.CompressedHttpResponseInterceptor.java
@Override public void process(HttpResponse response, HttpContext hc) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header header = entity.getContentEncoding(); if (header != null) { for (HeaderElement headerElement : header.getElements()) { if (headerElement.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(entity)); return; }/*from w ww . j a va 2 s .c o m*/ if (headerElement.getName().equalsIgnoreCase("deflate")) { response.setEntity(new DeflateDecompressingEntity(entity)); return; } } } } }
From source file:org.esigate.http.HttpResponseUtils.java
/** * Returns the response body as a string or the reason phrase if body is empty. * <p>//from ww w . jav a2 s . co m * This methods is similar to EntityUtils#toString() internally, but uncompress the entity first if necessary. * <p> * This methods also holds an extension point, which can be used to guess the real encoding of the entity, if the * HTTP headers set a wrong encoding declaration. * * @since 3.0 * @since 4.1 - Event EventManager.EVENT_READ_ENTITY is fired when calling this method. * * @param httpResponse * @param eventManager * @return The body as string or the reason phrase if body was empty. * @throws HttpErrorPage */ public static String toString(HttpResponse httpResponse, EventManager eventManager) throws HttpErrorPage { HttpEntity httpEntity = httpResponse.getEntity(); String result; if (httpEntity == null) { result = httpResponse.getStatusLine().getReasonPhrase(); } else { // Unzip the stream if necessary Header contentEncoding = httpEntity.getContentEncoding(); if (contentEncoding != null) { String contentEncodingValue = contentEncoding.getValue(); if ("gzip".equalsIgnoreCase(contentEncodingValue) || "x-gzip".equalsIgnoreCase(contentEncodingValue)) { httpEntity = new GzipDecompressingEntity(httpEntity); } else if ("deflate".equalsIgnoreCase(contentEncodingValue)) { httpEntity = new DeflateDecompressingEntity(httpEntity); } else { throw new UnsupportedContentEncodingException( "Content-encoding \"" + contentEncoding + "\" is not supported"); } } try { byte[] rawEntityContent = EntityUtils.toByteArray(httpEntity); ContentType contentType = null; Charset charset = null; String mimeType = null; try { contentType = ContentType.getOrDefault(httpEntity); mimeType = contentType.getMimeType(); charset = contentType.getCharset(); } catch (UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } // Use default charset is no valid information found from HTTP // headers if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } ReadEntityEvent event = new ReadEntityEvent(mimeType, charset, rawEntityContent); // Read using charset based on HTTP headers event.setEntityContent(new String(rawEntityContent, charset)); // Allow extensions to detect document encoding if (eventManager != null) { eventManager.fire(EventManager.EVENT_READ_ENTITY, event); } return event.getEntityContent(); } catch (IOException e) { throw new HttpErrorPage(HttpErrorPage.generateHttpResponse(e)); } } return removeSessionId(result, httpResponse); }
From source file:simple.crawler.http.HttpClientFactory.java
public static DefaultHttpClient createNewDefaultHttpClient() { ////from w w w .j ava 2s.c o m HttpParams params = new BasicHttpParams(); //Determines the connection timeout HttpConnectionParams.setConnectionTimeout(params, 1 * 60 * 1000); //Determines the socket timeout HttpConnectionParams.setSoTimeout(params, 1 * 60 * 1000); //Determines whether stale connection check is to be used HttpConnectionParams.setStaleCheckingEnabled(params, false); //The Nagle's algorithm tries to conserve bandwidth by minimizing the number of segments that are sent. //When application wish to decrease network latency and increase performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY) //Data will be sent earlier, at the cost of an increase in bandwidth consumption HttpConnectionParams.setTcpNoDelay(params, true); // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4"); //Create and initialize scheme registry SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); PoolingClientConnectionManager pm = new PoolingClientConnectionManager(schemeRegistry); // DefaultHttpClient httpclient = new DefaultHttpClient(pm, params); //ConnManagerParams.setMaxTotalConnections(params, MAX_HTTP_CONNECTION); //ConnManagerParams.setMaxConnectionsPerRoute(params, defaultConnPerRoute); //ConnManagerParams.setTimeout(params, 1 * 60 * 1000); httpclient.getParams().setParameter("http.conn-manager.max-total", MAX_HTTP_CONNECTION); ConnPerRoute defaultConnPerRoute = new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 4; } }; httpclient.getParams().setParameter("http.conn-manager.max-per-route", defaultConnPerRoute); httpclient.getParams().setParameter("http.conn-manager.timeout", 1 * 60 * 1000L); httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true); httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH); // HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount > 2) { return false; } if (exception instanceof NoHttpResponseException) { return true; } if (exception instanceof SSLHandshakeException) { return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }; httpclient.setHttpRequestRetryHandler(retryHandler); HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() { public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip, deflate"); } } }; HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() { public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header header = entity.getContentEncoding(); if (header != null) { HeaderElement[] codecs = header.getElements(); for (int i = 0; i < codecs.length; i++) { String codecName = codecs[i].getName(); if ("gzip".equalsIgnoreCase(codecName)) { response.setEntity(new GzipDecompressingEntity(entity)); return; } else if ("deflate".equalsIgnoreCase(codecName)) { response.setEntity(new DeflateDecompressingEntity(entity)); return; } } } } }; httpclient.addRequestInterceptor(requestInterceptor); httpclient.addResponseInterceptor(responseInterceptor); httpclient.setRedirectStrategy(new DefaultRedirectStrategy()); return httpclient; }
From source file:com.sap.cloudlabs.connectivity.proxy.ProxyServlet.java
private void handleContentEncoding(HttpResponse response) throws ServletException { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncodingHeader = entity.getContentEncoding(); if (contentEncodingHeader != null) { HeaderElement[] codecs = contentEncodingHeader.getElements(); LOGGER.debug("Content-Encoding in response:"); for (HeaderElement codec : codecs) { String codecname = codec.getName().toLowerCase(); LOGGER.debug(" => codec: " + codecname); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } else if ("identity".equals(codecname)) { return; } else { throw new ServletException("Unsupported Content-Encoding: " + codecname); }/*from w w w. j a va 2s.c om*/ } } } }