-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathURIRequirementBuilder.java
224 lines (207 loc) · 6.47 KB
/
URIRequirementBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* The MIT License
*
* Copyright (c) 2011-2013, CloudBees, Inc., Stephen Connolly.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.plugins.credentials.domains;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A builder to help creating requirements from URIs.
*
* @since 1.6
*/
public class URIRequirementBuilder {
/**
* The list of requirements.
*/
@NonNull
private final List<DomainRequirement> requirements;
/**
* Private constructor.
*
* @param requirements the list of requirements.
*/
private URIRequirementBuilder(@NonNull List<DomainRequirement> requirements) {
this.requirements = new ArrayList<>(requirements);
}
/**
* Creates an empty builder.
*
* @return a new empty builder.
*/
@NonNull
public static URIRequirementBuilder create() {
return new URIRequirementBuilder(Collections.emptyList());
}
/**
* Creates a new builder using the supplied URI.
*
* @param uri the URI to create the requirements of.
* @return a new builder with the requirements of the supplied URI.
*/
@NonNull
public static URIRequirementBuilder fromUri(@CheckForNull String uri) {
return create().withUri(uri);
}
/**
* Creates a new builder with the same requirements as this builder.
*
* @return a new builder with the same requirements as this builder.
*/
@NonNull
public URIRequirementBuilder duplicate() {
return new URIRequirementBuilder(requirements);
}
/**
* Replaces the requirements with those of the supplied URI.
*
* @param uri the URI.
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withUri(@CheckForNull String uri) {
if (uri != null) {
try {
URI u = new URI(uri);
withScheme(u.getScheme());
withHostnamePort(u.getHost(), u.getPort());
withPath(u.getRawPath());
} catch (URISyntaxException e) {
withoutScheme().withoutHostname().withoutHostnamePort();
}
}
return this;
}
/**
* Removes any scheme requirements.
*
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withoutScheme() {
requirements.removeIf(r -> r instanceof SchemeRequirement);
return this;
}
/**
* Removes any path requirements.
*
* @return {@code this}.
* @since 1.12
*/
@NonNull
public URIRequirementBuilder withoutPath() {
requirements.removeIf(r -> r instanceof PathRequirement);
return this;
}
/**
* Removes any hostname or hostname:port requirements.
*
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withoutHostname() {
requirements.removeIf(r -> r instanceof HostnameRequirement);
return this;
}
/**
* Removes any hostname:port requirements.
*
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withoutHostnamePort() {
requirements.removeIf(r -> r instanceof HostnamePortRequirement);
return this;
}
/**
* Replace any scheme requirements with the supplied scheme.
*
* @param scheme the scheme to use as a requirement
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withScheme(@CheckForNull String scheme) {
withoutScheme();
if (scheme != null) {
requirements.add(new SchemeRequirement(scheme));
}
return this;
}
/**
* Replace any path requirements with the supplied path.
*
* @param path the path to use as a requirement
* @return {@code this}.
* @since 1.12
*/
@NonNull
public URIRequirementBuilder withPath(@CheckForNull String path) {
withoutPath();
if (path != null) {
requirements.add(new PathRequirement(path));
}
return this;
}
/**
* Replace any hostname requirements with the supplied hostname.
*
* @param hostname the hostname to use as a requirement
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withHostname(@CheckForNull String hostname) {
return withHostnamePort(hostname, -1);
}
/**
* Replace any hostname or hostname:port requirements with the supplied hostname and port.
*
* @param hostname the hostname to use as a requirement or (@code null} to not add any requirement
* @param port the port or {@code -1} to not add {@link HostnamePortRequirement}s
* @return {@code this}.
*/
@NonNull
public URIRequirementBuilder withHostnamePort(@CheckForNull String hostname, int port) {
withoutHostname();
withoutHostnamePort();
if (hostname != null) {
requirements.add(new HostnameRequirement(hostname));
if (port != -1) {
requirements.add(new HostnamePortRequirement(hostname, port));
}
}
return this;
}
/**
* Builds the list of requirements.
*
* @return the list of requirements.
*/
@NonNull
public List<DomainRequirement> build() {
return new ArrayList<>(requirements);
}
}