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.util; |
21 |
| |
22 |
| import org.apache.xindice.core.Collection; |
23 |
| import org.apache.xindice.core.DBException; |
24 |
| import org.apache.xindice.core.FaultCodes; |
25 |
| import org.apache.xindice.core.data.Key; |
26 |
| import org.apache.xindice.core.data.Entry; |
27 |
| import org.apache.xindice.webadmin.webdav.WebdavStatus; |
28 |
| import org.apache.xindice.webadmin.Location; |
29 |
| import org.apache.xindice.util.Configuration; |
30 |
| import org.apache.commons.logging.Log; |
31 |
| import org.apache.commons.logging.LogFactory; |
32 |
| import org.w3c.dom.Document; |
33 |
| |
34 |
| import java.util.HashMap; |
35 |
| import java.util.Map; |
36 |
| import java.util.Collections; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| public class DAVOperations { |
45 |
| |
46 |
| private static final Log log = LogFactory.getLog(DAVOperations.class); |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
5
| public static Map copy(Collection col, String dest, boolean overwrite, boolean deep) throws DBException {
|
58 |
| |
59 |
5
| Location destLocation = new Location(dest);
|
60 |
5
| Collection destCollection = destLocation.getCollection();
|
61 |
5
| String destName = destLocation.getName();
|
62 |
5
| Map results = new HashMap();
|
63 |
| |
64 |
5
| if (destLocation.isRoot()) {
|
65 |
0
| results.put(dest, new Integer(WebdavStatus.SC_BAD_REQUEST));
|
66 |
0
| return results;
|
67 |
5
| } else if (destCollection == null) {
|
68 |
| |
69 |
| |
70 |
0
| results.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
|
71 |
0
| return results;
|
72 |
5
| } else if (deep && destCollection.getCanonicalName().startsWith(col.getCanonicalName())) {
|
73 |
| |
74 |
| |
75 |
2
| results.put(dest, new Integer(WebdavStatus.SC_FORBIDDEN));
|
76 |
2
| return results;
|
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
3
| if (destName != null) {
|
82 |
| |
83 |
3
| results.putAll(copyCollection(col, destCollection, destName, deep));
|
84 |
3
| if (results.size() == 0) {
|
85 |
3
| results.put(dest, new Integer(WebdavStatus.SC_CREATED));
|
86 |
| } |
87 |
| } else { |
88 |
| |
89 |
0
| if (overwrite) {
|
90 |
| |
91 |
0
| String name = destCollection.getName();
|
92 |
0
| Collection parent = destCollection.getParentCollection();
|
93 |
| |
94 |
0
| parent.dropCollection(destCollection);
|
95 |
0
| results.putAll(copyCollection(col, parent, name, deep));
|
96 |
0
| if (results.size() == 0) {
|
97 |
0
| results.put(dest, new Integer(WebdavStatus.SC_NO_CONTENT));
|
98 |
| } |
99 |
| } else { |
100 |
0
| results.put(dest, new Integer(WebdavStatus.SC_PRECONDITION_FAILED));
|
101 |
| } |
102 |
| |
103 |
| } |
104 |
| |
105 |
3
| return results;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
6
| public static int copy(Collection col, String name, String dest, boolean overwrite) throws DBException {
|
119 |
| |
120 |
6
| Location destLocation = new Location(dest);
|
121 |
6
| Collection destCollection = destLocation.getCollection();
|
122 |
6
| String destName = destLocation.getName();
|
123 |
| |
124 |
6
| if (destLocation.isRoot()) {
|
125 |
| |
126 |
0
| return WebdavStatus.SC_FORBIDDEN;
|
127 |
6
| } else if (destCollection == null) {
|
128 |
2
| return WebdavStatus.SC_CONFLICT;
|
129 |
4
| } else if (col == destCollection && name.equals(destName)) {
|
130 |
0
| return WebdavStatus.SC_FORBIDDEN;
|
131 |
| } |
132 |
| |
133 |
| |
134 |
4
| Entry object = col.getEntry(name);
|
135 |
4
| if (object == null) {
|
136 |
0
| return WebdavStatus.SC_NOT_FOUND;
|
137 |
| } |
138 |
| |
139 |
4
| int status;
|
140 |
4
| try {
|
141 |
4
| if (object.getEntryType() == Entry.BINARY) {
|
142 |
0
| if (overwrite) {
|
143 |
0
| if (destCollection.setBinary(new Key(destName), (byte[]) object.getValue())) {
|
144 |
0
| status = WebdavStatus.SC_CREATED;
|
145 |
| } else { |
146 |
0
| status = WebdavStatus.SC_NO_CONTENT;
|
147 |
| } |
148 |
| } else { |
149 |
0
| destCollection.insertBinary(destName, (byte[]) object.getValue());
|
150 |
0
| status = WebdavStatus.SC_CREATED;
|
151 |
| } |
152 |
4
| } else if (object.getEntryType() == Entry.DOCUMENT) {
|
153 |
4
| if (overwrite) {
|
154 |
2
| if (destCollection.setDocument(new Key(destName), (Document) object.getValue())) {
|
155 |
2
| status = WebdavStatus.SC_CREATED;
|
156 |
| } else { |
157 |
0
| status = WebdavStatus.SC_NO_CONTENT;
|
158 |
| } |
159 |
| } else { |
160 |
2
| destCollection.insertDocument(destName, (Document) object.getValue());
|
161 |
0
| status = WebdavStatus.SC_CREATED;
|
162 |
| } |
163 |
| } else { |
164 |
| |
165 |
0
| status = WebdavStatus.SC_FORBIDDEN;
|
166 |
| } |
167 |
| } catch (DBException e) { |
168 |
2
| if (e.faultCode == FaultCodes.COL_DUPLICATE_RESOURCE) {
|
169 |
2
| status = WebdavStatus.SC_PRECONDITION_FAILED;
|
170 |
0
| } else if (e.faultCode == FaultCodes.COL_NO_FILER) {
|
171 |
0
| status = WebdavStatus.SC_FORBIDDEN;
|
172 |
0
| } else if (e.faultCode == FaultCodes.COL_COLLECTION_NOT_FOUND) {
|
173 |
0
| status = WebdavStatus.SC_CONFLICT;
|
174 |
0
| } else if (e.faultCode == FaultCodes.COL_CANNOT_STORE) {
|
175 |
0
| if (log.isDebugEnabled()) {
|
176 |
0
| log.debug("Collection Inline Metadata is not enabled!");
|
177 |
| } |
178 |
0
| status = WebdavStatus.SC_FORBIDDEN;
|
179 |
| } else { |
180 |
0
| throw e;
|
181 |
| } |
182 |
| } |
183 |
| |
184 |
4
| return status;
|
185 |
| } |
186 |
| |
187 |
3
| private static Map copyCollection(Collection col, Collection parent, String name, boolean deep)
|
188 |
| throws DBException { |
189 |
| |
190 |
3
| Configuration config = CollectionConfigurationHelper.copyConfiguration(name, col.getConfig());
|
191 |
3
| Collection newCol = parent.createCollection(name, config);
|
192 |
| |
193 |
3
| if (deep) {
|
194 |
2
| return copyCollectionContent(col, newCol);
|
195 |
| } else { |
196 |
1
| return Collections.EMPTY_MAP;
|
197 |
| } |
198 |
| } |
199 |
| |
200 |
4
| private static Map copyCollectionContent(Collection srcCol, Collection destCol) throws DBException {
|
201 |
4
| HashMap result = new HashMap();
|
202 |
| |
203 |
| |
204 |
4
| String[] resList = srcCol.listDocuments();
|
205 |
4
| for (int i = 0; i < resList.length; i++) {
|
206 |
200
| Entry object = srcCol.getEntry(resList[i]);
|
207 |
200
| if (object.getEntryType() == Entry.DOCUMENT) {
|
208 |
200
| destCol.setDocument(resList[i], (Document) object.getValue());
|
209 |
| } else { |
210 |
0
| destCol.setBinary(resList[i], (byte[]) object.getValue());
|
211 |
| } |
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
4
| String[] colList = srcCol.listCollections();
|
225 |
4
| for (int i = 0; i < colList.length; i++) {
|
226 |
2
| Collection srcChild = srcCol.getCollection(colList[i]);
|
227 |
2
| Collection destChild = destCol.getCollection(colList[i]);
|
228 |
2
| try {
|
229 |
2
| result.putAll(copyCollectionContent(srcChild, destChild));
|
230 |
| } catch (DBException e) { |
231 |
0
| int code;
|
232 |
0
| if (e.faultCode == FaultCodes.COL_NO_FILER) {
|
233 |
0
| code = WebdavStatus.SC_FORBIDDEN;
|
234 |
0
| } else if (e.faultCode == FaultCodes.COL_COLLECTION_NOT_FOUND) {
|
235 |
0
| code = WebdavStatus.SC_CONFLICT;
|
236 |
0
| } else if (e.faultCode == FaultCodes.COL_CANNOT_STORE) {
|
237 |
0
| if (log.isDebugEnabled()) {
|
238 |
0
| log.debug("Collection Inline Metadata is not enabled!");
|
239 |
| } |
240 |
0
| code = WebdavStatus.SC_FORBIDDEN;
|
241 |
| } else { |
242 |
0
| throw e;
|
243 |
| } |
244 |
| |
245 |
0
| result.put(destChild.getCanonicalName(), new Integer(code));
|
246 |
| } |
247 |
| } |
248 |
| |
249 |
4
| return result;
|
250 |
| } |
251 |
| } |