View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import static com.atlassian.plugin.servlet.AbstractFileServerServlet.PATH_SEPARATOR;
4   import static com.atlassian.plugin.servlet.AbstractFileServerServlet.SERVLET_PATH;
5   import static com.atlassian.plugin.servlet.AbstractFileServerServlet.RESOURCE_URL_PREFIX;
6   
7   import java.util.Map;
8   import java.util.Collections;
9   
10  /**
11   * Represents a single plugin resource.
12   *
13   * It provides methods to parse and generate urls to locate a single plugin resource.
14   *
15   * Note: This PluginResource does not use it's parameters in generating the url. 
16   *
17   * @since 2.2
18   */
19  public class SinglePluginResource implements PluginResource
20  {
21      /**
22       * The url prefix to a single plugin resource: "/download/resources/"
23       */
24      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + RESOURCE_URL_PREFIX;
25  
26      private final String resourceName;
27      private final String moduleCompleteKey;
28      private final boolean cached;
29      private final Map<String, String> params;
30  
31      public SinglePluginResource(String resourceName, String moduleCompleteKey, boolean cached)
32      {
33          this(resourceName, moduleCompleteKey, cached, Collections.EMPTY_MAP);
34      }
35  
36      public SinglePluginResource(String resourceName, String moduleCompleteKey, boolean cached, Map<String, String> params)
37      {
38          this.resourceName = resourceName;
39          this.moduleCompleteKey = moduleCompleteKey;
40          this.cached = cached;
41          this.params = params;
42      }
43  
44      public String getResourceName()
45      {
46          return resourceName;
47      }
48  
49      public String getModuleCompleteKey()
50      {
51          return moduleCompleteKey;
52      }
53  
54      public Map<String, String> getParams()
55      {
56          return Collections.unmodifiableMap(params);
57      }
58  
59      public boolean isCacheSupported()
60      {
61          return cached;
62      }
63  
64      /**
65       * Returns a url string in the format: /download/resources/MODULE_COMPLETE_KEY/RESOURCE_NAME
66       *
67       * e.g. /download/resources/example.plugin:webresources/foo.css
68       */
69      public String getUrl()
70      {
71          return URL_PREFIX + PATH_SEPARATOR + moduleCompleteKey + PATH_SEPARATOR + resourceName;
72      }
73  
74      public static boolean matches(String url)
75      {
76          return url.indexOf(URL_PREFIX) != -1;
77      }
78  
79      /**
80       * Parses the given url into a SinglePluginResource.
81       *
82       * @param url the url to parse
83       */
84      public static SinglePluginResource parse(String url)
85      {
86          int indexOfPrefix = url.indexOf(SinglePluginResource.URL_PREFIX);
87          String libraryAndResource = url.substring(indexOfPrefix + SinglePluginResource.URL_PREFIX.length() + 1);
88  
89          if (libraryAndResource.indexOf('?') != -1) // remove query parameters
90          {
91              libraryAndResource = libraryAndResource.substring(0, libraryAndResource.indexOf('?'));
92          }
93  
94          String[] parts = libraryAndResource.split("/", 2);
95  
96          if (parts.length != 2)
97              return null;
98  
99          return new SinglePluginResource(parts[1], parts[0], url.substring(0, indexOfPrefix).length() > 0);
100     }
101 }