forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameParser.java
More file actions
100 lines (85 loc) · 3.45 KB
/
Copy pathNameParser.java
File metadata and controls
100 lines (85 loc) · 3.45 KB
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
/**
* Copyright (C) 2014 SignalFuse, Inc.
*/
package com.github.dockerjava.core;
import java.util.regex.Pattern;
import com.github.dockerjava.api.model.AuthConfig;
public class NameParser {
private static final Pattern VALID_HEX_PATTERN = Pattern.compile("^([a-f0-9]{64})$");
private static final Pattern VALID_NAMESPACE_PATTERN = Pattern.compile("^([a-z0-9_]{4,30})$");
private static final Pattern VALID_REPO_PATTERN = Pattern.compile("^([a-z0-9-_.]+)$");
public static ReposTag parseRepositoryTag(String name) {
int n = name.lastIndexOf(':');
if (n < 0) {
return new ReposTag(name, "");
}
String tag = name.substring(n + 1);
if (!tag.contains("/")) {
return new ReposTag(name.substring(0, n), tag);
}
return new ReposTag(name, "");
}
public static class ReposTag {
public final String repos;
public final String tag;
public ReposTag(String repos, String tag) {
this.repos = repos;
this.tag = tag;
}
}
public static void validateRepositoryName(String repositoryName) {
String name;
String namespace;
String[] nameParts = repositoryName.split("/", 2);
if (nameParts.length < 2) {
namespace = "library";
name = nameParts[0];
if (VALID_HEX_PATTERN.matcher(name).matches()) {
throw new InvalidRepositoryNameException(String.format(
"Invalid repository name (%s), cannot specify 64-byte hexadecimal strings",
name));
}
} else {
namespace = nameParts[0];
name = nameParts[1];
}
if (!VALID_NAMESPACE_PATTERN.matcher(namespace).matches()) {
throw new InvalidRepositoryNameException(
String.format(
"Invalid namespace name (%s), only [a-z0-9_] are allowed, size between 4 and 30",
namespace));
}
if (!VALID_REPO_PATTERN.matcher(name).matches()) {
throw new InvalidRepositoryNameException(String.format(
"Invalid repository name (%s), only [a-z0-9-_.] are allowed", name));
}
}
public static HostnameReposName resolveRepositoryName(String reposName) {
if (reposName.contains("://")) {
// It cannot contain a scheme!
throw new InvalidRepositoryNameException();
}
String[] nameParts = reposName.split("/", 2);
if (nameParts.length == 1
|| (!nameParts[0].contains(".") && !nameParts[0].contains(":") && !nameParts[0]
.equals("localhost"))) {
return new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, reposName);
}
String hostname = nameParts[0];
reposName = nameParts[1];
if (hostname.contains("index.docker.io")) {
throw new InvalidRepositoryNameException(String.format(
"Invalid repository name, try \"%s\" instead", reposName));
}
validateRepositoryName(reposName);
return new HostnameReposName(hostname, reposName);
}
public static class HostnameReposName {
public final String hostname;
public final String reposName;
public HostnameReposName(String hostname, String reposName) {
this.hostname = hostname;
this.reposName = reposName;
}
}
}