View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.elements.ResourceLocation;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.servlet.util.LastModifiedHandler;
6   
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   import java.io.IOException;
10  import java.util.Date;
11  
12  public abstract class AbstractDownloadableResource implements DownloadableResource
13  {
14      protected ResourceLocation resourceLocation;
15      protected String extraPath;
16      protected Plugin plugin;
17      protected BaseFileServerServlet servlet;
18  
19      public AbstractDownloadableResource(BaseFileServerServlet servlet, Plugin plugin, ResourceLocation resourceLocation, String extraPath)
20      {
21          if (extraPath != null && !"".equals(extraPath.trim()) && !resourceLocation.getLocation().endsWith("/"))
22          {
23              extraPath = "/" + extraPath;
24          }
25  
26          this.resourceLocation = resourceLocation;
27          this.extraPath = extraPath;
28          this.plugin = plugin;
29          this.servlet = servlet;
30      }
31  
32  
33      protected String getContentType()
34      {
35          if (resourceLocation.getContentType() == null)
36          {
37              return servlet.getContentType(getLocation());
38          }
39  
40          return resourceLocation.getContentType();
41      }
42  
43      protected String getLocation()
44      {
45          return resourceLocation.getLocation() + extraPath;
46      }
47  
48      public String toString()
49      {
50          return "Resource: " + getLocation() + " (" + getContentType() + ")";
51      }
52  
53      public abstract void serveResource(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException;
54  
55      /**
56       * Checks any "If-Modified-Since" header from the request against the plugin's loading time, since plugins can't
57       * be modified after they've been loaded this is a good way to determine if a plugin resource has been modified
58       * or not.
59       *
60       * If this method returns true, don't do any more processing on the request -- the response code has already been
61       * set to "304 Not Modified" for you, and you don't need to serve the file.
62       */
63      protected boolean checkResourceNotModified(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
64      {
65          Date resourceLastModifiedDate = (plugin.getDateLoaded() == null) ? new Date() : plugin.getDateLoaded();
66          LastModifiedHandler lastModifiedHandler = new LastModifiedHandler(resourceLastModifiedDate);
67          return lastModifiedHandler.checkRequest(httpServletRequest, httpServletResponse);
68      }
69  }