View Javadoc

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