1 package com.atlassian.plugin.servlet;
2
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import javax.servlet.ServletConfig;
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.io.*;
13 import java.util.*;
14
15
16
17
18 public abstract class BaseFileServerServlet extends HttpServlet
19 {
20
21 public static final String PATH_SEPARATOR = "/";
22 public static final String RESOURCE_URL_PREFIX = "resources";
23
24 private static List downloadStrategies = Collections.synchronizedList(new ArrayList());
25 private static final Log log = LogFactory.getLog(BaseFileServerServlet.class);
26
27 static
28 {
29 downloadStrategies.add(PluginResourceDownload.class);
30 }
31
32 public static String SERVLET_PATH = "download";
33
34
35
36 public void init() throws ServletException
37 {
38 super.init();
39 }
40
41 public void init(ServletConfig servletConfig) throws ServletException
42 {
43 super.init(servletConfig);
44 }
45
46 public String getMimeType(File fileToServe)
47 {
48 return getServletContext().getMimeType(fileToServe.getAbsolutePath());
49 }
50
51
52
53
54 public void serveFileImpl(HttpServletResponse httpServletResponse, InputStream in) throws IOException
55 {
56 ResourceDownloadUtils.serveFileImpl(httpServletResponse, in);
57 }
58
59 public abstract String getDecodedPathInfo(HttpServletRequest httpServletRequest);
60
61 protected abstract DownloadStrategy instantiateDownloadStrategy(Class downloadStrategyClass);
62
63 protected abstract String urlDecode(String url);
64
65 protected abstract String getContentType(String location);
66
67 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
68 throws ServletException, IOException
69 {
70
71 try
72 {
73 DownloadStrategy downloadStrategy = getDownloadStrategy(httpServletRequest);
74
75 if (downloadStrategy != null)
76 {
77 downloadStrategy.serveFile(this, httpServletRequest, httpServletResponse);
78 }
79 else
80 {
81 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "The file you were looking for was not found");
82 }
83 }
84 catch (Throwable t)
85 {
86 log.info("Error while serving file ", t);
87 throw new ServletException(t);
88 }
89 }
90
91 protected void addDownloadStrategy(Class strategyClass)
92 {
93 downloadStrategies.add(strategyClass);
94 }
95
96 private DownloadStrategy getDownloadStrategy(HttpServletRequest httpServletRequest)
97 {
98 String url = httpServletRequest.getRequestURI().toLowerCase();
99 for (Iterator iterator = downloadStrategies.iterator(); iterator.hasNext();)
100 {
101 Class downloadStrategyClass = (Class) iterator.next();
102 DownloadStrategy downloadStrategy = instantiateDownloadStrategy(downloadStrategyClass);
103 if (downloadStrategy.matches(url))
104 {
105 return downloadStrategy;
106 }
107 }
108
109 return null;
110 }
111
112 }