1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.webadmin.webdav.components; |
21 |
| |
22 |
| import java.io.BufferedInputStream; |
23 |
| import java.io.ByteArrayOutputStream; |
24 |
| import java.io.IOException; |
25 |
| import java.io.InputStream; |
26 |
| |
27 |
| import javax.servlet.ServletException; |
28 |
| import javax.servlet.http.HttpServletResponse; |
29 |
| |
30 |
| import org.apache.xindice.core.Collection; |
31 |
| import org.apache.xindice.core.DBException; |
32 |
| import org.apache.xindice.core.data.Key; |
33 |
| import org.apache.xindice.util.XindiceException; |
34 |
| import org.apache.xindice.webadmin.util.CollectionConfigurationHelper; |
35 |
| import org.apache.xindice.webadmin.webdav.WebdavStatus; |
36 |
| import org.apache.xindice.webadmin.webdav.DAVRequest; |
37 |
| import org.apache.xindice.webadmin.webdav.DAVResponse; |
38 |
| import org.apache.xindice.webadmin.Location; |
39 |
| import org.apache.xindice.xml.dom.DOMParser; |
40 |
| import org.apache.commons.logging.Log; |
41 |
| import org.apache.commons.logging.LogFactory; |
42 |
| import org.w3c.dom.Document; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| public class Put implements DAVComponent { |
53 |
| private static final Log log = LogFactory.getLog(Put.class); |
54 |
| |
55 |
| |
56 |
| protected static final int BUFFER_SIZE = 4096; |
57 |
| |
58 |
6
| public void execute(DAVRequest req, DAVResponse res, Location target) throws ServletException, IOException {
|
59 |
6
| if (target.isRoot()) {
|
60 |
0
| res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
|
61 |
0
| return;
|
62 |
| } |
63 |
| |
64 |
6
| if (target.getCollection() == null) {
|
65 |
| |
66 |
1
| res.setStatus(WebdavStatus.SC_CONFLICT);
|
67 |
5
| } else if (target.getName() == null) {
|
68 |
1
| if (log.isDebugEnabled()) {
|
69 |
0
| log.debug("Method Put is not implemented for collections: use Mkcol");
|
70 |
| } |
71 |
1
| res.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
|
72 |
| } else { |
73 |
4
| Collection col = target.getCollection();
|
74 |
4
| String name = target.getName();
|
75 |
4
| try {
|
76 |
4
| res.setStatus(storeResource(col, name, req.getInputStream()));
|
77 |
| } catch (DBException e) { |
78 |
0
| log.error("Failed to create resource " + name + " in the collection " + col.getCanonicalName(), e);
|
79 |
0
| throw new ServletException(e);
|
80 |
| } |
81 |
| } |
82 |
| } |
83 |
| |
84 |
4
| private int storeResource(Collection col, String name, InputStream input) throws IOException, DBException {
|
85 |
| |
86 |
4
| ByteArrayOutputStream rawBinCache = new ByteArrayOutputStream(10 * BUFFER_SIZE);
|
87 |
4
| BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
|
88 |
| |
89 |
4
| int bytesRead;
|
90 |
4
| byte[] transferBuffer = new byte[BUFFER_SIZE];
|
91 |
?
| while ((bytesRead = in.read(transferBuffer)) != -1) {
|
92 |
4
| rawBinCache.write(transferBuffer, 0, bytesRead);
|
93 |
| } |
94 |
4
| in.close();
|
95 |
4
| byte[] binCache = rawBinCache.toByteArray();
|
96 |
| |
97 |
4
| Document document = null;
|
98 |
4
| byte[] binary = null;
|
99 |
4
| try {
|
100 |
4
| document = DOMParser.toDocument(binCache);
|
101 |
| } catch (XindiceException e) { |
102 |
2
| binary = binCache;
|
103 |
| } |
104 |
| |
105 |
4
| if (document != null) {
|
106 |
| |
107 |
2
| if (col.setDocument(new Key(name), document)) {
|
108 |
1
| return HttpServletResponse.SC_CREATED;
|
109 |
| } else { |
110 |
1
| return HttpServletResponse.SC_NO_CONTENT;
|
111 |
| } |
112 |
2
| } else if (binary != null) {
|
113 |
| |
114 |
2
| if (!CollectionConfigurationHelper.isInlineMetaEnabled(col)) {
|
115 |
0
| if (log.isWarnEnabled()) {
|
116 |
0
| log.warn("Collection Inline Metadata is not enabled!");
|
117 |
| } |
118 |
0
| return WebdavStatus.SC_FORBIDDEN;
|
119 |
| } |
120 |
| |
121 |
2
| if (col.setBinary(new Key(name), binary)) {
|
122 |
1
| return HttpServletResponse.SC_CREATED;
|
123 |
| } else { |
124 |
1
| return WebdavStatus.SC_NO_CONTENT;
|
125 |
| } |
126 |
| } |
127 |
| |
128 |
0
| return WebdavStatus.SC_INTERNAL_SERVER_ERROR;
|
129 |
| } |
130 |
| |
131 |
| } |