View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import static com.atlassian.plugin.util.EfficientStringUtils.endsWith;
4   import static com.atlassian.plugin.webresource.SuperBatchPluginResource.DEFAULT_RESOURCE_NAME_PREFIX;
5   import static com.atlassian.plugin.webresource.SuperBatchPluginResource.URL_PREFIX;
6   import static com.google.common.collect.Iterables.concat;
7   import static com.google.common.collect.Iterables.transform;
8   
9   import com.atlassian.plugin.PluginAccessor;
10  import com.atlassian.plugin.cache.filecache.FileCache;
11  import com.atlassian.plugin.servlet.DownloadableResource;
12  
13  import com.atlassian.plugin.webresource.cache.CacheHandle;
14  import com.atlassian.plugin.webresource.cache.FileCacheKey;
15  import com.google.common.base.Function;
16  
17  import javax.annotation.Nullable;
18  import java.util.Map;
19  
20  /**
21   * Produces a batch containing all the defined super batch dependencies
22   * @since 2.9.0
23   */
24  class SuperBatchDownloadableResourceBuilder extends AbstractBatchResourceBuilder
25  {
26      private final ResourceDependencyResolver dependencyResolver;
27  
28      public SuperBatchDownloadableResourceBuilder(final ResourceDependencyResolver dependencyResolver, final PluginAccessor pluginAccessor,
29                                                   final WebResourceUrlProvider webResourceUrlProvider, final DownloadableResourceFinder resourceFinder,
30                                                   final FileCache<FileCacheKey> cache)
31      {
32          super(pluginAccessor, webResourceUrlProvider, resourceFinder, cache);
33          this.dependencyResolver = dependencyResolver;
34      }
35  
36      public boolean matches(final String path)
37      {
38          final String type = ResourceUtils.getType(path);
39          return (path.indexOf(URL_PREFIX) != -1) && endsWith(path, DEFAULT_RESOURCE_NAME_PREFIX, ".", type);
40      }
41  
42      public SuperBatchDownloadableResource parse(final String path, final Map<String, String> params)
43      {
44          final String type = ResourceUtils.getType(path);
45          Iterable<DownloadableResource> resources = concat(transform(dependencyResolver.getSuperBatchDependencies(), new Function<WebResourceModuleDescriptor, Iterable<DownloadableResource>>() {
46              @Override
47              public Iterable<DownloadableResource> apply(@Nullable WebResourceModuleDescriptor moduleDescriptor) {
48                  return resolve(moduleDescriptor, type, params);
49              }
50          }));
51  
52          return new SuperBatchDownloadableResource(type, params, resources,
53                  CacheHandle.Builder.forRequest(cache, "superbatch", path, params));
54      }
55  }