View Javadoc
1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.elements.ResourceLocation;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   import javax.servlet.ServletContext;
9   import java.io.InputStream;
10  
11  /**
12   * A {@link DownloadableResource} that will serve the resource via the web application's {@link ServletContext}.
13   */
14  public class DownloadableWebResource extends AbstractDownloadableResource {
15      private static final Logger log = LoggerFactory.getLogger(DownloadableWebResource.class);
16  
17      private final ServletContext servletContext;
18  
19      public DownloadableWebResource(Plugin plugin, ResourceLocation resourceLocation, String extraPath, ServletContext servletContext, boolean disableMinification) {
20          super(plugin, resourceLocation, extraPath, disableMinification);
21          this.servletContext = servletContext;
22      }
23  
24      @Override
25      protected InputStream getResourceAsStream(final String resourceLocation) {
26          String fixedResourceLocation = fixResourceLocation(resourceLocation);
27          return servletContext.getResourceAsStream(fixedResourceLocation);
28      }
29  
30      private String fixResourceLocation(final String resourceLocation) {
31          if (!resourceLocation.startsWith("/")) {
32              final String resourceLocationWithSlash = "/" + resourceLocation;
33              log.debug("ResourceLocation: {}, does not start with slash. Location was modified to: {}",
34                      resourceLocation, resourceLocationWithSlash);
35              return resourceLocationWithSlash;
36          }
37          return resourceLocation;
38      }
39  }