1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.transport.servlet; |
11 | |
|
12 | |
import org.mule.util.IOUtils; |
13 | |
|
14 | |
import java.io.ByteArrayOutputStream; |
15 | |
import java.io.IOException; |
16 | |
import java.io.InputStream; |
17 | |
|
18 | |
import javax.servlet.ServletException; |
19 | |
import javax.servlet.http.HttpServlet; |
20 | |
import javax.servlet.http.HttpServletRequest; |
21 | |
import javax.servlet.http.HttpServletResponse; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class JarResourceServlet extends HttpServlet |
27 | |
{ |
28 | |
public static final String DEFAULT_PATH_SPEC = "/mule-resource/*"; |
29 | |
|
30 | |
public static final String DEFAULT_BASE_PATH = ""; |
31 | |
|
32 | |
public static final String DEFAULT_MIME_TYPE = "application/octet-stream"; |
33 | |
|
34 | 0 | private String basepath = DEFAULT_BASE_PATH; |
35 | |
|
36 | |
@Override |
37 | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException |
38 | |
{ |
39 | 0 | String file = getBasepath() + req.getPathInfo(); |
40 | |
|
41 | 0 | if(file.startsWith("/")) file = file.substring(1); |
42 | 0 | InputStream in = IOUtils.getResourceAsStream(file, getClass(), false, false); |
43 | 0 | if (in == null) |
44 | |
{ |
45 | 0 | resp.setStatus(HttpServletResponse.SC_NOT_FOUND); |
46 | 0 | resp.getWriter().write("Unable to find file: " + req.getPathInfo()); |
47 | 0 | return; |
48 | |
} |
49 | |
byte[] buffer; |
50 | |
try |
51 | |
{ |
52 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
53 | 0 | IOUtils.copyLarge(in, baos); |
54 | |
|
55 | 0 | buffer = baos.toByteArray(); |
56 | |
|
57 | 0 | String mimetype = DEFAULT_MIME_TYPE; |
58 | 0 | if (getServletContext() != null) |
59 | |
{ |
60 | 0 | String temp = getServletContext().getMimeType(file); |
61 | 0 | if (temp != null) |
62 | |
{ |
63 | 0 | mimetype = temp; |
64 | |
} |
65 | |
} |
66 | |
|
67 | 0 | resp.setContentType(mimetype); |
68 | 0 | resp.setContentLength(buffer.length); |
69 | 0 | resp.setHeader("Content-Disposition", "attachment; filename=\"" + req.getPathInfo() + "\""); |
70 | 0 | resp.getOutputStream().write(buffer); |
71 | |
} |
72 | |
finally |
73 | |
{ |
74 | 0 | in.close(); |
75 | 0 | resp.getOutputStream().flush(); |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | |
public String getBasepath() |
80 | |
{ |
81 | 0 | return basepath; |
82 | |
} |
83 | |
|
84 | |
public void setBasepath(String basepath) |
85 | |
{ |
86 | 0 | this.basepath = basepath; |
87 | 0 | } |
88 | |
} |