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