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