View Javadoc

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.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   * A set of utility methods that simplify file and folder operations.
43   */
44  public final class FileUtils {
45  
46      private FileUtils() {
47      }
48  
49      /**
50       * Gets an object by path or object id.
51       * 
52       * @param pathOrIdOfObject
53       *            the path or object id
54       * @param session
55       *            the session
56       * @return the object
57       * 
58       * @throws CmisBaseException
59       *             if something go wrong, for example the object doesn't exist
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       * Gets a folder by path or object id.
81       * 
82       * @param pathOrIdOfObject
83       *            the path or folder id
84       * @param session
85       *            the session
86       * @return the folder object
87       * 
88       * @throws CmisBaseException
89       *             if something go wrong, for example the object doesn't exist
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      * Creates a document from a file.
103      * 
104      * @param parentIdOrPath
105      *            the id or path of the parent folder
106      * @param file
107      *            the source file
108      * @param type
109      *            the document type (defaults to <code>cmis:document</code>)
110      * @param versioningState
111      *            the versioning state or <code>null</code>
112      * @return the newly created document
113      * 
114      * @throws FileNotFoundException
115      *             if the file does not exist
116      * @throws CmisBaseException
117      *             if something go wrong, for example the object doesn't exist
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(); // "cmis:document";
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      * Creates a text document from a string.
144      * 
145      * @param parentIdOrPath
146      *            the id or path of the parent folder
147      * @param name
148      *            the document name
149      * @param content
150      *            the content string
151      * @param type
152      *            the document type (defaults to <code>cmis:document</code>)
153      * @param versioningState
154      *            the versioning state or <code>null</code>
155      * @param session
156      *            the session
157      * @return the newly created document
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(); // "cmis:document";
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      * Creates a child folder with the name specified of the type specified. If
182      * type is null then will default to cmis:folder.
183      * 
184      * @param parentFolder
185      *            the parent folder
186      * @param name
187      *            the folder name
188      * @param type
189      *            the folder type (defaults to <code>cmis:folder</code>)
190      * @return the newly created folder
191      * 
192      * @throws CmisBaseException
193      *             if something go wrong, for example the parent folder doesn't
194      *             exist
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      * Creates a folder using a String identifier.
210      * 
211      * @param parentIdOrPath
212      *            the id or path of the parent folder
213      * @param name
214      *            the folder name
215      * @param type
216      *            the folder type (defaults to <code>cmis:folder</code>)
217      * @param session
218      *            the session
219      * @return the newly created folder
220      * 
221      * @throws CmisBaseException
222      *             if something go wrong, for example the parent folder doesn't
223      *             exist
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      * Downloads the contentStream for the given doc to the specified path.
241      * 
242      * @param doc
243      *            the document
244      * @param destinationPath
245      *            the destination path
246      * 
247      * @throws IOException
248      *             if the download fails because of an IO problem
249      * @throws CmisBaseException
250      *             if something go wrong, for example the document doesn't exist
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      * Downloads a document by its id or path.
262      * 
263      * @param docIdOrPath
264      *            the id or path of the document
265      * @param destinationPath
266      *            the destination path
267      * @param session
268      *            the session
269      * 
270      * @throws IOException
271      *             if the download fails because of an IO problem
272      * @throws CmisBaseException
273      *             if something go wrong, for example the document doesn't exist
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      * Deletes an object by path or id (string identifier).
287      * 
288      * @param pathOrIdOfObject
289      *            the id or path of the object
290      * @param session
291      *            the session
292      * 
293      * @throws CmisBaseException
294      *             if something go wrong, for example the object doesn't exist
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      * Prints out all of the properties for this object to System.out.
308      * 
309      * @param object
310      *            the object
311      */
312     public static void printProperties(CmisObject object) {
313         printProperties(object, System.out);
314     }
315 
316     /**
317      * Prints out all of the properties for this object to the given
318      * PrintStream.
319      * 
320      * @param object
321      *            the object
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 }