View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model.vm
3   //  template, any modifications will be overwritten.
4   // ==============================================================
5   package org.apache.maven.api.settings;
6   
7   import java.io.Serializable;
8   import java.util.Collections;
9   import java.util.HashMap;
10  import java.util.Map;
11  import org.apache.maven.api.annotations.Experimental;
12  import org.apache.maven.api.annotations.Generated;
13  import org.apache.maven.api.annotations.Immutable;
14  import org.apache.maven.api.annotations.Nonnull;
15  import org.apache.maven.api.annotations.NotThreadSafe;
16  import org.apache.maven.api.annotations.ThreadSafe;
17  
18  /**
19   * The <code>&lt;proxy&gt;</code> element contains informations required to a proxy settings.
20   */
21  @Experimental
22  @Generated @ThreadSafe @Immutable
23  public class Proxy
24      extends IdentifiableBase
25      implements Serializable, InputLocationTracker
26  {
27      /**
28       * Whether this proxy configuration is the active one. Note: While the type of this field
29       * is {@code String} for technical reasons, the semantic type is actually {@code boolean}.
30       * @see #isActive()
31       */
32      final String activeString;
33      /**
34       * The proxy protocol.
35       */
36      final String protocol;
37      /**
38       * The proxy user.
39       */
40      final String username;
41      /**
42       * The proxy password.
43       */
44      final String password;
45      /**
46       * The proxy port. Note: While the type of this field is {@code String} for technical
47       * reasons, the semantic type is actually {@code int}.
48       * @see #getPort()
49       */
50      final String portString;
51      /**
52       * The proxy host.
53       */
54      final String host;
55      /**
56       * The list of non-proxied hosts (delimited by |).
57       */
58      final String nonProxyHosts;
59  
60      /**
61        * Constructor for this class, package protected.
62        * @see Builder#build()
63        */
64      Proxy(
65          String id,
66          String activeString,
67          String protocol,
68          String username,
69          String password,
70          String portString,
71          String host,
72          String nonProxyHosts,
73          Map<Object, InputLocation> locations
74      ) {
75          super(
76              id,
77              locations
78          );
79          this.activeString = activeString;
80          this.protocol = protocol;
81          this.username = username;
82          this.password = password;
83          this.portString = portString;
84          this.host = host;
85          this.nonProxyHosts = nonProxyHosts;
86      }
87  
88      /**
89       * Whether this proxy configuration is the active one. Note: While the type of this field
90       * is {@code String} for technical reasons, the semantic type is actually {@code boolean}.
91       * @see #isActive()
92       *
93       * @return a {@code String}
94       */
95      public String getActiveString() {
96          return this.activeString;
97      }
98  
99      /**
100      * The proxy protocol.
101      *
102      * @return a {@code String}
103      */
104     public String getProtocol() {
105         return this.protocol;
106     }
107 
108     /**
109      * The proxy user.
110      *
111      * @return a {@code String}
112      */
113     public String getUsername() {
114         return this.username;
115     }
116 
117     /**
118      * The proxy password.
119      *
120      * @return a {@code String}
121      */
122     public String getPassword() {
123         return this.password;
124     }
125 
126     /**
127      * The proxy port. Note: While the type of this field is {@code String} for technical
128      * reasons, the semantic type is actually {@code int}.
129      * @see #getPort()
130      *
131      * @return a {@code String}
132      */
133     public String getPortString() {
134         return this.portString;
135     }
136 
137     /**
138      * The proxy host.
139      *
140      * @return a {@code String}
141      */
142     public String getHost() {
143         return this.host;
144     }
145 
146     /**
147      * The list of non-proxied hosts (delimited by |).
148      *
149      * @return a {@code String}
150      */
151     public String getNonProxyHosts() {
152         return this.nonProxyHosts;
153     }
154 
155     /**
156      * Creates a new builder with this object as the basis.
157      *
158      * @return a {@code Builder}
159      */
160     @Nonnull
161     public Builder with() {
162         return newBuilder(this);
163     }
164     /**
165      * Creates a new {@code Proxy} instance using the specified id.
166      *
167      * @param id the new {@code String} to use
168      * @return a {@code Proxy} with the specified id
169      */
170     @Nonnull
171     public Proxy withId(String id) {
172         return newBuilder(this, true).id(id).build();
173     }
174     /**
175      * Creates a new {@code Proxy} instance using the specified activeString.
176      *
177      * @param activeString the new {@code String} to use
178      * @return a {@code Proxy} with the specified activeString
179      */
180     @Nonnull
181     public Proxy withActiveString(String activeString) {
182         return newBuilder(this, true).activeString(activeString).build();
183     }
184     /**
185      * Creates a new {@code Proxy} instance using the specified protocol.
186      *
187      * @param protocol the new {@code String} to use
188      * @return a {@code Proxy} with the specified protocol
189      */
190     @Nonnull
191     public Proxy withProtocol(String protocol) {
192         return newBuilder(this, true).protocol(protocol).build();
193     }
194     /**
195      * Creates a new {@code Proxy} instance using the specified username.
196      *
197      * @param username the new {@code String} to use
198      * @return a {@code Proxy} with the specified username
199      */
200     @Nonnull
201     public Proxy withUsername(String username) {
202         return newBuilder(this, true).username(username).build();
203     }
204     /**
205      * Creates a new {@code Proxy} instance using the specified password.
206      *
207      * @param password the new {@code String} to use
208      * @return a {@code Proxy} with the specified password
209      */
210     @Nonnull
211     public Proxy withPassword(String password) {
212         return newBuilder(this, true).password(password).build();
213     }
214     /**
215      * Creates a new {@code Proxy} instance using the specified portString.
216      *
217      * @param portString the new {@code String} to use
218      * @return a {@code Proxy} with the specified portString
219      */
220     @Nonnull
221     public Proxy withPortString(String portString) {
222         return newBuilder(this, true).portString(portString).build();
223     }
224     /**
225      * Creates a new {@code Proxy} instance using the specified host.
226      *
227      * @param host the new {@code String} to use
228      * @return a {@code Proxy} with the specified host
229      */
230     @Nonnull
231     public Proxy withHost(String host) {
232         return newBuilder(this, true).host(host).build();
233     }
234     /**
235      * Creates a new {@code Proxy} instance using the specified nonProxyHosts.
236      *
237      * @param nonProxyHosts the new {@code String} to use
238      * @return a {@code Proxy} with the specified nonProxyHosts
239      */
240     @Nonnull
241     public Proxy withNonProxyHosts(String nonProxyHosts) {
242         return newBuilder(this, true).nonProxyHosts(nonProxyHosts).build();
243     }
244 
245     /**
246      * Creates a new {@code Proxy} instance.
247      * Equivalent to {@code newInstance(true)}.
248      * @see #newInstance(boolean)
249      *
250      * @return a new {@code Proxy}
251      */
252     @Nonnull
253     public static Proxy newInstance() {
254         return newInstance(true);
255     }
256 
257     /**
258      * Creates a new {@code Proxy} instance using default values or not.
259      * Equivalent to {@code newBuilder(withDefaults).build()}.
260      *
261      * @param withDefaults the boolean indicating whether default values should be used
262      * @return a new {@code Proxy}
263      */
264     @Nonnull
265     public static Proxy newInstance(boolean withDefaults) {
266         return newBuilder(withDefaults).build();
267     }
268 
269     /**
270      * Creates a new {@code Proxy} builder instance.
271      * Equivalent to {@code newBuilder(true)}.
272      * @see #newBuilder(boolean)
273      *
274      * @return a new {@code Builder}
275      */
276     @Nonnull
277     public static Builder newBuilder() {
278         return newBuilder(true);
279     }
280 
281     /**
282      * Creates a new {@code Proxy} builder instance using default values or not.
283      *
284      * @param withDefaults the boolean indicating whether default values should be used
285      * @return a new {@code Builder}
286      */
287     @Nonnull
288     public static Builder newBuilder(boolean withDefaults) {
289         return new Builder(withDefaults);
290     }
291 
292     /**
293      * Creates a new {@code Proxy} builder instance using the specified object as a basis.
294      * Equivalent to {@code newBuilder(from, false)}.
295      *
296      * @param from the {@code Proxy} instance to use as a basis
297      * @return a new {@code Builder}
298      */
299     @Nonnull
300     public static Builder newBuilder(Proxy from) {
301         return newBuilder(from, false);
302     }
303 
304     /**
305      * Creates a new {@code Proxy} builder instance using the specified object as a basis.
306      *
307      * @param from the {@code Proxy} instance to use as a basis
308      * @param forceCopy the boolean indicating if a copy should be forced
309      * @return a new {@code Builder}
310      */
311     @Nonnull
312     public static Builder newBuilder(Proxy from, boolean forceCopy) {
313         return new Builder(from, forceCopy);
314     }
315 
316     /**
317      * Builder class used to create Proxy instances.
318      * @see #with()
319      * @see #newBuilder()
320      */
321     @NotThreadSafe
322     public static class Builder
323         extends IdentifiableBase.Builder
324     {
325         Proxy base;
326         String activeString;
327         String protocol;
328         String username;
329         String password;
330         String portString;
331         String host;
332         String nonProxyHosts;
333 
334         Builder(boolean withDefaults) {
335             super(withDefaults);
336             if (withDefaults) {
337                 this.activeString = "true";
338                 this.protocol = "http";
339                 this.portString = "8080";
340             }
341         }
342 
343         Builder(Proxy base, boolean forceCopy) {
344             super(base, forceCopy);
345             if (forceCopy) {
346                 this.activeString = base.activeString;
347                 this.protocol = base.protocol;
348                 this.username = base.username;
349                 this.password = base.password;
350                 this.portString = base.portString;
351                 this.host = base.host;
352                 this.nonProxyHosts = base.nonProxyHosts;
353                 this.locations = base.locations;
354             } else {
355                 this.base = base;
356             }
357         }
358 
359         @Nonnull
360         public Builder id(String id) {
361             this.id = id;
362             return this;
363         }
364 
365         @Nonnull
366         public Builder activeString(String activeString) {
367             this.activeString = activeString;
368             return this;
369         }
370 
371         @Nonnull
372         public Builder protocol(String protocol) {
373             this.protocol = protocol;
374             return this;
375         }
376 
377         @Nonnull
378         public Builder username(String username) {
379             this.username = username;
380             return this;
381         }
382 
383         @Nonnull
384         public Builder password(String password) {
385             this.password = password;
386             return this;
387         }
388 
389         @Nonnull
390         public Builder portString(String portString) {
391             this.portString = portString;
392             return this;
393         }
394 
395         @Nonnull
396         public Builder host(String host) {
397             this.host = host;
398             return this;
399         }
400 
401         @Nonnull
402         public Builder nonProxyHosts(String nonProxyHosts) {
403             this.nonProxyHosts = nonProxyHosts;
404             return this;
405         }
406 
407 
408         @Nonnull
409         public Builder location(Object key, InputLocation location) {
410             if (location != null) {
411                 if (!(this.locations instanceof HashMap)) {
412                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
413                 }
414                 this.locations.put(key, location);
415             }
416             return this;
417         }
418 
419         @Nonnull
420         public Proxy build() {
421             if (base != null
422                     && (id == null || id == base.id)
423                     && (activeString == null || activeString == base.activeString)
424                     && (protocol == null || protocol == base.protocol)
425                     && (username == null || username == base.username)
426                     && (password == null || password == base.password)
427                     && (portString == null || portString == base.portString)
428                     && (host == null || host == base.host)
429                     && (nonProxyHosts == null || nonProxyHosts == base.nonProxyHosts)
430             ) {
431                 return base;
432             }
433             Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
434             Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
435             Map<Object, InputLocation> locations = new HashMap<>();
436             locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
437             locations.put("id", newlocs.containsKey("id") ? newlocs.get("id") : oldlocs.get("id"));
438             locations.put("activeString", newlocs.containsKey("activeString") ? newlocs.get("activeString") : oldlocs.get("activeString"));
439             locations.put("protocol", newlocs.containsKey("protocol") ? newlocs.get("protocol") : oldlocs.get("protocol"));
440             locations.put("username", newlocs.containsKey("username") ? newlocs.get("username") : oldlocs.get("username"));
441             locations.put("password", newlocs.containsKey("password") ? newlocs.get("password") : oldlocs.get("password"));
442             locations.put("portString", newlocs.containsKey("portString") ? newlocs.get("portString") : oldlocs.get("portString"));
443             locations.put("host", newlocs.containsKey("host") ? newlocs.get("host") : oldlocs.get("host"));
444             locations.put("nonProxyHosts", newlocs.containsKey("nonProxyHosts") ? newlocs.get("nonProxyHosts") : oldlocs.get("nonProxyHosts"));
445             return new Proxy(
446                 id != null ? id : (base != null ? base.id : null),
447                 activeString != null ? activeString : (base != null ? base.activeString : null),
448                 protocol != null ? protocol : (base != null ? base.protocol : null),
449                 username != null ? username : (base != null ? base.username : null),
450                 password != null ? password : (base != null ? base.password : null),
451                 portString != null ? portString : (base != null ? base.portString : null),
452                 host != null ? host : (base != null ? base.host : null),
453                 nonProxyHosts != null ? nonProxyHosts : (base != null ? base.nonProxyHosts : null),
454                 locations
455             );
456         }
457     }
458 
459 
460     /**
461      * Indicates if this proxy is active.
462      * To allow interpolation of this field, this method lazily parses
463      * the {@link #getActiveString()} value as a boolean and defaults to {@code true}
464      * if not set.
465      *
466      * @return a boolean indicating if this proxy is active
467      */
468     public boolean isActive() {
469         return (getActiveString() != null) ? Boolean.parseBoolean(getActiveString()) : true;
470     }
471 
472     /**
473      * Returns the port to use for this proxy.
474      * To allow interpolation of this field, this method lazily parses
475      * the {@link #getPortString()} value as an integer and defaults to {@code 8080}
476      * if not set.
477      *
478      * @return an integer indicating the port to use for this proxy
479      */
480     public int getPort() {
481         return (getPortString() != null) ? Integer.parseInt(getPortString()) : 8080;
482     }
483 
484           
485 }