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.examples.fluent;
28  
29  import java.nio.charset.Charset;
30  import java.nio.charset.StandardCharsets;
31  
32  import javax.xml.parsers.DocumentBuilder;
33  import javax.xml.parsers.DocumentBuilderFactory;
34  import javax.xml.parsers.ParserConfigurationException;
35  
36  import org.apache.hc.client5.http.ClientProtocolException;
37  import org.apache.hc.client5.http.HttpResponseException;
38  import org.apache.hc.client5.http.fluent.Request;
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.http.HttpEntity;
41  import org.apache.hc.core5.http.HttpStatus;
42  import org.w3c.dom.Document;
43  import org.xml.sax.SAXException;
44  
45  /**
46   * This example demonstrates how the HttpClient fluent API can be used to handle HTTP responses
47   * without buffering content body in memory.
48   */
49  public class FluentResponseHandling {
50  
51      public static void main(final String... args)throws Exception {
52          final Document result = Request.get("http://somehost/content")
53                  .execute().handleResponse(response -> {
54                      final int status = response.getCode();
55                      final HttpEntity entity = response.getEntity();
56                      if (status >= HttpStatus.SC_REDIRECTION) {
57                          throw new HttpResponseException(status, response.getReasonPhrase());
58                      }
59                      if (entity == null) {
60                          throw new ClientProtocolException("Response contains no content");
61                      }
62                      final DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
63                      try {
64                          final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
65                          final ContentType contentType = ContentType.parseLenient(entity.getContentType());
66                          if (!contentType.equals(ContentType.APPLICATION_XML)) {
67                              throw new ClientProtocolException("Unexpected content type:" + contentType);
68                          }
69                          Charset charset = contentType.getCharset();
70                          if (charset == null) {
71                              charset = StandardCharsets.ISO_8859_1;
72                          }
73                          return docBuilder.parse(entity.getContent(), charset.name());
74                      } catch (final ParserConfigurationException ex) {
75                          throw new IllegalStateException(ex);
76                      } catch (final SAXException ex) {
77                          throw new ClientProtocolException("Malformed XML document", ex);
78                      }
79                  });
80          // Do something useful with the result
81          System.out.println(result);
82      }
83  
84  }