View Javadoc

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       * @return the REST context, always starts with "/".
34       */
35      public String getRestContext() {
36          return restContext;
37      }
38  
39      /**
40       * @return the API path, always starts with "/"
41       */
42      public String getApiPath() {
43          return apiPath;
44      }
45  
46      /**
47       * @return the API version
48       */
49      public ApiVersion getVersion() {
50          return version;
51      }
52  
53      /**
54       * @return String representing /restContext/apiPath/version, always starts with "/"
55       */
56      public String getPathToVersion() {
57          // if you say that you want to go versionless then you get the latest version
58          // (this is a point of indirection where we could potentially choose a version other than latest)
59          return getPathToVersion(version);
60      }
61  
62      /**
63       * @return String representing /restContext/apiPath/"latest", always starts with "/"
64       */
65      public String getPathToLatest() {
66          return getPathToVersion(LATEST);
67      }
68  
69      /**
70       * @param version ApiVersion such as "1.2.3" or "none"
71       * @return String representing /restContext/apiPath/version, always starts with "/"
72       */
73      public String getPathToVersion(String version) {
74          return restContext + getContextlessPathToVersion(version);
75      }
76  
77      /**
78       * @param version ApiVersion such as "1.2.3" or "none"
79       * @return String representing /restContext/apiPath/version, always starts with "/"
80       */
81      private String getPathToVersion(ApiVersion version) {
82          return restContext + getContextlessPathToVersion(version);
83      }
84  
85      /**
86       * @return String representing /apiPath/version, always starts with "/"
87       */
88      public String getContextlessPathToVersion() {
89          return getContextlessPathToVersion(version);
90      }
91  
92      /**
93       * @param version ApiVersion such as "1.2.3" or "none"
94       * @return String representing /apiPath/version, always starts with "/"
95       */
96      private String getContextlessPathToVersion(String version) {
97          return ApiVersion.isNone(version)
98                  ? apiPath
99                  : apiPath + prependSlash(version);
100     }
101 
102     /**
103      * Handles the version="none" case more efficiently than the overload that takes a String.
104      *
105      * @param version ApiVersion such as "1.2.3" or "none"
106      * @return String representing /apiPath/version, always starts with "/"
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 }