View Javadoc
1   /*
2   * The contents of this file are subject to the terms of the Common Development and
3   * Distribution License (the License). You may not use this file except in compliance with the
4   * License.
5   *
6   * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7   * specific language governing permission and limitations under the License.
8   *
9   * When distributing Covered Software, include this CDDL Header Notice in each file and include
10  * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11  * Header, with the fields enclosed by brackets [] replaced by your own identifying
12  * information: "Portions copyright [year] [name of copyright owner]".
13  *
14  * Copyright 2014-2017 ForgeRock AS.
15  */
16  
17  package org.forgerock.util;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.net.URL;
23  import java.net.URLConnection;
24  
25  /**
26   * Simple helper client for connecting to URLs over HTTP
27   * and retrieving their contents via a GET request.
28   *
29   * Settable timeouts on read and connection.
30   */
31  public class SimpleHTTPClient {
32  
33      /**
34       * Default read timeout on HTTP requests from this client.
35       */
36      public static final int DEFAULT_READ_TIMEOUT = 5_000;
37  
38      /**
39       * Default connection timeout on HTTP requests from this client.
40       */
41      public static final int DEFAULT_CONNECTION_TIMEOUT = 5_000;
42  
43      private final int readTimeout;
44      private final int connTimeout;
45  
46      /**
47       * Generates a new SimpleHTTPClient with the appropriate timeouts.
48       */
49      public SimpleHTTPClient() {
50          this(DEFAULT_READ_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
51      }
52  
53      /**
54       * Generates a new SimpleHTTPClient with the appropriate timeouts.
55       *
56       * @param readTimeout read timeout value (greater than or equal to zero)
57       * @param connTimeout connection timeout value (greater than or equal to zero)
58       */
59      public SimpleHTTPClient(final int readTimeout, final int connTimeout) {
60  
61          if (readTimeout < 0 || connTimeout < 0) {
62              throw new IllegalArgumentException("Unable to set the read or connection timeouts "
63                      + "to a value less than zero");
64          }
65  
66          this.readTimeout = readTimeout;
67          this.connTimeout = connTimeout;
68      }
69  
70      /**
71       * Utility method for gathering the contents of an HTTP page.
72       *
73       * Should ideally be in an HTTP Client utils type package, rather than here.
74       *
75       * @param url from which to attempt to retrieve the contents
76       * @return The contents of the provided url
77       * @throws java.io.IOException If there are any problems connecting to or gathering the contents of the page
78       */
79      public String get(final URL url) throws IOException {
80          final URLConnection conn = url.openConnection();
81  
82          if (readTimeout >= 0) {
83              conn.setReadTimeout(readTimeout);
84          } else {
85              conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
86          }
87  
88          if (connTimeout >= 0) {
89              conn.setConnectTimeout(connTimeout);
90          } else {
91              conn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
92          }
93  
94          final StringBuilder sb = new StringBuilder();
95  
96          try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
97              String input;
98              while ((input = reader.readLine()) != null) {
99                  sb.append(input);
100             }
101         }
102 
103         return sb.toString();
104     }
105 
106 }