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