View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.lang.reflect.Proxy;
32  
33  import org.apache.http.HttpEntity;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.client.cache.InputLimit;
37  import org.apache.http.client.cache.Resource;
38  import org.apache.http.client.cache.ResourceFactory;
39  import org.apache.http.client.methods.CloseableHttpResponse;
40  import org.apache.http.message.BasicHttpResponse;
41  
42  /**
43   * @since 4.1
44   */
45  class SizeLimitedResponseReader {
46  
47      private final ResourceFactory resourceFactory;
48      private final long maxResponseSizeBytes;
49      private final HttpRequest request;
50      private final CloseableHttpResponse response;
51  
52      private InputStream instream;
53      private InputLimit limit;
54      private Resource resource;
55      private boolean consumed;
56  
57      /**
58       * Create an {@link HttpResponse} that is limited in size, this allows for checking
59       * the size of objects that will be stored in the cache.
60       */
61      public SizeLimitedResponseReader(
62              final ResourceFactory resourceFactory,
63              final long maxResponseSizeBytes,
64              final HttpRequest request,
65              final CloseableHttpResponse response) {
66          super();
67          this.resourceFactory = resourceFactory;
68          this.maxResponseSizeBytes = maxResponseSizeBytes;
69          this.request = request;
70          this.response = response;
71      }
72  
73      protected void readResponse() throws IOException {
74          if (!consumed) {
75              doConsume();
76          }
77      }
78  
79      private void ensureNotConsumed() {
80          if (consumed) {
81              throw new IllegalStateException("Response has already been consumed");
82          }
83      }
84  
85      private void ensureConsumed() {
86          if (!consumed) {
87              throw new IllegalStateException("Response has not been consumed");
88          }
89      }
90  
91      private void doConsume() throws IOException {
92          ensureNotConsumed();
93          consumed = true;
94  
95          limit = new InputLimit(maxResponseSizeBytes);
96  
97          final HttpEntity entity = response.getEntity();
98          if (entity == null) {
99              return;
100         }
101         final String uri = request.getRequestLine().getUri();
102         instream = entity.getContent();
103         try {
104             resource = resourceFactory.generate(uri, instream, limit);
105         } finally {
106             if (!limit.isReached()) {
107                 instream.close();
108             }
109         }
110     }
111 
112     boolean isLimitReached() {
113         ensureConsumed();
114         return limit.isReached();
115     }
116 
117     Resource getResource() {
118         ensureConsumed();
119         return resource;
120     }
121 
122     CloseableHttpResponse getReconstructedResponse() throws IOException {
123         ensureConsumed();
124         final HttpResponse reconstructed = new BasicHttpResponse(response.getStatusLine());
125         reconstructed.setHeaders(response.getAllHeaders());
126 
127         final CombinedEntity combinedEntity = new CombinedEntity(resource, instream);
128         final HttpEntity entity = response.getEntity();
129         if (entity != null) {
130             combinedEntity.setContentType(entity.getContentType());
131             combinedEntity.setContentEncoding(entity.getContentEncoding());
132             combinedEntity.setChunked(entity.isChunked());
133         }
134         reconstructed.setEntity(combinedEntity);
135         return (CloseableHttpResponse) Proxy.newProxyInstance(
136                 ResponseProxyHandler.class.getClassLoader(),
137                 new Class<?>[] { CloseableHttpResponse.class },
138                 new ResponseProxyHandler(reconstructed) {
139 
140                     @Override
141                     public void close() throws IOException {
142                         response.close();
143                     }
144 
145                 });
146     }
147 
148 }