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