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              final InputStream content = entity.getContent();
74              if (content != null) {
75                  content.close();
76              }
77          } catch (final Exception ignore) {
78          } finally {
79              this.consumed = true;
80          }
81      }
82  
83      /**
84       * Discards response content and deallocates all resources associated with it.
85       */
86      public void discardContent() {
87          dispose();
88      }
89  
90      /**
91       * Handles the response using the specified {@link HttpClientResponseHandler}
92       */
93      public <T> T handleResponse(final HttpClientResponseHandler<T> handler) throws IOException {
94          assertNotConsumed();
95          try {
96              return handler.handleResponse(this.response);
97          } catch (final HttpException ex) {
98              throw new ClientProtocolException(ex);
99          } finally {
100             dispose();
101         }
102     }
103 
104     public Content returnContent() throws IOException {
105         return handleResponse(new ContentResponseHandler());
106     }
107 
108     public HttpResponse returnResponse() throws IOException {
109         assertNotConsumed();
110         try {
111             final HttpEntity entity = this.response.getEntity();
112             if (entity != null) {
113                 final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
114                         EntityUtils.toByteArray(entity), ContentType.parse(entity.getContentType()));
115                 this.response.setEntity(byteArrayEntity);
116             }
117             return this.response;
118         } finally {
119             this.consumed = true;
120         }
121     }
122 
123     public void saveContent(final File file) throws IOException {
124         assertNotConsumed();
125         final int status = response.getCode();
126         if (status >= HttpStatus.SC_REDIRECTION) {
127             throw new HttpResponseException(status, response.getReasonPhrase());
128         }
129         try (FileOutputStream out = new FileOutputStream(file)) {
130             final HttpEntity entity = this.response.getEntity();
131             if (entity != null) {
132                 entity.writeTo(out);
133             }
134         } finally {
135             this.consumed = true;
136 
137         }
138     }
139 
140 }