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