DownloadServlet.java
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.AccessDeniedException
* javax.jcr.Binary
* javax.jcr.Item
* javax.jcr.ItemNotFoundException
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.servlet.ServletException
* javax.servlet.ServletOutputStream
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.io.IOUtils
* org.apache.jackrabbit.util.Text
* org.osgi.framework.BundleContext
* org.slf4j.Logger
*/
package com.day.crx.delite.impl.servlets;
import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.jcr.AccessDeniedException;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.util.Text;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
public class DownloadServlet
extends AbstractServlet {
public DownloadServlet(BundleContext bc) {
super(bc);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doService(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
RequestData req = new RequestData(request);
String path = req.getParameter("path");
try {
Item item = session.getItem(path);
if (item.isNode()) {
res.sendError(501);
return;
}
InputStream in = null;
try {
Binary bin;
Property p = (Property)item;
if (p.isMultiple()) {
int index = Integer.parseInt(req.getParameter("index"));
bin = p.getValues()[index].getBinary();
} else {
bin = p.getBinary();
}
in = bin.getStream();
res.setContentType("application/octet-stream");
res.addHeader("Content-Disposition", "attachment; filename=\"" + Text.escape((String)this.getDownloadName(p)) + "\"");
res.addHeader("Content-Length", String.valueOf(bin.getSize()));
IOUtils.copy((InputStream)in, (OutputStream)res.getOutputStream());
}
finally {
if (in != null) {
IOUtils.closeQuietly((InputStream)in);
}
}
}
catch (PathNotFoundException e) {
res.sendError(404);
}
catch (Exception e) {
this.logger.error("Error downloading: " + path, (Throwable)e);
res.sendError(500);
}
}
private String getDownloadName(Property p) throws AccessDeniedException, ItemNotFoundException, RepositoryException {
if (p.getName().equals("jcr:data")) {
return p.getParent().getName();
}
return p.getName();
}
}