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