11package docker.registry.web
22
3+ import groovyx.net.http.HTTPBuilder
4+
35class Registry {
4- String url
5- String apiVersion
6+ String host = " localhost"
7+ int port
8+ String apiVersion = " v1"
9+ String username
10+ String password
611
712 def repositoryService
813
914 static constraints = {
15+ username nullable : true
16+ password nullable : true
1017 }
1118
12- static transients = [' toUrl' , ' repositories' , ' ping' ]
19+ static transients = [' toUrl' , ' repositories' , ' ping' , ' fromUrl ' ]
1320
1421 def toUrl () {
15- return " ${ this.url} /${ this.apiVersion} "
22+ def urlString = " http://${ this.host} :${ this.port} /${ this.apiVersion} "
23+ if (username) {
24+ if (password) urlString = urlString. replace(" ://" , " ://$username :$password @" )
25+ else urlString = urlString. replace(" ://" , " ://$username @" )
26+ }
27+ urlString
1628 }
1729
1830 def getRepositories () {
@@ -23,12 +35,37 @@ class Registry {
2335 repositoryService. ping(this )
2436 }
2537
38+ /**
39+ * Static factory method for creating an instance from a URL.
40+ * @param urlStr a url in the format: http://hostOrIP:OptionalPort/v1/
41+ **/
42+ static def fromUrl (final String urlStr ) {
43+ if (urlStr?. endsWith(" /v1/" )) { // FIXME this won't work for new api versions
44+ def url = urlStr. toURL()
45+ if (url) {
46+ def auth = url. userInfo?. split(" :" )
47+ return new Registry (
48+ host : url. host,
49+ port : url. port == -1 ? 80 : url. port,
50+ apiVersion : url. path. replaceAll(" \\ p{Punct}" , " " ),
51+ username : auth?. length > 0 ? auth[0 ] : null ,
52+ password : auth?. length > 1 ? auth[1 ] : null
53+ )
54+ }
55+ }
56+ null
57+ }
58+
2659 @Override
2760 public String toString () {
2861 return " Registry{" +
2962 " id=" + id +
30- " , url='" + url + ' \' ' +
63+ " , host='" + host + ' \' ' +
64+ " , port=" + port +
3165 " , apiVersion='" + apiVersion + ' \' ' +
66+ " , username='" + username + ' \' ' +
67+ " , password='" + password + ' \' ' +
68+ " , repositoryService=" + repositoryService +
3269 " , version=" + version +
3370 ' }' ;
3471 }
0 commit comments