1   package com.atlassian.plugins.rest.module;
2   
3   import com.google.common.base.Preconditions;
4   import org.apache.commons.lang.StringUtils;
5   
6   public class RestApiContext
7   {
8       public static final String SLASH = "/";
9   
10      public final static String LATEST = SLASH + "latest";
11  
12      public final static String ANY_PATH_PATTERN = "/*";
13  
14      private final String restContext;
15      private final String apiPath;
16      private final ApiVersion version;
17  
18      public RestApiContext(String restContext, String apiContext, ApiVersion version)
19      {
20          this.restContext = prependSlash(Preconditions.checkNotNull(restContext));
21          this.apiPath = prependSlash(Preconditions.checkNotNull(apiContext));
22          this.version = Preconditions.checkNotNull(version);
23      }
24  
25      /**
26       * @return the REST context, always starts with "/".
27       */
28      public String getRestContext()
29      {
30          return restContext;
31      }
32  
33      /**
34       * @return the API path, always starts with "/"
35       */
36      public String getApiPath()
37      {
38          return apiPath;
39      }
40  
41      /**
42       * @return the API version
43       */
44      public ApiVersion getVersion()
45      {
46          return version;
47      }
48  
49      public String getPathToVersion()
50      {
51          return getPathToVersion(SLASH + version);
52      }
53  
54      public String getPathToLatest()
55      {
56          return getPathToVersion(LATEST);
57      }
58  
59      public String getPathToVersion(String version)
60      {
61          return restContext + apiPath + version;
62      }
63  
64      private String prependSlash(String path)
65      {
66          return StringUtils.startsWith(path, SLASH) ? path : SLASH + path;
67      }
68  }