@Override public void setEntity(final HttpEntity entity) { original.setEntity(entity); }
public static void enchance(final HttpResponse response, final ConnectionHolder connHolder) { final HttpEntity entity = response.getEntity(); if (entity != null && entity.isStreaming() && connHolder != null) { response.setEntity(new ResponseEntityProxy(entity, connHolder)); } }
public void receiveResponseEntity(final HttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); assertOpen(); final HttpEntity entity = this.entitydeserializer.deserialize(this.inbuffer, response); response.setEntity(entity); }
@Override public void receiveResponseEntity( final HttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); ensureOpen(); final HttpEntity entity = prepareInput(response); response.setEntity(entity); }
/** * Updates an entity in a response by first consuming an existing entity, then setting the new one. * * @param response the response with an entity to update; must not be null. * @param entity the entity to set in the response. * @throws IOException if an error occurs while reading the input stream on the existing * entity. * @throws IllegalArgumentException if response is null. * * @since 4.3 */ public static void updateEntity( final HttpResponse response, final HttpEntity entity) throws IOException { Args.notNull(response, "Response"); consume(response.getEntity()); response.setEntity(entity); }
CloseableHttpResponse generateIncompleteResponseError( final HttpResponse response, final Resource resource) { final Integer contentLength = Integer.valueOf(response.getFirstHeader(HTTP.CONTENT_LEN).getValue()); final HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_GATEWAY, "Bad Gateway"); error.setHeader("Content-Type","text/plain;charset=UTF-8"); final String msg = String.format("Received incomplete response " + "with Content-Length %d but actual body length %d", contentLength, resource.length()); final byte[] msgBytes = msg.getBytes(); error.setHeader("Content-Length", Integer.toString(msgBytes.length)); error.setEntity(new ByteArrayEntity(msgBytes)); return Proxies.enhanceResponse(error); }
CloseableHttpResponse getReconstructedResponse() throws IOException { ensureConsumed(); final HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine()); reconstructed.setHeaders(response.getAllHeaders()); final CombinedEntity combinedEntity = new CombinedEntity(resource, instream); final HttpEntity entity = response.getEntity(); if (entity != null) { combinedEntity.setContentType(entity.getContentType()); combinedEntity.setContentEncoding(entity.getContentEncoding()); combinedEntity.setChunked(entity.isChunked()); } reconstructed.setEntity(combinedEntity); return (CloseableHttpResponse) Proxy.newProxyInstance( ResponseProxyHandler.class.getClassLoader(), new Class<?>[] { CloseableHttpResponse.class }, new ResponseProxyHandler(reconstructed) { @Override public void close() throws IOException { response.close(); } }); }
/** * Handles the given exception and generates an HTTP response to be sent * back to the client to inform about the exceptional condition encountered * in the course of the request processing. * * @param ex the exception. * @param response the HTTP response. */ protected void handleException(final HttpException ex, final HttpResponse response) { if (ex instanceof MethodNotSupportedException) { response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED); } else if (ex instanceof UnsupportedHttpVersionException) { response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED); } else if (ex instanceof ProtocolException) { response.setStatusCode(HttpStatus.SC_BAD_REQUEST); } else { response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); } String message = ex.getMessage(); if (message == null) { message = ex.toString(); } final byte[] msg = EncodingUtils.getAsciiBytes(message); final ByteArrayEntity entity = new ByteArrayEntity(msg); entity.setContentType("text/plain; charset=US-ASCII"); response.setEntity(entity); }
/** * If I was able to use a {@link CacheEntity} to response to the {@link cz.msebera.android.httpclient.HttpRequest} then * generate an {@link HttpResponse} based on the cache entry. * @param request {@link HttpRequestWrapper} to generate the response for * @param entry {@link CacheEntity} to transform into an {@link HttpResponse} * @return {@link HttpResponse} that was constructed */ CloseableHttpResponse generateResponse(final HttpRequestWrapper request, final HttpCacheEntry entry) { final Date now = new Date(); final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, entry .getStatusCode(), entry.getReasonPhrase()); response.setHeaders(entry.getAllHeaders()); if (responseShouldContainEntity(request, entry)) { final HttpEntity entity = new CacheEntity(entry); addMissingContentLengthHeader(response, entity); response.setEntity(entity); } final long age = this.validityStrategy.getCurrentAgeSecs(entry, now); if (age > 0) { if (age >= Integer.MAX_VALUE) { response.setHeader(HeaderConstants.AGE, "2147483648"); } else { response.setHeader(HeaderConstants.AGE, "" + ((int) age)); } } return Proxies.enhanceResponse(response); }
/** * When we get a response from a down stream server (Origin Server) * we attempt to see if it is HTTP 1.1 Compliant and if not, attempt to * make it so. * * @param request The {@link HttpRequest} that generated an origin hit and response * @param response The {@link HttpResponse} from the origin server * @throws IOException Bad things happened */ public void ensureProtocolCompliance(final HttpRequestWrapper request, final HttpResponse response) throws IOException { if (backendResponseMustNotHaveBody(request, response)) { consumeBody(response); response.setEntity(null); } requestDidNotExpect100ContinueButResponseIsOne(request, response); transferEncodingIsNotReturnedTo1_0Client(request, response); ensurePartialContentIsNotSentToAClientThatDidNotRequestIt(request, response); ensure200ForOPTIONSRequestWithNoBodyHasContentLengthZero(request, response); ensure206ContainsDateHeader(response); ensure304DoesNotContainExtraEntityHeaders(response); identityIsNotUsedInContentEncoding(response); warningsWithNonMatchingWarnDatesAreRemoved(response); }
@Override public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final HttpClientContext clientContext = HttpClientContext.adapt(context); final RequestConfig requestConfig = clientContext.getRequestConfig(); // entity can be null in case of 304 Not Modified, 204 No Content or similar // check for zero length entity. if (requestConfig.isDecompressionEnabled() && entity != null && entity.getContentLength() != 0) { final Header ceheader = entity.getContentEncoding(); if (ceheader != null) { final HeaderElement[] codecs = ceheader.getElements(); for (final HeaderElement codec : codecs) { final String codecname = codec.getName().toLowerCase(Locale.ROOT); final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname); if (decoderFactory != null) { response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory)); response.removeHeaders("Content-Length"); response.removeHeaders("Content-Encoding"); response.removeHeaders("Content-MD5"); } else { if (!"identity".equals(codecname)) { throw new HttpException("Unsupported Content-Coding: " + codec.getName()); } } } } } }
response.setEntity(entity);
response.setEntity(new BufferedHttpEntity(entity));
response.setEntity(new BufferedHttpEntity(entity));
response.setEntity(new BufferedHttpEntity(entity));