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 java.util.Collections;
7   import java.util.HashSet;
8   import java.util.List;
9   import java.util.Map;
10  import java.util.Set;
11  
12  import com.google.common.collect.ImmutableMap;
13  
14  /**
15   * Represents a batch of all resources that declare themselves as part of a given context(s).
16   * <p/>
17   * The URL for batch resources is /download/contextbatch/&lt;type>/&lt;contextname>/batch.&lt;type. The additional type part in the path
18   * is simply there to make the number of path-parts identical with other resources, so relative URLs will still work
19   * in CSS files.
20   * <p/>
21   * The &lt;contextname&gt; part has the following syntax - <br/>
22   * &lt;includedContext1&gt;,&lt;includedContext2&gt;,-&lt;excludedContext1&gt;,-&lt;excludedContext2&gt;
23   * <br/>
24   * To clarify, multiple comma separated contexts should be combined with any contexts preceded by a '-' to be excluded.
25   *
26   * @since 2.9.0
27   */
28  class ContextBatchPluginResource implements PluginResource
29  {
30      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + "contextbatch" + PATH_SEPARATOR;
31  
32      private final ResourceKey resource;
33      private final String key;
34      private final String hash;
35      private final List<String> contexts;
36      private final Iterable<String> excludedContexts;    
37      private final Map<String, String> params;
38      private final Set<BatchedWebResourceDescriptor> batchedWebResourceDescriptors;
39  
40      ContextBatchPluginResource(final String key, List<String> contexts, final Iterable<String> excludedContexts,
41              final String hash, final String type, final Map<String, String> params, Set<BatchedWebResourceDescriptor> batchedWebResourceDescriptor)
42      {
43          resource = ResourceKey.Builder.batch(type);
44          this.params = ImmutableMap.copyOf(params);
45          this.key = key;
46          this.hash = hash;
47          this.contexts = contexts;
48          this.excludedContexts = excludedContexts;
49          this.batchedWebResourceDescriptors = Collections.unmodifiableSet(batchedWebResourceDescriptor);
50      }
51      
52      ContextBatchPluginResource(final String key, final String hash, final String type, final Map<String, String> params, Set<BatchedWebResourceDescriptor> batchedWebResourceDescriptor)
53      {
54          this(key, Collections.<String>emptyList(), Collections.<String>emptyList(), hash, type, params, batchedWebResourceDescriptor);
55      }
56  
57      Iterable<String> getContexts()
58      {
59          return contexts;
60      }
61  
62      public Iterable<String> getExcludedContexts()
63      {
64          return excludedContexts;
65      }    
66      
67      public String getUrl()
68      {
69          final StringBuilder buf = new StringBuilder(URL_PREFIX.length() + 20);
70          buf.append(URL_PREFIX).append(getType()).append(PATH_SEPARATOR).append(key).append(PATH_SEPARATOR).append(getResourceName());
71          ResourceUtils.addParamsToUrl(buf, getParams());
72          return buf.toString();
73      }
74  
75      public Map<String, String> getParams()
76      {
77          return params;
78      }
79      
80      public String getVersion(final WebResourceIntegration integration)
81      {
82          return hash;
83      }
84  
85      public String getType()
86      {
87          return resource.suffix();
88      }
89  
90      public boolean isCacheSupported()
91      {
92          return true;
93      }
94  
95      public String getResourceName()
96      {
97          return resource.name();
98      }
99  
100     public String getModuleCompleteKey()
101     {
102         return "contextbatch-" + resource.name();
103     }
104         
105     @Override
106     public Set<BatchedWebResourceDescriptor> getBatchedWebResourceDescriptors()
107     {
108         return batchedWebResourceDescriptors;
109     }
110 
111     @Override
112     public String toString()
113     {
114         return "[Context Batch type=" + getType() + ", params=" + params + "]";
115     }
116 }