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.IOException;
30  
31  import org.apache.hc.client5.http.HttpResponseException;
32  import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
33  import org.apache.hc.core5.http.ClassicHttpResponse;
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.http.HttpEntity;
36  import org.apache.hc.core5.http.io.entity.EntityUtils;
37  
38  /**
39   * {@link org.apache.hc.core5.http.io.HttpClientResponseHandler} implementation
40   * that converts {@link org.apache.hc.core5.http.HttpResponse} messages
41   * to {@link Content} instances.
42   *
43   * @see Content
44   * @since 4.4
45   */
46  public class ContentResponseHandler extends AbstractHttpClientResponseHandler<Content> {
47  
48  
49      /**
50       * The maximum length of the exception message, to avoid excessive memory usage.
51       */
52      private static final int MAX_MESSAGE_LENGTH = 256;
53  
54      @Override
55      public Content handleEntity(final HttpEntity entity) throws IOException {
56          return entity != null ?
57                  new Content(EntityUtils.toByteArray(entity), ContentType.parse(entity.getContentType())) :
58                  Content.NO_CONTENT;
59      }
60  
61      /**
62       * Handles a successful response (2xx status code) and returns the response entity as a {@link Content} object.
63       * If no response entity exists, {@link Content#NO_CONTENT} is returned.
64       *
65       * @param response the HTTP response.
66       * @return a {@link Content} object that encapsulates the response body, or {@link Content#NO_CONTENT} if the
67       * response body is {@code null} or has zero length.
68       * @throws HttpResponseException if the response was unsuccessful (status code greater than 300).
69       * @throws IOException           if an I/O error occurs.
70       */
71      @Override
72      public Content handleResponse(final ClassicHttpResponse response) throws IOException {
73          final int statusCode = response.getCode();
74          final HttpEntity entity = response.getEntity();
75          final byte[] contentBytes = (entity != null) ? EntityUtils.toByteArray(entity, MAX_MESSAGE_LENGTH) : new byte[0];
76          final ContentType contentType = (entity != null && entity.getContentType() != null) ? ContentType.parse(entity.getContentType()) : ContentType.DEFAULT_BINARY;
77          final Content content = new Content(contentBytes, contentType);
78          if (statusCode >= 300) {
79              throw new HttpResponseException(statusCode, response.getReasonPhrase(), contentBytes, contentType);
80          }
81          return content;
82      }
83  }