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