Skip to content

Commit 2b4ca23

Browse files
committed
Add some utility classes for splitting up "tag parameters" passed
at various points (e.g: 10.0.0.1:8000/mybuild:1234) Signed-off-by: Nigel Magnay <nigel.magnay@gmail.com>
1 parent 0f01863 commit 2b4ca23

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.dockerjava.api.model;
2+
3+
4+
import com.google.common.base.Objects;
5+
import com.google.common.base.Optional;
6+
7+
/**
8+
* Created by magnayn on 22/07/2014.
9+
*/
10+
public class Identifier {
11+
public final Repository repository;
12+
public final Optional<String> tag;
13+
14+
public Identifier(Repository repository, String tag) {
15+
this.repository = repository;
16+
17+
if( tag == null )
18+
this.tag = Optional.absent();
19+
else
20+
this.tag = Optional.of(tag);
21+
}
22+
23+
24+
/**
25+
* Return an identifier that correctly splits up the repository and tag.
26+
* There can be &gt; 1 ":"
27+
* fred/jim --&gt; fred/jim, []
28+
* fred/jim:123 --&gt; fred/jim, 123
29+
* fred:123/jim:123 --&gt; fred:123/jim, 123
30+
*
31+
*
32+
* @param identifier as a string
33+
* @return parsed identifier.
34+
*/
35+
public static Identifier fromCompoundString(String identifier) {
36+
String[] parts = identifier.split("/");
37+
if( parts.length != 2 ) {
38+
String[] rhs = identifier.split(":");
39+
if( rhs.length != 2 )
40+
return new Identifier( new Repository(identifier), null);
41+
else
42+
return new Identifier( new Repository(rhs[0]), rhs[1]);
43+
}
44+
45+
String[] rhs = parts[1].split(":");
46+
if( rhs.length != 2 )
47+
return new Identifier( new Repository(identifier), null);
48+
49+
return new Identifier( new Repository(parts[0] + "/" + rhs[0]), rhs[1]);
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return Objects.toStringHelper(this)
55+
.add("repository", repository)
56+
.add("tag", tag)
57+
.toString();
58+
}
59+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.google.common.base.Objects;
4+
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
8+
/**
9+
* A repository or image name.
10+
*/
11+
public class Repository {
12+
public final String name;
13+
14+
/**
15+
* Name may be eg. 'busybox' or '10.0.0.1:5000/fred'
16+
* @param name Repository name
17+
*/
18+
public Repository(String name) {
19+
this.name = name;
20+
}
21+
22+
/**
23+
* Return the URL portion (repository).
24+
* Note that this might not actually BE a repository location.
25+
* @return
26+
* @throws java.net.MalformedURLException
27+
*/
28+
public URL getURL() throws MalformedURLException {
29+
return new URL("http://" + name);
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return Objects.toStringHelper(this)
35+
.add("name", name)
36+
.toString();
37+
}
38+
39+
40+
public String getPath() {
41+
if( !name.contains("/") )
42+
return name;
43+
44+
return name.substring(name.indexOf("/") + 1 );
45+
}
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import junit.framework.TestCase;
4+
5+
6+
public class IdentifierTest extends TestCase {
7+
8+
public void testFromCompoundString() throws Exception {
9+
10+
Identifier i1 = Identifier.fromCompoundString("10.0.0.1/jim");
11+
Identifier i2 = Identifier.fromCompoundString("10.0.0.1/jim:123");
12+
Identifier i3 = Identifier.fromCompoundString("10.0.0.1:123/jim:124");
13+
Identifier i3A = Identifier.fromCompoundString("10.0.0.1:123/jim:latest");
14+
15+
assertTrue(!i1.tag.isPresent());
16+
assertEquals(i1.repository.name, "10.0.0.1/jim");
17+
18+
assertTrue(i2.tag.isPresent());
19+
assertEquals(i2.tag.get(), "123");
20+
assertEquals(i2.repository.name, "10.0.0.1/jim");
21+
22+
assertTrue(i3.tag.isPresent());
23+
assertEquals(i3.tag.get(), "124");
24+
assertEquals(i3.repository.name, "10.0.0.1:123/jim");
25+
assertEquals(i3.repository.getURL().getPort(), 123);
26+
assertEquals(i3A.tag.get(), "latest");
27+
28+
29+
Identifier i4 = Identifier.fromCompoundString("centos:latest");
30+
assertTrue(i4.tag.isPresent());
31+
assertEquals(i4.tag.get(), "latest");
32+
33+
Identifier i5 = Identifier.fromCompoundString("busybox");
34+
assertTrue(!i5.tag.isPresent());
35+
36+
Identifier i6 = Identifier.fromCompoundString("10.0.0.1:5000/my-test-image:1234");
37+
assertEquals(i6.repository.getPath(), "my-test-image");
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import junit.framework.TestCase;
4+
5+
public class RepositoryTest extends TestCase {
6+
public void testRepository() throws Exception {
7+
8+
Repository repo = new Repository("10.0.0.1/jim");
9+
Repository repo1 = new Repository("10.0.0.1:1234/jim");
10+
Repository repo2 = new Repository("busybox");
11+
12+
assertEquals("jim", repo.getPath());
13+
assertEquals("jim", repo1.getPath());
14+
assertEquals("busybox", repo2.getPath());
15+
16+
assertEquals(1234, repo1.getURL().getPort());
17+
}
18+
}

0 commit comments

Comments
 (0)