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
57
58
59
60
61
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 }