URLConnectionOptions.java

  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. package org.apache.commons.configuration2.io;

  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.net.URLConnection;
  21. import java.util.Objects;

  22. /**
  23.  * Carries options and operates on {@link URLConnection}.
  24.  *
  25.  * @since 2.8.0
  26.  */
  27. public final class URLConnectionOptions {

  28.     /**
  29.      * Default options.
  30.      */
  31.     public static final URLConnectionOptions DEFAULT = new URLConnectionOptions();

  32.     /**
  33.      * TODO
  34.      */
  35.     private boolean allowUserInteraction;

  36.     /**
  37.      * TODO
  38.      */
  39.     private int connectTimeoutMillis;

  40.     /**
  41.      * TODO
  42.      */
  43.     private int readTimeoutMillis;

  44.     /**
  45.      * TODO
  46.      */
  47.     private boolean useCaches = true;

  48.     /**
  49.      * Constructs a new default instance.
  50.      */
  51.     public URLConnectionOptions() {
  52.         // Defaults initialized in declarations.
  53.     }

  54.     /**
  55.      * Constructs an instance with values from the given URLConnectionOptions.
  56.      *
  57.      * @param urlConnectionOptions the source
  58.      */
  59.     public URLConnectionOptions(final URLConnectionOptions urlConnectionOptions) {
  60.         this.allowUserInteraction = urlConnectionOptions.getAllowUserInteraction();
  61.         this.useCaches = urlConnectionOptions.getUseCaches();
  62.         this.connectTimeoutMillis = urlConnectionOptions.getConnectTimeoutMillis();
  63.         this.readTimeoutMillis = urlConnectionOptions.getReadTimeoutMillis();
  64.     }

  65.     /**
  66.      * Applies the options to the given connection.
  67.      *
  68.      * @param urlConnection the target connection.
  69.      * @return the given connection.
  70.      */
  71.     public URLConnection apply(final URLConnection urlConnection) {
  72.         urlConnection.setUseCaches(useCaches);
  73.         urlConnection.setConnectTimeout(connectTimeoutMillis);
  74.         urlConnection.setReadTimeout(readTimeoutMillis);
  75.         return urlConnection;
  76.     }

  77.     @Override
  78.     public boolean equals(final Object obj) {
  79.         if (this == obj) {
  80.             return true;
  81.         }
  82.         if (!(obj instanceof URLConnectionOptions)) {
  83.             return false;
  84.         }
  85.         final URLConnectionOptions other = (URLConnectionOptions) obj;
  86.         return allowUserInteraction == other.allowUserInteraction && connectTimeoutMillis == other.connectTimeoutMillis
  87.             && readTimeoutMillis == other.readTimeoutMillis && useCaches == other.useCaches;
  88.     }

  89.     /**
  90.      * Gets whether to allow user interaction.
  91.      *
  92.      * @return whether to allow user interaction.
  93.      */
  94.     public boolean getAllowUserInteraction() {
  95.         return allowUserInteraction;
  96.     }

  97.     /**
  98.      * Gets the connect timeout.
  99.      *
  100.      * @return the connect timeout.
  101.      */
  102.     public int getConnectTimeoutMillis() {
  103.         return connectTimeoutMillis;
  104.     }

  105.     /**
  106.      * Gets the read timeout.
  107.      *
  108.      * @return the read timeout.
  109.      */
  110.     public int getReadTimeoutMillis() {
  111.         return readTimeoutMillis;
  112.     }

  113.     /**
  114.      * Gets whether to cache.
  115.      *
  116.      * @return Whether to cache.
  117.      */
  118.     public boolean getUseCaches() {
  119.         return useCaches;
  120.     }

  121.     @Override
  122.     public int hashCode() {
  123.         return Objects.hash(allowUserInteraction, connectTimeoutMillis, readTimeoutMillis, useCaches);
  124.     }

  125.     /**
  126.      * Opens a connection for the given URL with our options.
  127.      *
  128.      * @param url the URL to open
  129.      * @return A new connection
  130.      * @throws IOException if an I/O exception occurs.
  131.      */
  132.     public URLConnection openConnection(final URL url) throws IOException {
  133.         return apply(url.openConnection());
  134.     }

  135.     public URLConnectionOptions setAllowUserInteraction(final boolean allowUserInteraction) {
  136.         this.allowUserInteraction = allowUserInteraction;
  137.         return this;
  138.     }

  139.     public URLConnectionOptions setConnectTimeoutMillis(final int connectTimeoutMillis) {
  140.         this.connectTimeoutMillis = connectTimeoutMillis;
  141.         return this;
  142.     }

  143.     public URLConnectionOptions setReadTimeoutMillis(final int readTimeoutMillis) {
  144.         this.readTimeoutMillis = readTimeoutMillis;
  145.         return this;
  146.     }

  147.     public URLConnectionOptions setUseCaches(final boolean useCaches) {
  148.         this.useCaches = useCaches;
  149.         return this;
  150.     }

  151.     @Override
  152.     public String toString() {
  153.         return "URLConnectionOptions [allowUserInteraction=" + allowUserInteraction + ", connectTimeoutMillis=" + connectTimeoutMillis + ", readTimeoutMillis="
  154.             + readTimeoutMillis + ", useCaches=" + useCaches + "]";
  155.     }
  156. }