1 package org.sonatype.aether.repository;
2
3
4
5
6
7
8
9
10
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
15
16
17
18
19
20
21 public class RemoteRepository
22 implements ArtifactRepository
23 {
24
25 private static final Pattern URL_PATTERN =
26 Pattern.compile( "([^:/]+(:[^:/]{2,}+(?=://))?):(//([^@/]*@)?([^/:]+))?.*" );
27
28 private String id = "";
29
30 private String type = "";
31
32 private String url = "";
33
34 private RepositoryPolicy releasePolicy;
35
36 private RepositoryPolicy snapshotPolicy;
37
38 private Proxy proxy;
39
40 private Authentication authentication;
41
42 private List<RemoteRepository> mirroredRepositories = Collections.emptyList();
43
44 private boolean repositoryManager;
45
46
47
48
49 public RemoteRepository()
50 {
51 setPolicy( true, null );
52 setPolicy( false, null );
53 }
54
55
56
57
58
59
60 public RemoteRepository( RemoteRepository repository )
61 {
62 setId( repository.getId() );
63 setContentType( repository.getContentType() );
64 setUrl( repository.getUrl() );
65 setPolicy( true, repository.getPolicy( true ) );
66 setPolicy( false, repository.getPolicy( false ) );
67 setAuthentication( repository.getAuthentication() );
68 setProxy( repository.getProxy() );
69 setMirroredRepositories( repository.getMirroredRepositories() );
70 setRepositoryManager( repository.isRepositoryManager() );
71 }
72
73
74
75
76
77
78
79
80 public RemoteRepository( String id, String type, String url )
81 {
82 setId( id );
83 setContentType( type );
84 setUrl( url );
85 setPolicy( true, null );
86 setPolicy( false, null );
87 }
88
89 public String getId()
90 {
91 return id;
92 }
93
94
95
96
97
98
99
100 public RemoteRepository setId( String id )
101 {
102 this.id = ( id != null ) ? id : "";
103
104 return this;
105 }
106
107 public String getContentType()
108 {
109 return type;
110 }
111
112
113
114
115
116
117
118 public RemoteRepository setContentType( String type )
119 {
120 this.type = ( type != null ) ? type : "";
121
122 return this;
123 }
124
125
126
127
128
129
130 public String getUrl()
131 {
132 return url;
133 }
134
135
136
137
138
139
140
141 public RemoteRepository setUrl( String url )
142 {
143 this.url = ( url != null ) ? url : "";
144
145 return this;
146 }
147
148
149
150
151
152
153
154 public String getProtocol()
155 {
156 Matcher m = URL_PATTERN.matcher( this.url );
157
158 if ( m.matches() )
159 {
160 return m.group( 1 );
161 }
162 return "";
163 }
164
165
166
167
168
169
170 public String getHost()
171 {
172 Matcher m = URL_PATTERN.matcher( this.url );
173
174 if ( m.matches() )
175 {
176 String host = m.group( 5 );
177 if ( host != null )
178 {
179 return host;
180 }
181 }
182 return "";
183 }
184
185
186
187
188
189
190
191 public RepositoryPolicy getPolicy( boolean snapshot )
192 {
193 return snapshot ? snapshotPolicy : releasePolicy;
194 }
195
196
197
198
199
200
201
202
203 public RemoteRepository setPolicy( boolean snapshot, RepositoryPolicy policy )
204 {
205 if ( policy == null )
206 {
207 policy = new RepositoryPolicy();
208 }
209
210 if ( snapshot )
211 {
212 snapshotPolicy = policy;
213 }
214 else
215 {
216 releasePolicy = policy;
217 }
218
219 return this;
220 }
221
222
223
224
225
226
227 public Proxy getProxy()
228 {
229 return proxy;
230 }
231
232
233
234
235
236
237
238 public RemoteRepository setProxy( Proxy proxy )
239 {
240 this.proxy = proxy;
241
242 return this;
243 }
244
245
246
247
248
249
250 public Authentication getAuthentication()
251 {
252 return authentication;
253 }
254
255
256
257
258
259
260
261 public RemoteRepository setAuthentication( Authentication authentication )
262 {
263 this.authentication = authentication;
264
265 return this;
266 }
267
268
269
270
271
272
273 public List<RemoteRepository> getMirroredRepositories()
274 {
275 return mirroredRepositories;
276 }
277
278
279
280
281
282
283
284 public RemoteRepository setMirroredRepositories( List<RemoteRepository> mirroredRepositories )
285 {
286 if ( mirroredRepositories == null || mirroredRepositories.isEmpty() )
287 {
288 this.mirroredRepositories = Collections.emptyList();
289 }
290 else
291 {
292 this.mirroredRepositories = mirroredRepositories;
293 }
294 return this;
295 }
296
297
298
299
300
301
302 public boolean isRepositoryManager()
303 {
304 return repositoryManager;
305 }
306
307
308
309
310
311
312
313
314 public RemoteRepository setRepositoryManager( boolean repositoryManager )
315 {
316 this.repositoryManager = repositoryManager;
317 return this;
318 }
319
320 @Override
321 public String toString()
322 {
323 StringBuilder buffer = new StringBuilder( 256 );
324 buffer.append( getId() );
325 buffer.append( " (" ).append( getUrl() );
326 boolean r = getPolicy( false ).isEnabled(), s = getPolicy( true ).isEnabled();
327 if ( r && s )
328 {
329 buffer.append( ", releases+snapshots" );
330 }
331 else if ( r )
332 {
333 buffer.append( ", releases" );
334 }
335 else if ( s )
336 {
337 buffer.append( ", snapshots" );
338 }
339 else
340 {
341 buffer.append( ", disabled" );
342 }
343 if ( isRepositoryManager() )
344 {
345 buffer.append( ", managed" );
346 }
347 buffer.append( ")" );
348 return buffer.toString();
349 }
350
351 @Override
352 public boolean equals( Object obj )
353 {
354 if ( this == obj )
355 {
356 return true;
357 }
358 if ( obj == null || !getClass().equals( obj.getClass() ) )
359 {
360 return false;
361 }
362
363 RemoteRepository that = (RemoteRepository) obj;
364
365 return eq( url, that.url ) && eq( type, that.type ) && eq( id, that.id )
366 && eq( releasePolicy, that.releasePolicy ) && eq( snapshotPolicy, that.snapshotPolicy )
367 && eq( proxy, that.proxy ) && eq( authentication, that.authentication )
368 && eq( mirroredRepositories, that.mirroredRepositories ) && repositoryManager == that.repositoryManager;
369 }
370
371 private static <T> boolean eq( T s1, T s2 )
372 {
373 return s1 != null ? s1.equals( s2 ) : s2 == null;
374 }
375
376 @Override
377 public int hashCode()
378 {
379 int hash = 17;
380 hash = hash * 31 + hash( url );
381 hash = hash * 31 + hash( type );
382 hash = hash * 31 + hash( id );
383 hash = hash * 31 + hash( releasePolicy );
384 hash = hash * 31 + hash( snapshotPolicy );
385 hash = hash * 31 + hash( proxy );
386 hash = hash * 31 + hash( authentication );
387 hash = hash * 31 + hash( mirroredRepositories );
388 hash = hash * 31 + ( repositoryManager ? 1 : 0 );
389 return hash;
390 }
391
392 private static int hash( Object obj )
393 {
394 return obj != null ? obj.hashCode() : 0;
395 }
396
397 }