1
2
3
4
5
6
7
8
9
10 package org.mule.transport.servlet;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.config.MuleProperties;
14 import org.mule.registry.RegistryMap;
15 import org.mule.util.IOUtils;
16 import org.mule.util.TemplateParser;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Map;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServlet;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28
29
30
31
32
33
34 public class JarResourceServlet extends HttpServlet
35 {
36 public static final String DEFAULT_PATH_SPEC = "/mule-resource/*";
37
38 public static final String DEFAULT_BASE_PATH = "";
39
40 public static final String DEFAULT_MIME_TYPE = "application/octet-stream";
41
42 private String basepath = DEFAULT_BASE_PATH;
43
44 private String[] templateExtensions = new String[] { "htm", "html", "xml", "json" };
45
46 private TemplateParser templateParser = TemplateParser.createAntStyleParser();
47
48 private MuleContext muleContext;
49
50 private Map<?, ?> properties;
51
52 @Override
53 public void init() throws ServletException
54 {
55 muleContext = (MuleContext) getServletContext().getAttribute(MuleProperties.MULE_CONTEXT_PROPERTY);
56
57
58 if (muleContext == null)
59 {
60 throw new ServletException("Property " + MuleProperties.MULE_CONTEXT_PROPERTY
61 + " not set on ServletContext");
62 }
63
64 properties = new RegistryMap(muleContext.getRegistry());
65 }
66
67 @Override
68 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
69 {
70 String file = getFile(request);
71
72 InputStream in = IOUtils.getResourceAsStream(file, getClass(), false, false);
73 if (in == null)
74 {
75 response.setStatus(HttpServletResponse.SC_NOT_FOUND);
76 response.getWriter().write("Unable to find file: " + request.getPathInfo());
77 return;
78 }
79
80 try
81 {
82 ByteArrayOutputStream baos = new ByteArrayOutputStream();
83 IOUtils.copyLarge(in, baos);
84 byte[] buffer = baos.toByteArray();
85
86 String mimetype = determineMimeType(file);
87 buffer = expandTemplates(buffer, mimetype);
88
89 response.setContentType(mimetype);
90 response.setContentLength(buffer.length);
91 if (mimetype.equals(DEFAULT_MIME_TYPE))
92 {
93 response.setHeader("Content-Disposition", "attachment; filename=\"" + request.getPathInfo() + "\"");
94 }
95 response.getOutputStream().write(buffer);
96 }
97 finally
98 {
99 in.close();
100 response.getOutputStream().flush();
101 }
102 }
103
104 protected String getFile(HttpServletRequest request)
105 {
106 String file = getBasepath() + request.getPathInfo();
107 if (file.startsWith("/"))
108 {
109 file = file.substring(1);
110 }
111 return file;
112 }
113
114 protected String determineMimeType(String file)
115 {
116 String mimetype = DEFAULT_MIME_TYPE;
117 if (getServletContext() != null)
118 {
119 String temp = getServletContext().getMimeType(file);
120 if (temp != null)
121 {
122 mimetype = temp;
123 }
124 }
125 return mimetype;
126 }
127
128 protected byte[] expandTemplates(byte[] buffer, String mimetype)
129 {
130
131 for (String extension : templateExtensions)
132 {
133 if (mimetype.endsWith(extension))
134 {
135 return templateParser.parse(properties, new String(buffer)).getBytes();
136 }
137 }
138 return buffer;
139 }
140
141 public String getBasepath()
142 {
143 return basepath;
144 }
145
146 public void setBasepath(String basepath)
147 {
148 this.basepath = basepath;
149 }
150 }