View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration2.io;
19  
20  import java.io.IOException;
21  import java.net.URL;
22  import java.net.URLConnection;
23  import java.util.Objects;
24  
25  /**
26   * Carries options and operates on {@link URLConnection}.
27   *
28   * @since 2.8.0
29   */
30  public final class URLConnectionOptions {
31  
32      /**
33       * Default options.
34       */
35      public static final URLConnectionOptions DEFAULT = new URLConnectionOptions();
36  
37      /**
38       * TODO
39       */
40      private boolean allowUserInteraction;
41  
42      /**
43       * TODO
44       */
45      private int connectTimeoutMillis;
46  
47      /**
48       * TODO
49       */
50      private int readTimeoutMillis;
51  
52      /**
53       * TODO
54       */
55      private boolean useCaches = true;
56  
57      /**
58       * Constructs a new default instance.
59       */
60      public URLConnectionOptions() {
61          // Defaults initialized in declarations.
62      }
63  
64      /**
65       * Constructs an instance with values from the given URLConnectionOptions.
66       *
67       * @param urlConnectionOptions the source
68       */
69      public URLConnectionOptions(final URLConnectionOptions urlConnectionOptions) {
70          this.allowUserInteraction = urlConnectionOptions.getAllowUserInteraction();
71          this.useCaches = urlConnectionOptions.getUseCaches();
72          this.connectTimeoutMillis = urlConnectionOptions.getConnectTimeoutMillis();
73          this.readTimeoutMillis = urlConnectionOptions.getReadTimeoutMillis();
74      }
75  
76      /**
77       * Applies the options to the given connection.
78       *
79       * @param urlConnection the target connection.
80       * @return the given connection.
81       */
82      public URLConnection apply(final URLConnection urlConnection) {
83          urlConnection.setUseCaches(useCaches);
84          urlConnection.setConnectTimeout(connectTimeoutMillis);
85          urlConnection.setReadTimeout(readTimeoutMillis);
86          return urlConnection;
87      }
88  
89      @Override
90      public boolean equals(final Object obj) {
91          if (this == obj) {
92              return true;
93          }
94          if (!(obj instanceof URLConnectionOptions)) {
95              return false;
96          }
97          final URLConnectionOptions other = (URLConnectionOptions) obj;
98          return allowUserInteraction == other.allowUserInteraction && connectTimeoutMillis == other.connectTimeoutMillis
99              && readTimeoutMillis == other.readTimeoutMillis && useCaches == other.useCaches;
100     }
101 
102     /**
103      * Gets whether to allow user interaction.
104      *
105      * @return whether to allow user interaction.
106      */
107     public boolean getAllowUserInteraction() {
108         return allowUserInteraction;
109     }
110 
111     /**
112      * Gets the connect timeout.
113      *
114      * @return the connect timeout.
115      */
116     public int getConnectTimeoutMillis() {
117         return connectTimeoutMillis;
118     }
119 
120     /**
121      * Gets the read timeout.
122      *
123      * @return the read timeout.
124      */
125     public int getReadTimeoutMillis() {
126         return readTimeoutMillis;
127     }
128 
129     /**
130      * Whether to cache.
131      *
132      * @return Whether to cache.
133      */
134     public boolean getUseCaches() {
135         return useCaches;
136     }
137 
138     @Override
139     public int hashCode() {
140         return Objects.hash(allowUserInteraction, connectTimeoutMillis, readTimeoutMillis, useCaches);
141     }
142 
143     /**
144      * Opens a connection for the given URL with our options.
145      *
146      * @param url the URL to open
147      * @return A new connection
148      * @throws IOException if an I/O exception occurs.
149      */
150     public URLConnection openConnection(final URL url) throws IOException {
151         return apply(url.openConnection());
152     }
153 
154     public URLConnectionOptions setAllowUserInteraction(final boolean allowUserInteraction) {
155         this.allowUserInteraction = allowUserInteraction;
156         return this;
157     }
158 
159     public URLConnectionOptions setConnectTimeoutMillis(final int connectTimeoutMillis) {
160         this.connectTimeoutMillis = connectTimeoutMillis;
161         return this;
162     }
163 
164     public URLConnectionOptions setReadTimeoutMillis(final int readTimeoutMillis) {
165         this.readTimeoutMillis = readTimeoutMillis;
166         return this;
167     }
168 
169     public URLConnectionOptions setUseCaches(final boolean useCaches) {
170         this.useCaches = useCaches;
171         return this;
172     }
173 
174     @Override
175     public String toString() {
176         return "URLConnectionOptions [allowUserInteraction=" + allowUserInteraction + ", connectTimeoutMillis=" + connectTimeoutMillis + ", readTimeoutMillis="
177             + readTimeoutMillis + ", useCaches=" + useCaches + "]";
178     }
179 }