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