1 package com.atlassian.plugins.rest.module;
2
3 import com.google.common.base.Preconditions;
4 import org.apache.commons.lang.StringUtils;
5
6 import java.util.Set;
7
8 public class RestApiContext {
9 public static final String SLASH = "/";
10
11 public final static String LATEST = SLASH + "latest";
12
13 public final static String ANY_PATH_PATTERN = "/*";
14
15 private final String restContext;
16 private final String apiPath;
17 private final ApiVersion version;
18 private final Set<String> packages;
19
20 public RestApiContext(String restContext, String apiContext, ApiVersion version, Set<String> packages) {
21 this.restContext = prependSlash(Preconditions.checkNotNull(restContext));
22 this.apiPath = prependSlash(Preconditions.checkNotNull(apiContext));
23 this.version = Preconditions.checkNotNull(version);
24 this.packages = Preconditions.checkNotNull(packages);
25 }
26
27
28
29
30 public String getRestContext() {
31 return restContext;
32 }
33
34
35
36
37 public String getApiPath() {
38 return apiPath;
39 }
40
41
42
43
44 public ApiVersion getVersion() {
45 return version;
46 }
47
48
49
50
51 public String getPathToVersion() {
52
53
54 return getPathToVersion(version);
55 }
56
57
58
59
60 public String getPathToLatest() {
61 return getPathToVersion(LATEST);
62 }
63
64
65
66
67
68 public String getPathToVersion(String version) {
69 return restContext + getContextlessPathToVersion(version);
70 }
71
72
73
74
75
76 private String getPathToVersion(ApiVersion version) {
77 return restContext + getContextlessPathToVersion(version);
78 }
79
80
81
82
83 public String getContextlessPathToVersion() {
84 return getContextlessPathToVersion(version);
85 }
86
87
88
89
90
91 private String getContextlessPathToVersion(String version) {
92 return ApiVersion.isNone(version)
93 ? apiPath
94 : apiPath + prependSlash(version);
95 }
96
97
98
99
100
101
102
103 private String getContextlessPathToVersion(ApiVersion version) {
104 return version.isNone()
105 ? apiPath
106 : apiPath + prependSlash(version.toString());
107 }
108
109 private String prependSlash(String path) {
110 return StringUtils.startsWith(path, SLASH) ? path : SLASH + path;
111 }
112
113 public Set<String> getPackages() {
114 return packages;
115 }
116 }