1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.client.util;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.apache.chemistry.opencmis.client.api.CmisObject;
29 import org.apache.chemistry.opencmis.client.api.Document;
30 import org.apache.chemistry.opencmis.client.api.Folder;
31 import org.apache.chemistry.opencmis.client.api.Property;
32 import org.apache.chemistry.opencmis.client.api.Session;
33 import org.apache.chemistry.opencmis.commons.PropertyIds;
34 import org.apache.chemistry.opencmis.commons.data.ContentStream;
35 import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
36 import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
37 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
38 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
39 import org.apache.chemistry.opencmis.commons.impl.IOUtils;
40
41
42
43
44 public final class FileUtils {
45
46 private FileUtils() {
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static CmisObject getObject(String pathOrIdOfObject, Session session) {
62 if (session == null) {
63 throw new IllegalArgumentException("session must be set!");
64 }
65 if (pathOrIdOfObject == null || pathOrIdOfObject.length() == 0) {
66 throw new IllegalArgumentException("pathOrIdOfObject must be set!");
67 }
68
69 CmisObject result = null;
70 if (pathOrIdOfObject.charAt(0) == '/') {
71 result = session.getObjectByPath(pathOrIdOfObject);
72 } else {
73 result = session.getObject(pathOrIdOfObject);
74 }
75
76 return result;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static Folder getFolder(String pathOrIdOfObject, Session session) {
92 CmisObject folder = getObject(pathOrIdOfObject, session);
93
94 if (folder instanceof Folder) {
95 return (Folder) folder;
96 } else {
97 throw new IllegalArgumentException("Object is not a folder!");
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public static Document createDocumentFromFile(String parentIdOrPath, File file, String type,
120 VersioningState versioningState, Session session) throws FileNotFoundException {
121 if (type == null) {
122 type = BaseTypeId.CMIS_DOCUMENT.value();
123 }
124
125 Folder parentFolder = getFolder(parentIdOrPath, session);
126
127 String name = file.getName();
128
129 Map<String, Object> properties = new HashMap<String, Object>();
130 properties.put(PropertyIds.OBJECT_TYPE_ID, type);
131 properties.put(PropertyIds.NAME, name);
132
133 ContentStream contentStream = ContentStreamUtils.createFileContentStream(name, file);
134
135 try {
136 return parentFolder.createDocument(properties, contentStream, versioningState);
137 } finally {
138 IOUtils.closeQuietly(contentStream);
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public static Document createTextDocument(String parentIdOrPath, String name, String content, String type,
160 VersioningState versioningState, Session session) {
161 if (type == null) {
162 type = BaseTypeId.CMIS_DOCUMENT.value();
163 }
164
165 Folder parentFolder = getFolder(parentIdOrPath, session);
166
167 Map<String, Object> properties = new HashMap<String, Object>();
168 properties.put(PropertyIds.OBJECT_TYPE_ID, type);
169 properties.put(PropertyIds.NAME, name);
170
171 ContentStream contentStream = ContentStreamUtils.createTextContentStream(name, content);
172
173 try {
174 return parentFolder.createDocument(properties, contentStream, versioningState);
175 } finally {
176 IOUtils.closeQuietly(contentStream);
177 }
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public static Folder createFolder(Folder parentFolder, String name, String type) {
197 if (type == null) {
198 type = BaseTypeId.CMIS_FOLDER.value();
199 }
200
201 Map<String, Object> properties = new HashMap<String, Object>();
202 properties.put(PropertyIds.OBJECT_TYPE_ID, type);
203 properties.put(PropertyIds.NAME, name);
204
205 return parentFolder.createFolder(properties);
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 public static Folder createFolder(String parentIdOrPath, String name, String type, Session session) {
226 Folder parentFolder = getFolder(parentIdOrPath, session);
227
228 if (type == null) {
229 type = BaseTypeId.CMIS_FOLDER.value();
230 }
231
232 Map<String, Object> properties = new HashMap<String, Object>();
233 properties.put(PropertyIds.OBJECT_TYPE_ID, type);
234 properties.put(PropertyIds.NAME, name);
235
236 return parentFolder.createFolder(properties);
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public static void download(Document doc, String destinationPath) throws IOException {
253 if (doc == null) {
254 return;
255 }
256
257 ContentStreamUtils.writeContentStreamToFile(doc.getContentStream(), new File(destinationPath));
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 public static void download(String docIdOrPath, String destinationPath, Session session) throws IOException {
276 CmisObject doc = getObject(docIdOrPath, session);
277
278 if (doc instanceof Document) {
279 download((Document) doc, destinationPath);
280 } else {
281 throw new IllegalArgumentException("Object is not a document!");
282 }
283 }
284
285
286
287
288
289
290
291
292
293
294
295
296 public static void delete(String pathOrIdOfObject, Session session) {
297 CmisObject object = getObject(pathOrIdOfObject, session);
298
299 if (object instanceof Folder) {
300 ((Folder) object).deleteTree(true, UnfileObject.DELETE, true);
301 } else {
302 object.delete(true);
303 }
304 }
305
306
307
308
309
310
311
312 public static void printProperties(CmisObject object) {
313 printProperties(object, System.out);
314 }
315
316
317
318
319
320
321
322
323 public static void printProperties(CmisObject object, PrintStream out) {
324 for (Property<?> prop : object.getProperties()) {
325 printProperty(prop, out);
326 }
327 }
328
329 public static void printProperty(Property<?> prop) {
330 printProperty(prop, System.out);
331 }
332
333 public static void printProperty(Property<?> prop, PrintStream out) {
334 out.println(prop.getId() + ": " + prop.getValuesAsString());
335 }
336 }