View Javadoc

1   package com.atlassian.httpclient.api.factory;
2   
3   import com.google.common.base.Preconditions;
4   import com.google.common.collect.ImmutableMap;
5   
6   import javax.annotation.Nonnull;
7   import java.util.Collections;
8   import java.util.HashMap;
9   import java.util.List;
10  import java.util.Map;
11  
12  /**
13   * Contains proxy configuration for the HTTP client
14   */
15  public class ProxyOptions {
16      /**
17       * Represents the mode of proxy configuration (i.e. from standard system properties (default), or configured
18       * by the library consumer, or no proxy at all)
19       */
20      public enum ProxyMode {
21          SYSTEM_PROPERTIES, CONFIGURED, NO_PROXY
22      }
23  
24      /**
25       * Get the mapping of schemes and their proxy hosts.
26       *
27       * @return the mapping of schemes and their proxy hosts.
28       */
29      public Map<Scheme, Host> getProxyHosts() {
30          return Collections.unmodifiableMap(proxyHostMap);
31      }
32  
33      /**
34       * @return the list of configured non-proxy hosts.
35       */
36      public Map<Scheme, List<String>> getNonProxyHosts() {
37          return Collections.unmodifiableMap(nonProxyHosts);
38      }
39  
40      /**
41       * @return the mode of proxy configuration
42       */
43      public ProxyMode getProxyMode() {
44          return proxyMode;
45      }
46  
47      private final Map<Scheme, Host> proxyHostMap;
48  
49      private final Map<Scheme, List<String>> nonProxyHosts;
50  
51      private final ProxyMode proxyMode;
52  
53      private ProxyOptions(ProxyMode mode, Map<Scheme, Host> proxyHostMap, Map<Scheme, List<String>> nonProxyHosts) {
54          this.proxyMode = mode;
55          this.proxyHostMap = proxyHostMap;
56          this.nonProxyHosts = nonProxyHosts;
57      }
58  
59      /**
60       * Use this builder to create a ProxyOptions
61       */
62      public static class ProxyOptionsBuilder {
63          private Map<Scheme, Host> proxyHostMap = new HashMap<Scheme, Host>();
64  
65          private Map<Scheme, List<String>> nonProxyHosts = new HashMap<Scheme, List<String>>();
66  
67          private ProxyMode proxyMode = ProxyMode.SYSTEM_PROPERTIES;
68  
69          /**
70           * Create a builder with default options (use what is configured in standard system properties (protocol).proxyHost/(protocol).proxyPort)
71           *
72           * @return Builder with default options set.
73           */
74          public static ProxyOptionsBuilder create() {
75              return new ProxyOptionsBuilder();
76          }
77  
78          /**
79           * @return The proxy options from the builder settings
80           */
81          public ProxyOptions build() {
82              return new ProxyOptions(proxyMode, proxyHostMap, nonProxyHosts);
83          }
84  
85          /**
86           * Use no proxy in the client
87           *
88           * @return Builder with 'no proxy' option set
89           */
90          public ProxyOptionsBuilder withNoProxy() {
91              proxyHostMap = ImmutableMap.of();
92              nonProxyHosts = ImmutableMap.of();
93              proxyMode = ProxyMode.NO_PROXY;
94              return this;
95          }
96  
97          /**
98           * Obtain proxy configuration for standard system properties (e.g. http.proxyHost, http.proxyPort, http.proxyUser, etc.)
99           *
100          * @return Builder with 'system properties' option set.
101          */
102         public ProxyOptionsBuilder withDefaultSystemProperties() {
103             proxyHostMap = ImmutableMap.of();
104             nonProxyHosts = ImmutableMap.of();
105             proxyMode = ProxyMode.SYSTEM_PROPERTIES;
106             return this;
107         }
108 
109         /**
110          * Add a proxy host for the given scheme. This enables 'configured' proxy mode, and will not obtain settings from
111          * system properties any more.
112          *
113          * @param scheme    the scheme
114          * @param proxyHost the proxy host
115          * @return Builder with appropriate settings
116          */
117         public ProxyOptionsBuilder withProxy(final @Nonnull Scheme scheme, final @Nonnull Host proxyHost) {
118             Preconditions.checkNotNull(proxyHost, "Proxy host cannot be null");
119             Preconditions.checkNotNull(scheme, "Scheme must not be null");
120 
121             this.proxyHostMap.put(scheme, proxyHost);
122             proxyMode = ProxyMode.CONFIGURED;
123             return this;
124         }
125 
126         /**
127          * Add a list of non-proxy hosts for the given scheme. This enables 'configured' proxy mode, and will not obtain settings from
128          * system properties any more.
129          *
130          * @param scheme        The scheme
131          * @param nonProxyHosts The list of non-proxy hosts
132          * @return Builder with appropriate settings
133          */
134         public ProxyOptionsBuilder withNonProxyHost(final @Nonnull Scheme scheme, final @Nonnull List<String> nonProxyHosts) {
135             Preconditions.checkNotNull(nonProxyHosts, "Non proxy hosts cannot be null");
136             Preconditions.checkNotNull(scheme, "Scheme must not be null");
137 
138             // this does not impact whether we consider our mode to be - only proxy host/port can determine that
139             this.nonProxyHosts.put(scheme, nonProxyHosts);
140             return this;
141         }
142 
143         /**
144          * Configure proxies as per given arguments. This enables 'configured' proxy mode, and will not obtain settings from
145          * system properties any more.
146          *
147          * @param proxyHostMap  Map of schemes to proxy hosts.
148          * @param nonProxyHosts List of hosts that we shouldn't use the proxy for
149          * @return Builder with appropriate settings
150          */
151         public ProxyOptionsBuilder withProxy(Map<Scheme, Host> proxyHostMap, Map<Scheme, List<String>> nonProxyHosts) {
152             this.proxyHostMap = proxyHostMap;
153             this.nonProxyHosts = nonProxyHosts;
154             proxyMode = ProxyMode.CONFIGURED;
155             return this;
156         }
157     }
158 
159 }