View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import static com.atlassian.plugin.webresource.BatchPluginResource.URL_PREFIX;
4   
5   import com.atlassian.plugin.PluginAccessor;
6   import com.atlassian.plugin.cache.filecache.FileCache;
7   import com.atlassian.plugin.servlet.DownloadableResource;
8   
9   import com.atlassian.plugin.webresource.cache.CacheHandle;
10  import com.atlassian.plugin.webresource.cache.FileCacheKey;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import java.util.Map;
15  
16  /**
17   * Produces a batch containing the resources defined in a single web resource module descriptor
18   * @since 2.9.0
19   */
20  class SingleBatchDownloadableResourceBuilder extends AbstractBatchResourceBuilder
21  {
22      private static final Logger log = LoggerFactory.getLogger(SingleBatchDownloadableResourceBuilder.class);
23  
24      public SingleBatchDownloadableResourceBuilder(final PluginAccessor pluginAccessor, final WebResourceUrlProvider webResourceUrlProvider,
25                                                    final DownloadableResourceFinder resourceFinder, final FileCache<FileCacheKey> cache)
26      {
27          super(pluginAccessor, webResourceUrlProvider, resourceFinder, cache);
28      }
29  
30      public boolean matches(final String path)
31      {
32          return path.indexOf(URL_PREFIX) > -1;
33      }
34  
35      public DownloadableResource parse(final String path, final Map<String, String> params) throws UrlParseException
36      {
37          final String type = ResourceUtils.getType(path);
38          final int startIndex = path.indexOf(URL_PREFIX) + URL_PREFIX.length() + 1;
39          final String typeAndModuleKey = path.substring(startIndex);
40          final String[] parts = typeAndModuleKey.split("/", 2);
41  
42          if (parts.length < 2)
43          {
44              throw new UrlParseException("Could not parse invalid batch resource url: " + path);
45          }
46          final String moduleKey = parts[0];
47          final String resourceName = parts[1];
48          final BatchDownloadableResource batchResource = new BatchDownloadableResource(moduleKey, type, params,
49                  resolve(moduleKey, type, params),
50                  CacheHandle.Builder.forRequest(cache, "singlebatch", path, params));
51  
52          if (log.isDebugEnabled())
53          {
54              log.debug(batchResource.toString());
55          }
56  
57          if (batchResource.isEmpty())
58          {
59              return getResourceFinder().find(moduleKey, resourceName);
60          }
61          return batchResource;
62      }
63  }