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   * @since 2.2
15   */
16  public class SinglePluginResource implements PluginResource
17  {
18      /**
19       * The url prefix to a single plugin resource: "/download/resources/"
20       */
21      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + RESOURCE_URL_PREFIX;
22  
23      final private String resourceName;
24      final private String moduleCompleteKey;
25      final private boolean cached;
26  
27      public SinglePluginResource(String resourceName, String moduleCompleteKey, boolean cached)
28      {
29          this.resourceName = resourceName;
30          this.moduleCompleteKey = moduleCompleteKey;
31          this.cached = cached;
32      }
33  
34      public String getResourceName()
35      {
36          return resourceName;
37      }
38  
39      public String getModuleCompleteKey()
40      {
41          return moduleCompleteKey;
42      }
43  
44      public Map<String, String> getParams()
45      {
46          return Collections.EMPTY_MAP;
47      }
48  
49      public boolean isCacheSupported()
50      {
51          return cached;
52      }
53  
54      /**
55       * Returns a url string in the format: /download/resources/MODULE_COMPLETE_KEY/RESOURCE_NAME
56       *
57       * e.g. /download/resources/example.plugin:webresources/foo.css
58       */
59      public String getUrl()
60      {
61          return URL_PREFIX + PATH_SEPARATOR + moduleCompleteKey + PATH_SEPARATOR + resourceName;
62      }
63  
64      public static boolean matches(String url)
65      {
66          return url.indexOf(URL_PREFIX) != -1;
67      }
68  
69      /**
70       * Parses the given url and query parameter map into a SinglePluginResource. Query paramters must be
71       * passed in through the map, any in the url String will be ignored.
72       * @param url the url to parse
73       * @param queryParams a map of String key and value pairs representing the query parameters in the url
74       */
75      public static SinglePluginResource parse(String url)
76      {
77          int indexOfPrefix = url.indexOf(SinglePluginResource.URL_PREFIX);
78          String libraryAndResource = url.substring(indexOfPrefix + SinglePluginResource.URL_PREFIX.length() + 1);
79  
80          if (libraryAndResource.indexOf('?') != -1) // remove query parameters
81          {
82              libraryAndResource = libraryAndResource.substring(0, libraryAndResource.indexOf('?'));
83          }
84  
85          String[] parts = libraryAndResource.split("/", 2);
86  
87          if (parts.length != 2)
88              return null;
89  
90          return new SinglePluginResource(parts[1], parts[0], url.substring(0, indexOfPrefix).length() > 0);
91      }
92  }