View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.servlet.DownloadableResource;
4   import com.atlassian.plugin.servlet.DownloadException;
5   import static com.atlassian.plugin.servlet.AbstractFileServerServlet.PATH_SEPARATOR;
6   import static com.atlassian.plugin.servlet.AbstractFileServerServlet.SERVLET_PATH;
7   import static com.atlassian.plugin.util.EfficientStringUtils.endsWith;
8   
9   import javax.servlet.http.HttpServletRequest;
10  import javax.servlet.http.HttpServletResponse;
11  import java.io.OutputStream;
12  import java.util.Map;
13  
14  /**
15   * Creates a batch of all like-typed resources that are declared as "super-batch="true"" in their plugin
16   * definitions.
17   *
18   * The URL for batch resources is /download/superbatch/<type>/batch.<type. The additional type part in the path
19   * is simply there to make the number of path-parts identical with other resources, so relative URLs will still work
20   * in CSS files.
21   *
22   */
23  public class SuperBatchPluginResource implements DownloadableResource, BatchResource, PluginResource
24  {
25      static final String URL_PREFIX = PATH_SEPARATOR + SERVLET_PATH + PATH_SEPARATOR + "superbatch" + PATH_SEPARATOR;
26      static final String DEFAULT_RESOURCE_NAME_PREFIX = "batch";
27  
28      private final BatchPluginResource delegate;
29      private final String resourceName;
30  
31      public static boolean matches(String path)
32      {
33          String type = getType(path);
34          return path.indexOf(URL_PREFIX) != -1 && endsWith(path, DEFAULT_RESOURCE_NAME_PREFIX, ".", type);
35      }
36  
37      public static SuperBatchPluginResource createBatchFor(PluginResource pluginResource)
38      {
39          return new SuperBatchPluginResource(getType(pluginResource.getResourceName()), pluginResource.getParams());
40      }
41  
42      public static SuperBatchPluginResource parse(String path, Map<String, String> params)
43      {
44          String type = path.substring(path.lastIndexOf(".") + 1);
45          return new SuperBatchPluginResource(type, params);
46      }
47  
48      protected static String getType(String path)
49      {
50          int index = path.lastIndexOf('.');
51          if (index > -1 && index < path.length())
52              return path.substring(index + 1);
53  
54          return "";
55      }
56  
57      public SuperBatchPluginResource(String type, Map<String, String> params)
58      {
59          this(DEFAULT_RESOURCE_NAME_PREFIX + "." + type, type, params);
60      }
61  
62      protected SuperBatchPluginResource(String resourceName, String type, Map<String, String> params)
63      {
64          this.resourceName = resourceName;
65          this.delegate = new BatchPluginResource(null, type, params);
66      }
67  
68      public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
69      {
70          return delegate.isResourceModified(request, response);
71      }
72  
73      public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
74      {
75          delegate.serveResource(request, response);
76      }
77  
78      public void streamResource(OutputStream out) throws DownloadException
79      {
80          delegate.streamResource(out);
81      }
82  
83      public String getContentType()
84      {
85          return delegate.getContentType();
86      }
87  
88      public void add(DownloadableResource downloadableResource)
89      {
90          delegate.add(downloadableResource);
91      }
92  
93      public boolean isEmpty()
94      {
95          return delegate.isEmpty();
96      }
97  
98      public String getUrl()
99      {
100         StringBuilder buf = new StringBuilder(URL_PREFIX.length() + 20);
101         buf.append(URL_PREFIX).append(getType()).append(PATH_SEPARATOR).append(resourceName);
102         delegate.addParamsToUrl(buf, delegate.getParams());
103         return buf.toString();
104     }
105 
106     public Map<String, String> getParams()
107     {
108         return delegate.getParams();
109     }
110 
111     public String getVersion(WebResourceIntegration integration)
112     {
113         return integration.getSuperBatchVersion();
114     }
115 
116     public String getType()
117     {
118         return delegate.getType();
119     }
120 
121     public boolean isCacheSupported()
122     {
123         return true;
124     }
125 
126     public String getResourceName()
127     {
128         return resourceName;
129     }
130 
131     public String getModuleCompleteKey()
132     {
133         return "superbatch";
134     }
135 
136     @Override
137     public String toString()
138     {
139         return "[Superbatch name=" + resourceName + ", type=" + getType() + ", params=" + getParams() + "]";
140     }
141 }