Java Code Examples for org.apache.http.HttpEntity#getContentEncoding()
The following examples show how to use
org.apache.http.HttpEntity#getContentEncoding() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: fanfouapp-opensource File: GzipResponseInterceptor.java License: Apache License 2.0 | 6 votes |
@Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (final HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase( GzipResponseInterceptor.ENCODING_GZIP)) { response.setEntity(new GzipDecompressingEntity(response .getEntity())); break; } } } }
Example 2
Source Project: cloud-connectivityproxy File: ProxyServlet.java License: Apache License 2.0 | 6 votes |
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); } } } } }
Example 3
Source Project: lucene-solr File: HttpClientUtil.java License: Apache License 2.0 | 6 votes |
@Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response .setEntity(new GzipDecompressingEntity(response.getEntity())); return; } if (codecs[i].getName().equalsIgnoreCase("deflate")) { response.setEntity(new DeflateDecompressingEntity(response .getEntity())); return; } } } }
Example 4
Source Project: p4ic4idea File: BasicResponse.java License: Apache License 2.0 | 6 votes |
private static Charset getEncoding(HttpEntity entity) { Header header = entity.getContentEncoding(); if (header == null) { return Charset.forName("UTF-8"); } for (HeaderElement headerElement : header.getElements()) { for (NameValuePair pair : headerElement.getParameters()) { if (pair != null && pair.getValue() != null) { if (Charset.isSupported(pair.getValue())) { return Charset.forName(pair.getValue()); } } } } return Charset.forName("UTF-8"); }
Example 5
Source Project: netcdf-java File: HTTPSession.java License: BSD 3-Clause "New" or "Revised" License | 5 votes |
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement h : codecs) { if (h.getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }
Example 6
Source Project: PYX-Reloaded File: GoogleApacheHttpResponse.java License: Apache License 2.0 | 5 votes |
@Override public String getContentEncoding() { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncodingHeader = entity.getContentEncoding(); if (contentEncodingHeader != null) { return contentEncodingHeader.getValue(); } } return null; }
Example 7
Source Project: confluence-publisher File: RequestFailedException.java License: Apache License 2.0 | 5 votes |
private static String entityAsString(HttpEntity entity) { try { InputStream content = entity.getContent(); Charset encoding = entity.getContentEncoding() == null ? defaultCharset() : Charset.forName(entity.getContentEncoding().getValue()); String contentString = inputStreamAsString(content, encoding); return contentString; } catch (Exception ignored) { return "<empty body>"; } }
Example 8
Source Project: beam File: InfluxDBPublisher.java License: Apache License 2.0 | 5 votes |
private static String getErrorMessage(final HttpEntity entity) throws IOException { final Header encodingHeader = entity.getContentEncoding(); final Charset encoding = encodingHeader == null ? StandardCharsets.UTF_8 : Charsets.toCharset(encodingHeader.getValue()); final JsonElement errorElement = new Gson().fromJson(EntityUtils.toString(entity, encoding), JsonObject.class).get("error"); return isNull(errorElement) ? "[Unable to get error message]" : errorElement.toString(); }
Example 9
Source Project: JMCCC File: HttpAsyncDownloader.java License: MIT License | 5 votes |
@Override protected void onResponseReceived(HttpResponse response) throws HttpException, IOException { StatusLine statusLine = response.getStatusLine(); if (statusLine != null) { int statusCode = statusLine.getStatusCode(); if (statusCode < 200 || statusCode > 299) // non-2xx response code throw new IllegalHttpResponseCodeException(statusLine.toString(), statusCode); } if (session == null) { boolean gzipOn = false; HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { long contextLength = httpEntity.getContentLength(); if (contextLength >= 0) { this.contextLength = contextLength; } Header contentEncodingHeader = httpEntity.getContentEncoding(); if (contentEncodingHeader != null && "gzip".equals(contentEncodingHeader.getValue())) { gzipOn = true; } } session = contextLength > 0 ? task.createSession(contextLength) : task.createSession(); if (gzipOn) { session = new GzipDownloadSession<>(session); } } }
Example 10
Source Project: google-http-java-client File: ApacheHttpResponse.java License: Apache License 2.0 | 5 votes |
@Override public String getContentEncoding() { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncodingHeader = entity.getContentEncoding(); if (contentEncodingHeader != null) { return contentEncodingHeader.getValue(); } } return null; }
Example 11
Source Project: google-http-java-client File: ApacheHttpResponse.java License: Apache License 2.0 | 5 votes |
@Override public String getContentEncoding() { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncodingHeader = entity.getContentEncoding(); if (contentEncodingHeader != null) { return contentEncodingHeader.getValue(); } } return null; }
Example 12
Source Project: volley File: HttpClientStack.java License: Apache License 2.0 | 5 votes |
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders); // add gzip support, not all request need gzip support if (request.isShouldGzip()) { httpRequest.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } addHeaders(httpRequest, additionalHeaders); addHeaders(httpRequest, request.getHeaders()); onPrepareRequest(httpRequest); HttpParams httpParams = httpRequest.getParams(); int timeoutMs = request.getTimeoutMs(); // TODO: Reevaluate this connection timeout based on more wide-scale // data collection and possibly different for wifi vs. 3G. HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIME_OUT_MS); HttpConnectionParams.setSoTimeout(httpParams, timeoutMs); HttpResponse response = mClient.execute(httpRequest); if (response != null) { final HttpEntity entity = response.getEntity(); if (entity == null) { return response; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } return response; }
Example 13
Source Project: jbosh File: ApacheHTTPResponse.java License: Apache License 2.0 | 5 votes |
/** * Await the response, storing the result in the instance variables of * this class when they arrive. * * @throws InterruptedException if interrupted while awaiting the response * @throws BOSHException on communication failure */ private synchronized void awaitResponse() throws BOSHException { HttpEntity entity = null; try { HttpResponse httpResp = client.execute(post, context); entity = httpResp.getEntity(); byte[] data = EntityUtils.toByteArray(entity); String encoding = entity.getContentEncoding() != null ? entity.getContentEncoding().getValue() : null; if (ZLIBCodec.getID().equalsIgnoreCase(encoding)) { data = ZLIBCodec.decode(data); } else if (GZIPCodec.getID().equalsIgnoreCase(encoding)) { data = GZIPCodec.decode(data); } body = StaticBody.fromString(new String(data, CHARSET)); statusCode = httpResp.getStatusLine().getStatusCode(); sent = true; } catch (IOException iox) { abort(); toThrow = new BOSHException("Could not obtain response", iox); throw(toThrow); } catch (RuntimeException ex) { abort(); throw(ex); } }
Example 14
Source Project: esigate File: ResponseSender.java License: Apache License 2.0 | 5 votes |
void sendHeaders(HttpResponse httpResponse, IncomingRequest httpRequest, HttpServletResponse response) { response.setStatus(httpResponse.getStatusLine().getStatusCode()); for (Header header : httpResponse.getAllHeaders()) { String name = header.getName(); String value = header.getValue(); response.addHeader(name, value); } // Copy new cookies Cookie[] newCookies = httpRequest.getNewCookies(); for (Cookie newCooky : newCookies) { // newCooky may be null. In that case just ignore. // See https://github.com/esigate/esigate/issues/181 if (newCooky != null) { response.addHeader("Set-Cookie", CookieUtil.encodeCookie(newCooky)); } } HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { long contentLength = httpEntity.getContentLength(); if (contentLength > -1 && contentLength < Integer.MAX_VALUE) { response.setContentLength((int) contentLength); } Header contentType = httpEntity.getContentType(); if (contentType != null) { response.setContentType(contentType.getValue()); } Header contentEncoding = httpEntity.getContentEncoding(); if (contentEncoding != null) { response.setHeader(contentEncoding.getName(), contentEncoding.getValue()); } } }
Example 15
Source Project: YiBo File: GzipResponseInterceptor.java License: Apache License 2.0 | 5 votes |
@Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } }
Example 16
Source Project: cyberduck File: HttpComponentsConnector.java License: GNU General Public License v3.0 | 4 votes |
@Override public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException { final HttpUriRequest request = this.toUriHttpRequest(clientRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request); try { final CloseableHttpResponse response; response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context)); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName()); final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); final ClientResponse responseContext = new ClientResponse(status, clientRequest); final List<URI> redirectLocations = context.getRedirectLocations(); if(redirectLocations != null && !redirectLocations.isEmpty()) { responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1)); } final Header[] respHeaders = response.getAllHeaders(); final MultivaluedMap<String, String> headers = responseContext.getHeaders(); for(final Header header : respHeaders) { final String headerName = header.getName(); List<String> list = headers.get(headerName); if(list == null) { list = new ArrayList<>(); } list.add(header.getValue()); headers.put(headerName, list); } final HttpEntity entity = response.getEntity(); if(entity != null) { if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) { headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength())); } final Header contentEncoding = entity.getContentEncoding(); if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) { headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue()); } } responseContext.setEntityStream(this.toInputStream(response)); return responseContext; } catch(final Exception e) { throw new ProcessingException(e); } }
Example 17
Source Project: cyberduck File: HttpComponentsConnector.java License: GNU General Public License v3.0 | 4 votes |
@Override public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException { final HttpUriRequest request = this.toUriHttpRequest(clientRequest); final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request); try { final CloseableHttpResponse response; response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context)); HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName()); final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); final ClientResponse responseContext = new ClientResponse(status, clientRequest); final List<URI> redirectLocations = context.getRedirectLocations(); if(redirectLocations != null && !redirectLocations.isEmpty()) { responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1)); } final Header[] respHeaders = response.getAllHeaders(); final MultivaluedMap<String, String> headers = responseContext.getHeaders(); for(final Header header : respHeaders) { final String headerName = header.getName(); List<String> list = headers.get(headerName); if(list == null) { list = new ArrayList<>(); } list.add(header.getValue()); headers.put(headerName, list); } final HttpEntity entity = response.getEntity(); if(entity != null) { if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) { headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength())); } final Header contentEncoding = entity.getContentEncoding(); if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) { headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue()); } } responseContext.setEntityStream(this.toInputStream(response)); return responseContext; } catch(final Exception e) { throw new ProcessingException(e); } }
Example 18
Source Project: uavstack File: ApacheHttpClientAdapter.java License: Apache License 2.0 | 4 votes |
@Override public void afterDoCap(InvokeChainContext context, Object[] args) { if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) { Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY); SlowOperContext slowOperContext = new SlowOperContext(); if (Throwable.class.isAssignableFrom(args[0].getClass())) { Throwable e = (Throwable) args[0]; slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString()); } else { HttpResponse response = (HttpResponse) args[0]; slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response)); HttpEntity entity = response.getEntity(); if (entity != null) { // 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断 if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) { Header header = entity.getContentEncoding(); String encoding = header == null ? "utf-8" : header.getValue(); slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(entity, encoding)); } else { slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory"); } } else { slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, ""); } } Object params[] = { span, slowOperContext }; UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params); } }
Example 19
Source Project: gatk File: HtsgetReader.java License: BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Object doWork() { // construct request from command line args and convert to URI final HtsgetRequestBuilder req = new HtsgetRequestBuilder(endpoint, id) .withFormat(format) .withDataClass(dataClass) .withInterval(interval) .withFields(fields) .withTags(tags) .withNotags(notags); final URI reqURI = req.toURI(); final HttpGet getReq = new HttpGet(reqURI); try (final CloseableHttpResponse resp = this.client.execute(getReq)) { // get content of response final HttpEntity entity = resp.getEntity(); final Header encodingHeader = entity.getContentEncoding(); final Charset encoding = encodingHeader == null ? StandardCharsets.UTF_8 : Charsets.toCharset(encodingHeader.getValue()); final String jsonBody = EntityUtils.toString(entity, encoding); final ObjectMapper mapper = this.getObjectMapper(); if (resp.getStatusLine() == null) { throw new UserException("htsget server response did not contain status line"); } final int statusCode = resp.getStatusLine().getStatusCode(); if (400 <= statusCode && statusCode < 500) { final HtsgetErrorResponse err = mapper.readValue(jsonBody, HtsgetErrorResponse.class); throw new UserException("Invalid request, received error code: " + statusCode + ", error type: " + err.getError() + ", message: " + err.getMessage()); } else if (statusCode == 200) { final HtsgetResponse response = mapper.readValue(jsonBody, HtsgetResponse.class); if (this.readerThreads > 1) { this.getDataParallel(response); } else { this.getData(response); } logger.info("Successfully wrote to: " + outputFile); if (checkMd5) { this.checkMd5(response); } } else { throw new UserException("Unrecognized status code: " + statusCode); } } catch (final IOException e) { throw new UserException("IOException during htsget download", e); } return null; }
Example 20
Source Project: Crawer File: HttpConnnectionManager.java License: MIT License | 2 votes |
/** * * 从response返回的实体中读取页面代码 * * @param httpEntity * Http实体 * * @return 页面代码 * * @throws ParseException * * @throws IOException */ private static String readHtmlContentFromEntity(HttpEntity httpEntity) throws ParseException, IOException { String html = ""; Header header = httpEntity.getContentEncoding(); if (httpEntity.getContentLength() < 2147483647L) { // EntityUtils无法处理ContentLength超过2147483647L的Entity if (header != null && "gzip".equals(header.getValue())) { html = EntityUtils.toString(new GzipDecompressingEntity( httpEntity)); } else { html = EntityUtils.toString(httpEntity); } } else { InputStream in = httpEntity.getContent(); if (header != null && "gzip".equals(header.getValue())) { html = unZip(in, ContentType.getOrDefault(httpEntity) .getCharset().toString()); } else { html = readInStreamToString(in, ContentType.getOrDefault(httpEntity).getCharset() .toString()); } if (in != null) { in.close(); } } return html; }