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.client.fluent;
28  
29  import java.io.File;
30  import java.io.FileOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  import org.apache.http.HttpEntity;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.StatusLine;
37  import org.apache.http.client.ClientProtocolException;
38  import org.apache.http.client.HttpResponseException;
39  import org.apache.http.client.ResponseHandler;
40  import org.apache.http.entity.ByteArrayEntity;
41  import org.apache.http.entity.ContentType;
42  import org.apache.http.util.EntityUtils;
43  
44  public class Response {
45  
46      private final HttpResponse response;
47      private boolean consumed;
48  
49      Response(final HttpResponse response) {
50          super();
51          this.response = response;
52      }
53  
54      private void assertNotConsumed() {
55          if (this.consumed) {
56              throw new IllegalStateException("Response content has been already consumed");
57          }
58      }
59  
60      private void dispose() {
61          if (this.consumed) {
62              return;
63          }
64          try {
65              final HttpEntity entity = this.response.getEntity();
66              final InputStream content = entity.getContent();
67              if (content != null) {
68                  content.close();
69              }
70          } catch (final Exception ignore) {
71          } finally {
72              this.consumed = true;
73          }
74      }
75  
76      /**
77       * Discards response content and deallocates all resources associated with it.
78       */
79      public void discardContent() {
80          dispose();
81      }
82  
83      /**
84       * Handles the response using the specified {@link ResponseHandler}
85       */
86      public <T> T handleResponse(
87              final ResponseHandler<T> handler) throws ClientProtocolException, IOException {
88          assertNotConsumed();
89          try {
90              return handler.handleResponse(this.response);
91          } finally {
92              dispose();
93          }
94      }
95  
96      public Content returnContent() throws ClientProtocolException, IOException {
97          return handleResponse(new ContentResponseHandler());
98      }
99  
100     public HttpResponse returnResponse() throws IOException {
101         assertNotConsumed();
102         try {
103             final HttpEntity entity = this.response.getEntity();
104             if (entity != null) {
105                 final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
106                         EntityUtils.toByteArray(entity));
107                 final ContentType contentType = ContentType.getOrDefault(entity);
108                 byteArrayEntity.setContentType(contentType.toString());
109                 this.response.setEntity(byteArrayEntity);
110             }
111             return this.response;
112         } finally {
113             this.consumed = true;
114         }
115     }
116 
117     public void saveContent(final File file) throws IOException {
118         assertNotConsumed();
119         final StatusLine statusLine = response.getStatusLine();
120         if (statusLine.getStatusCode() >= 300) {
121             throw new HttpResponseException(statusLine.getStatusCode(),
122                     statusLine.getReasonPhrase());
123         }
124         final FileOutputStream out = new FileOutputStream(file);
125         try {
126             final HttpEntity entity = this.response.getEntity();
127             if (entity != null) {
128                 entity.writeTo(out);
129             }
130         } finally {
131             this.consumed = true;
132             out.close();
133         }
134     }
135 
136 }