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.hc.client5.http.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.hc.client5.http.ClientProtocolException;
35  import org.apache.hc.client5.http.HttpResponseException;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.HttpEntity;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.HttpResponse;
41  import org.apache.hc.core5.http.HttpStatus;
42  import org.apache.hc.core5.http.io.HttpClientResponseHandler;
43  import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
44  import org.apache.hc.core5.http.io.entity.EntityUtils;
45  import org.apache.hc.core5.http.io.support.ClassicResponseBuilder;
46  
47  /**
48   * HTTP response used by the fluent facade.
49   *
50   * @since 4.2
51   */
52  public class Response {
53  
54      private final ClassicHttpResponse response;
55      private boolean consumed;
56  
57      Response(final ClassicHttpResponse response) {
58          super();
59          this.response = response;
60      }
61  
62      private void assertNotConsumed() {
63          if (this.consumed) {
64              throw new IllegalStateException("Response content has been already consumed");
65          }
66      }
67  
68      private void dispose() throws IOException {
69          if (this.consumed) {
70              return;
71          }
72          try {
73              final HttpEntity entity = this.response.getEntity();
74              if (entity != null) {
75                  final InputStream content = entity.getContent();
76                  if (content != null) {
77                      content.close();
78                  }
79              }
80          } finally {
81              this.consumed = true;
82              this.response.close();
83          }
84      }
85  
86      /**
87       * Discards response content and deallocates all resources associated with it.
88       */
89      public void discardContent() {
90          try {
91              dispose();
92          } catch (final Exception ignore) {
93          }
94      }
95  
96      /**
97       * Handles the response using the specified {@link HttpClientResponseHandler}
98       */
99      public <T> T handleResponse(final HttpClientResponseHandler<T> handler) throws IOException {
100         assertNotConsumed();
101         try {
102             return handler.handleResponse(this.response);
103         } catch (final HttpException ex) {
104             throw new ClientProtocolException(ex);
105         } finally {
106             dispose();
107         }
108     }
109 
110     public Content returnContent() throws IOException {
111         return handleResponse(new ContentResponseHandler());
112     }
113 
114     public HttpResponse returnResponse() throws IOException {
115         assertNotConsumed();
116         try {
117             final HttpEntity entity = this.response.getEntity();
118             return ClassicResponseBuilder.copy(response)
119                     .setEntity(entity != null ?
120                             new ByteArrayEntity(
121                                     EntityUtils.toByteArray(entity),
122                                     ContentType.parse(entity.getContentType()))
123                             : null)
124                     .build();
125         } finally {
126             this.consumed = true;
127             this.response.close();
128         }
129     }
130 
131     public void saveContent(final File file) throws IOException {
132         assertNotConsumed();
133         try {
134             final int status = response.getCode();
135             if (status >= HttpStatus.SC_REDIRECTION) {
136                 throw new HttpResponseException(status, response.getReasonPhrase());
137             }
138             try (FileOutputStream out = new FileOutputStream(file)) {
139                 final HttpEntity entity = this.response.getEntity();
140                 if (entity != null) {
141                     entity.writeTo(out);
142                 }
143             }
144         } finally {
145             this.consumed = true;
146             this.response.close();
147         }
148     }
149 
150 }