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   import com.atlassian.util.concurrent.LazyReference;
8   import com.google.common.collect.ImmutableMap;
9   
10  import java.util.Collections;
11  import java.util.Map;
12  
13  /**
14   * Represents a single plugin resource.
15   *
16   * It provides methods to parse and generate urls to locate a single plugin resource.
17   *
18   * Note: This PluginResource does not use it's parameters in generating the url. 
19   *
20   * @since 2.2
21   */
22  public class SinglePluginResource implements PluginResource
23  {
24      /**
25       * The url prefix to a single plugin resource: "/download/resources/"
26       */
27      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + RESOURCE_URL_PREFIX;
28  
29      private final String resourceName;
30      private final String moduleCompleteKey;
31      private final boolean cached;
32      private final Map<String, String> params;
33      private final LazyReference<String> type;
34  
35      public SinglePluginResource(final String resourceName, final String moduleCompleteKey, final boolean cached)
36      {
37          this(resourceName, moduleCompleteKey, cached, Collections.<String, String>emptyMap());
38      }
39  
40      public SinglePluginResource(final String resourceName, final String moduleCompleteKey, final boolean cached, final Map<String, String> params)
41      {
42          this.resourceName = resourceName;
43          this.moduleCompleteKey = moduleCompleteKey;
44          this.cached = cached;
45          this.params = ImmutableMap.copyOf(params);
46          this.type = new LazyReference<String>() {
47              @Override
48              protected String create() throws Exception
49              {
50                  return ResourceUtils.getType(resourceName);
51              }
52          };
53      }
54  
55      public String getResourceName()
56      {
57          return resourceName;
58      }
59  
60      public String getModuleCompleteKey()
61      {
62          return moduleCompleteKey;
63      }
64  
65      public Map<String, String> getParams()
66      {
67          return params;
68      }
69  
70      public String getVersion(WebResourceIntegration integration)
71      {
72          final Plugin plugin = integration.getPluginAccessor().getEnabledPluginModule(getModuleCompleteKey()).getPlugin();
73          return plugin.getPluginInformation().getVersion();
74      }
75  
76      public boolean isCacheSupported()
77      {
78          return cached;
79      }
80  
81      /**
82       * Returns a url string in the format: /download/resources/MODULE_COMPLETE_KEY/RESOURCE_NAME
83       *
84       * e.g. /download/resources/example.plugin:webresources/foo.css
85       */
86      public String getUrl()
87      {
88          return URL_PREFIX + PATH_SEPARATOR + moduleCompleteKey + PATH_SEPARATOR + resourceName;
89      }
90  
91      @Override
92      public String getType()
93      {
94          return type.get();
95      }
96  }