This project has retired. For details please refer to its Attic page.
FileUtils xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A set of utility methods that simplify file and folder operations.
51   */
52  public class FileUtils {
53  
54      private FileUtils() {
55      }
56  
57      /**
58       * Gets an object by path or object id.
59       * 
60       * @param pathOrIdOfObject
61       *            the path or object id
62       * @param session
63       *            the session
64       * @return the object
65       * @throws CmisBaseException
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       * Gets a folder by path or object id.
87       * 
88       * @param pathOrIdOfObject
89       *            the path or folder id
90       * @param session
91       *            the session
92       * @return the folder object
93       * @throws CmisBaseException
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      * Creates a document from a file.
107      * 
108      * @param parentIdOrPath
109      *            the id or path of the parent folder
110      * @param file
111      *            the source file
112      * @param type
113      *            the document type (defaults to <code>cmis:document</code>)
114      * @param versioningState
115      *            the versioning state or <code>null</code>
116      * @return the newly created document
117      * @throws FileNotFoundException
118      * @throws CmisBaseException
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(); // "cmis:document";
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      * Creates a text document from a string.
143      * 
144      * @param parentIdOrPath
145      *            the id or path of the parent folder
146      * @param name
147      *            the document name
148      * @param content
149      *            the content string
150      * @param type
151      *            the document type (defaults to <code>cmis:document</code>)
152      * @param versioningState
153      *            the versioning state or <code>null</code>
154      * @param session
155      *            the session
156      * @return the newly created document
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(); // "cmis:document";
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      * Creates a child folder with the name specified of the type specified. If
179      * type is null then will default to cmis:folder.
180      * 
181      * @param parentFolder
182      *            the parent folder
183      * @param name
184      *            the folder name
185      * @param type
186      *            the folder type (defaults to <code>cmis:folder</code>)
187      * @return the newly created folder
188      * @throws CmisBaseException
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      * Creates a folder using a String identifier.
204      * 
205      * @param parentIdOrPath
206      *            the id or path of the parent folder
207      * @param name
208      *            the folder name
209      * @param type
210      *            the folder type (defaults to <code>cmis:folder</code>)
211      * @param session
212      *            the session
213      * @return the newly created folder
214      * @throws CmisBaseException
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      * Downloads the contentStream for the given doc to the specified path.
232      * 
233      * @param doc
234      *            the document
235      * @param destinationPath
236      *            the destination path
237      * @throws IOException
238      * @throws CmisBaseException
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      * Downloads a document by its id or path.
249      * 
250      * @param docIdOrPath
251      *            the id or path of the document
252      * @param destinationPath
253      *            the destination path
254      * @param session
255      *            the session
256      * @throws IOException
257      * @throws CmisBaseException
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      * Deletes an object by path or id (string identifier).
279      * 
280      * @param pathOrIdOfObject
281      *            the id or path of the object
282      * @param session
283      *            the session
284      * @throws CmisBaseException
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      * Prints out all of the properties for this object to System.out.
298      * 
299      * @param object
300      *            the object
301      */
302     public static void printProperties(CmisObject object) {
303         printProperties(object, System.out);
304     }
305 
306     /**
307      * Prints out all of the properties for this object to the given
308      * PrintStream.
309      * 
310      * @param object
311      *            the object
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 }