View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.servlet.DownloadableResource;
4   
5   import java.util.Arrays;
6   import java.util.Map;
7   
8   import static com.atlassian.plugin.webresource.SuperBatchPluginResource.URL_PREFIX;
9   
10  /**
11   * Provides a fallback to serve resources relative to a super batch resource
12   * In practice, the resources url should be transformed via the
13   * {{com.atlassian.plugin.webresource.RelativeURLTransformResource}}.
14   * This builder is in place in case this does not happen
15   * @since 2.9.0
16   */
17  public class SuperBatchSubResourceBuilder implements DownloadableResourceBuilder
18  {
19      private final ResourceDependencyResolver dependencyResolver;
20      private final DownloadableResourceFinder resourceFinder;
21  
22      public SuperBatchSubResourceBuilder(ResourceDependencyResolver dependencyResolver, DownloadableResourceFinder resourceFinder)
23      {
24          this.dependencyResolver = dependencyResolver;
25          this.resourceFinder = resourceFinder;
26      }
27  
28      public boolean matches(String path)
29      {
30          return path.indexOf(URL_PREFIX) > -1;
31      }
32  
33      public DownloadableResource parse(String path, Map<String, String> params) throws UrlParseException
34      {
35          String resourceName = getResourceName(path);
36  
37          for (WebResourceModuleDescriptor moduleDescriptor : dependencyResolver.getSuperBatchDependencies())
38          {
39              DownloadableResource resource = resourceFinder.find(moduleDescriptor.getCompleteKey(), resourceName);
40  
41              if (resource != null)
42              {
43                  return resource;
44              }
45          }
46  
47          return null; // 404
48      }
49  
50      private String getResourceName(String path)
51      {
52          int startIndex = path.indexOf(URL_PREFIX) + URL_PREFIX.length();
53          return path.substring(startIndex);
54      }
55  }