View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.google.common.collect.ImmutableMap;
5   import com.google.common.collect.ImmutableSet;
6   
7   import java.util.Map;
8   import java.util.Set;
9   
10  import static com.atlassian.plugin.servlet.AbstractFileServerServlet.PATH_SEPARATOR;
11  import static com.atlassian.plugin.servlet.AbstractFileServerServlet.SERVLET_PATH;
12  
13  /**
14   * Represents a batch of plugin resources. <p/>
15   * <p/>
16   * It provides methods to parse and generate urls to locate a batch of plugin resources. <p/>
17   * <p/>
18   * @since 2.2
19   */
20  public class BatchPluginResource implements PluginResource
21  {
22      /**
23       * The url prefix for a batch of plugin resources: "/download/batch/"
24       */
25      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + "batch";
26  
27      private final ResourceKey resource;
28      private final Map<String, String> params;
29  
30      // not thread-safe but a safe race on setting as hash function is referentially transparent
31      private int hash = 0;
32  
33      private final BatchedWebResourceDescriptor batchedWebResourceDescriptor;
34      
35      /**
36       * This constructor should only ever be used internally within this class. It does not ensure that the resourceName's
37       * file extension is the same as the given type. It is up to the calling code to ensure this.
38       *
39       * @param resource - the resource key
40       * @param params - the parameters of the resource (ieonly, media, etc)
41       */
42      BatchPluginResource(final ResourceKey resource, final Map<String, String> params, BatchedWebResourceDescriptor batchedWebResourceDescriptor)
43      {
44          this.resource = resource;
45          this.params = ImmutableMap.copyOf(params);
46          this.batchedWebResourceDescriptor = batchedWebResourceDescriptor;
47      }
48  
49      /**
50       * Returns a url string in the format: /download/batch/MODULE_COMPLETE_KEY/resourceName?PARAMS
51       * <p/>
52       * e.g. /download/batch/example.plugin:webresources/example.plugin:webresources.css?ie=true
53       * <p/>
54       * It is important for the url structure to be:
55       * 1. the same number of sectioned paths as the SinglePluginResource
56       * 2. include the module completey key in the path before the resource name
57       * This is due to css resources referencing other resources such as images in relative path forms.
58       */
59      public String getUrl()
60      {
61          final StringBuilder sb = new StringBuilder();
62          sb.append(URL_PREFIX).append(PATH_SEPARATOR).append(resource.key()).append(PATH_SEPARATOR).append(
63              resource.name());
64          ResourceUtils.addParamsToUrl(sb, params);
65          return sb.toString();
66      }
67  
68  
69      public String getResourceName()
70      {
71          return resource.name();
72      }
73  
74      public Map<String, String> getParams()
75      {
76          return params;
77      }
78  
79      public String getVersion(final WebResourceIntegration integration)
80      {
81          final Plugin plugin = integration.getPluginAccessor().getEnabledPluginModule(
82              getModuleCompleteKey()).getPlugin();
83          return plugin.getPluginInformation().getVersion();
84      }
85  
86      public String getModuleCompleteKey()
87      {
88          return resource.key();
89      }
90  
91      public boolean isCacheSupported()
92      {
93          return !"false".equals(params.get("cache"));
94      }
95  
96      @Override
97      public String getType()
98      {
99          return resource.suffix();
100     }
101 
102     @Override
103     public Set<BatchedWebResourceDescriptor> getBatchedWebResourceDescriptors()
104     {
105         return ImmutableSet.of(batchedWebResourceDescriptor);
106     }
107     
108     @Override
109     public boolean equals(final Object o)
110     {
111         if (this == o)
112         {
113             return true;
114         }
115         if ((o == null) || (getClass() != o.getClass()))
116         {
117             return false;
118         }
119 
120         final BatchPluginResource that = (BatchPluginResource) o;
121 
122         if (params != null ? !params.equals(that.params) : that.params != null)
123         {
124             return false;
125         }
126         return resource.equals(that.resource);
127         }
128 
129     @Override
130     public int hashCode()
131     {
132         if (hash == 0)
133         {
134             int result;
135             result = resource.hashCode();
136             result = 31 * result + (params != null ? params.hashCode() : 0);
137             hash = result;
138         }
139         return hash;
140     }
141 
142     @Override
143     public String toString()
144     {
145         return "[moduleCompleteKey=" + resource.key() + ", type=" + resource.suffix() + ", params=" + params + "]";
146     }
147 }