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   
6   import com.google.common.collect.ImmutableMap;
7   
8   import java.util.HashSet;
9   import java.util.Map;
10  import java.util.Set;
11  
12  /**
13   * Creates a batch of all like-typed resources that are declared as "super-batch="true"" in their plugin
14   * definitions.
15   * <p/>
16   * The URL for batch resources is /download/superbatch/&lt;type>/batch.&lt;type. The additional type part in the path
17   * is simply there to make the number of path-parts identical with other resources, so relative URLs will still work
18   * in CSS files.
19   */
20  public class SuperBatchPluginResource implements PluginResource
21  {
22      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + "superbatch" + PATH_SEPARATOR;
23      static final String DEFAULT_RESOURCE_NAME_PREFIX = "batch";
24  
25      private final ResourceKey resource;
26      private final Map<String, String> params;
27      private final Set<BatchedWebResourceDescriptor> batchedWebResourceDescriptors;
28  
29      public static SuperBatchPluginResource createBatchFor(final PluginResource pluginResource)
30      {
31          return new SuperBatchPluginResource(pluginResource.getType(), pluginResource.getParams());
32      }
33  
34      public SuperBatchPluginResource(final String type, final Map<String, String> params)
35      {
36          this(ResourceKey.Builder.batch(type), params);
37      }
38  
39      protected SuperBatchPluginResource(final ResourceKey resource, final Map<String, String> params)
40      {
41          this.resource = resource;
42          this.params = ImmutableMap.copyOf(params);
43          this.batchedWebResourceDescriptors = new HashSet<BatchedWebResourceDescriptor>();
44      }
45  
46      public String getUrl()
47      {
48          final StringBuilder buf = new StringBuilder(URL_PREFIX.length() + 20);
49          buf.append(URL_PREFIX).append(getType()).append(PATH_SEPARATOR).append(getResourceName());
50          ResourceUtils.addParamsToUrl(buf, getParams());
51          return buf.toString();
52      }
53  
54      public Map<String, String> getParams()
55      {
56          return params;
57      }
58  
59      public String getVersion(final WebResourceIntegration integration)
60      {
61          return integration.getSuperBatchVersion();
62      }
63  
64      public String getType()
65      {
66          return resource.suffix();
67      }
68  
69      public boolean isCacheSupported()
70      {
71          return true;
72      }
73  
74      public String getResourceName()
75      {
76          return resource.name();
77      }
78  
79      public String getModuleCompleteKey()
80      {
81          return "superbatch";
82      }
83      
84      void addBatchedWebResourceDescriptor(BatchedWebResourceDescriptor descriptor)
85      {
86          batchedWebResourceDescriptors.add(descriptor);
87      }
88      
89      @Override
90      public Set<BatchedWebResourceDescriptor> getBatchedWebResourceDescriptors()
91      {
92          return batchedWebResourceDescriptors;
93      }
94  
95      @Override
96      public String toString()
97      {
98          return "[Superbatch type=" + getType() + ", params=" + getParams() + "]";
99      }
100 }