This project has retired. For details please refer to its Attic page.
ObjectServiceImpl 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.bindings.spi.browser;
20  
21  import java.io.OutputStream;
22  import java.math.BigInteger;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
27  import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils;
28  import org.apache.chemistry.opencmis.commons.PropertyIds;
29  import org.apache.chemistry.opencmis.commons.data.Acl;
30  import org.apache.chemistry.opencmis.commons.data.AllowableActions;
31  import org.apache.chemistry.opencmis.commons.data.ContentStream;
32  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
33  import org.apache.chemistry.opencmis.commons.data.FailedToDeleteData;
34  import org.apache.chemistry.opencmis.commons.data.ObjectData;
35  import org.apache.chemistry.opencmis.commons.data.Properties;
36  import org.apache.chemistry.opencmis.commons.data.RenditionData;
37  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
38  import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
39  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
40  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
41  import org.apache.chemistry.opencmis.commons.impl.Constants;
42  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
43  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
44  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
45  import org.apache.chemistry.opencmis.commons.impl.dataobjects.FailedToDeleteDataImpl;
46  import org.apache.chemistry.opencmis.commons.spi.Holder;
47  import org.apache.chemistry.opencmis.commons.spi.ObjectService;
48  
49  /**
50   * Object Service Browser Binding client.
51   */
52  public class ObjectServiceImpl extends AbstractBrowserBindingService implements ObjectService {
53  
54      /**
55       * Constructor.
56       */
57      public ObjectServiceImpl(BindingSession session) {
58          setSession(session);
59      }
60  
61      public String createDocument(String repositoryId, Properties properties, String folderId,
62              ContentStream contentStream, VersioningState versioningState, List<String> policies, Acl addAces,
63              Acl removeAces, ExtensionsData extension) {
64          // build URL
65          UrlBuilder url = (folderId != null ? getObjectUrl(repositoryId, folderId) : getRepositoryUrl(repositoryId));
66  
67          // prepare form data
68          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_DOCUMENT, contentStream);
69          formData.addPropertiesParameters(properties);
70          formData.addParameter(Constants.PARAM_VERSIONIG_STATE, versioningState);
71          formData.addPoliciesParameters(policies);
72          formData.addAddAcesParameters(addAces);
73          formData.addRemoveAcesParameters(removeAces);
74  
75          // send and parse
76          HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
77              public void write(OutputStream out) throws Exception {
78                  formData.write(out);
79              }
80          });
81  
82          Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
83          ObjectData newObj = JSONConverter.convertObject(json);
84  
85          return (newObj == null ? null : newObj.getId());
86      }
87  
88      public String createDocumentFromSource(String repositoryId, String sourceId, Properties properties,
89              String folderId, VersioningState versioningState, List<String> policies, Acl addAces, Acl removeAces,
90              ExtensionsData extension) {
91          // build URL
92          UrlBuilder url = (folderId != null ? getObjectUrl(repositoryId, folderId) : getRepositoryUrl(repositoryId));
93  
94          // prepare form data
95          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_DOCUMENT_FROM_SOURCE);
96          formData.addParameter(Constants.PARAM_SOURCE_ID, sourceId);
97          formData.addPropertiesParameters(properties);
98          formData.addParameter(Constants.PARAM_VERSIONIG_STATE, versioningState);
99          formData.addPoliciesParameters(policies);
100         formData.addAddAcesParameters(addAces);
101         formData.addRemoveAcesParameters(removeAces);
102 
103         // send and parse
104         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
105             public void write(OutputStream out) throws Exception {
106                 formData.write(out);
107             }
108         });
109 
110         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
111         ObjectData newObj = JSONConverter.convertObject(json);
112 
113         return (newObj == null ? null : newObj.getId());
114     }
115 
116     public String createFolder(String repositoryId, Properties properties, String folderId, List<String> policies,
117             Acl addAces, Acl removeAces, ExtensionsData extension) {
118         // build URL
119         UrlBuilder url = getObjectUrl(repositoryId, folderId);
120 
121         // prepare form data
122         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_FOLDER);
123         formData.addPropertiesParameters(properties);
124         formData.addPoliciesParameters(policies);
125         formData.addAddAcesParameters(addAces);
126         formData.addRemoveAcesParameters(removeAces);
127 
128         // send and parse
129         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
130             public void write(OutputStream out) throws Exception {
131                 formData.write(out);
132             }
133         });
134 
135         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
136         ObjectData newObj = JSONConverter.convertObject(json);
137 
138         return (newObj == null ? null : newObj.getId());
139     }
140 
141     public String createRelationship(String repositoryId, Properties properties, List<String> policies, Acl addAces,
142             Acl removeAces, ExtensionsData extension) {
143         // build URL
144         UrlBuilder url = getRepositoryUrl(repositoryId);
145 
146         // prepare form data
147         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_RELATIONSHIP);
148         formData.addPropertiesParameters(properties);
149         formData.addPoliciesParameters(policies);
150         formData.addAddAcesParameters(addAces);
151         formData.addRemoveAcesParameters(removeAces);
152 
153         // send and parse
154         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
155             public void write(OutputStream out) throws Exception {
156                 formData.write(out);
157             }
158         });
159 
160         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
161         ObjectData newObj = JSONConverter.convertObject(json);
162 
163         return (newObj == null ? null : newObj.getId());
164     }
165 
166     public String createPolicy(String repositoryId, Properties properties, String folderId, List<String> policies,
167             Acl addAces, Acl removeAces, ExtensionsData extension) {
168         // build URL
169         UrlBuilder url = (folderId != null ? getObjectUrl(repositoryId, folderId) : getRepositoryUrl(repositoryId));
170 
171         // prepare form data
172         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_CREATE_POLICY);
173         formData.addPropertiesParameters(properties);
174         formData.addPoliciesParameters(policies);
175         formData.addAddAcesParameters(addAces);
176         formData.addRemoveAcesParameters(removeAces);
177 
178         // send and parse
179         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
180             public void write(OutputStream out) throws Exception {
181                 formData.write(out);
182             }
183         });
184 
185         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
186         ObjectData newObj = JSONConverter.convertObject(json);
187 
188         return (newObj == null ? null : newObj.getId());
189     }
190 
191     public AllowableActions getAllowableActions(String repositoryId, String objectId, ExtensionsData extension) {
192         // build URL
193         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_ALLOWABLEACTIONS);
194 
195         // read and parse
196         HttpUtils.Response resp = read(url);
197         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
198 
199         return JSONConverter.convertAllowableActions(json);
200     }
201 
202     public ObjectData getObject(String repositoryId, String objectId, String filter, Boolean includeAllowableActions,
203             IncludeRelationships includeRelationships, String renditionFilter, Boolean includePolicyIds,
204             Boolean includeAcl, ExtensionsData extension) {
205         // build URL
206         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_OBJECT);
207         url.addParameter(Constants.PARAM_FILTER, filter);
208         url.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
209         url.addParameter(Constants.PARAM_RELATIONSHIPS, includeRelationships);
210         url.addParameter(Constants.PARAM_RENDITION_FILTER, renditionFilter);
211         url.addParameter(Constants.PARAM_POLICY_IDS, includePolicyIds);
212         url.addParameter(Constants.PARAM_ACL, includeAcl);
213 
214         // read and parse
215         HttpUtils.Response resp = read(url);
216         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
217 
218         return JSONConverter.convertObject(json);
219     }
220 
221     public ObjectData getObjectByPath(String repositoryId, String path, String filter, Boolean includeAllowableActions,
222             IncludeRelationships includeRelationships, String renditionFilter, Boolean includePolicyIds,
223             Boolean includeAcl, ExtensionsData extension) {
224         // build URL
225         UrlBuilder url = getPathUrl(repositoryId, path, Constants.SELECTOR_OBJECT);
226         url.addParameter(Constants.PARAM_FILTER, filter);
227         url.addParameter(Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions);
228         url.addParameter(Constants.PARAM_RELATIONSHIPS, includeRelationships);
229         url.addParameter(Constants.PARAM_RENDITION_FILTER, renditionFilter);
230         url.addParameter(Constants.PARAM_POLICY_IDS, includePolicyIds);
231         url.addParameter(Constants.PARAM_ACL, includeAcl);
232 
233         // read and parse
234         HttpUtils.Response resp = read(url);
235         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
236 
237         return JSONConverter.convertObject(json);
238     }
239 
240     public Properties getProperties(String repositoryId, String objectId, String filter, ExtensionsData extension) {
241         // build URL
242         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_PROPERTIES);
243         url.addParameter(Constants.PARAM_FILTER, filter);
244 
245         // read and parse
246         HttpUtils.Response resp = read(url);
247         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
248 
249         return JSONConverter.convertProperties(json);
250     }
251 
252     public List<RenditionData> getRenditions(String repositoryId, String objectId, String renditionFilter,
253             BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
254         // build URL
255         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_RENDITIONS);
256         url.addParameter(Constants.PARAM_RENDITION_FILTER, renditionFilter);
257         url.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
258         url.addParameter(Constants.PARAM_SKIP_COUNT, skipCount);
259 
260         // read and parse
261         HttpUtils.Response resp = read(url);
262         List<Object> json = parseArray(resp.getStream(), resp.getCharset());
263 
264         return JSONConverter.convertRenditions(json);
265     }
266 
267     public ContentStream getContentStream(String repositoryId, String objectId, String streamId, BigInteger offset,
268             BigInteger length, ExtensionsData extension) {
269         ContentStreamImpl result = new ContentStreamImpl();
270 
271         // build URL
272         UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_CONTENT);
273         url.addParameter(Constants.PARAM_STREAM_ID, streamId);
274 
275         // get the content
276         HttpUtils.Response resp = HttpUtils.invokeGET(url, getSession(), offset, length);
277 
278         // check response code
279         if ((resp.getResponseCode() != 200) && (resp.getResponseCode() != 206)) {
280             throw convertStatusCode(resp.getResponseCode(), resp.getResponseMessage(), resp.getErrorContent(), null);
281         }
282 
283         result.setFileName(null);
284         result.setLength(resp.getContentLength());
285         result.setMimeType(resp.getContentTypeHeader());
286         result.setStream(resp.getStream());
287 
288         return result;
289     }
290 
291     public void updateProperties(String repositoryId, Holder<String> objectId, Holder<String> changeToken,
292             Properties properties, ExtensionsData extension) {
293         // we need an object id
294         if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
295             throw new CmisInvalidArgumentException("Object id must be set!");
296         }
297 
298         // build URL
299         UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
300 
301         // prepare form data
302         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_UPDATE_PROPERTIES);
303         formData.addPropertiesParameters(properties);
304         formData.addParameter(Constants.PARAM_CHANGE_TOKEN, (changeToken == null ? null : changeToken.getValue()));
305 
306         // send and parse
307         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
308             public void write(OutputStream out) throws Exception {
309                 formData.write(out);
310             }
311         });
312 
313         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
314         ObjectData newObj = JSONConverter.convertObject(json);
315 
316         objectId.setValue(newObj == null ? null : newObj.getId());
317 
318         if (changeToken != null && newObj.getProperties() != null) {
319             Object ct = newObj.getProperties().getProperties().get(PropertyIds.CHANGE_TOKEN);
320             changeToken.setValue(ct == null ? null : ct.toString());
321         }
322     }
323 
324     public void moveObject(String repositoryId, Holder<String> objectId, String targetFolderId, String sourceFolderId,
325             ExtensionsData extension) {
326         // we need an object id
327         if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
328             throw new CmisInvalidArgumentException("Object id must be set!");
329         }
330 
331         // build URL
332         UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
333 
334         // prepare form data
335         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_MOVE);
336         formData.addParameter(Constants.PARAM_TARGET_FOLDER_ID, targetFolderId);
337         formData.addParameter(Constants.PARAM_SOURCE_FOLDER_ID, sourceFolderId);
338 
339         // send and parse
340         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
341             public void write(OutputStream out) throws Exception {
342                 formData.write(out);
343             }
344         });
345 
346         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
347         ObjectData newObj = JSONConverter.convertObject(json);
348 
349         objectId.setValue(newObj == null ? null : newObj.getId());
350     }
351 
352     public void deleteObject(String repositoryId, String objectId, Boolean allVersions, ExtensionsData extension) {
353         // build URL
354         UrlBuilder url = getObjectUrl(repositoryId, objectId);
355 
356         // prepare form data
357         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_DELETE);
358         formData.addParameter(Constants.PARAM_ALL_VERSIONS, allVersions);
359 
360         // send
361         postAndConsume(url, formData.getContentType(), new HttpUtils.Output() {
362             public void write(OutputStream out) throws Exception {
363                 formData.write(out);
364             }
365         });
366     }
367 
368     public FailedToDeleteData deleteTree(String repositoryId, String folderId, Boolean allVersions,
369             UnfileObject unfileObjects, Boolean continueOnFailure, ExtensionsData extension) {
370         // build URL
371         UrlBuilder url = getObjectUrl(repositoryId, folderId);
372 
373         // prepare form data
374         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_DELETE_TREE);
375         formData.addParameter(Constants.PARAM_ALL_VERSIONS, allVersions);
376         formData.addParameter(Constants.PARAM_UNFILE_OBJECTS, unfileObjects);
377         formData.addParameter(Constants.PARAM_CONTINUE_ON_FAILURE, continueOnFailure);
378 
379         // send
380         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
381             public void write(OutputStream out) throws Exception {
382                 formData.write(out);
383             }
384         });
385 
386         if (!BigInteger.ZERO.equals(resp.getContentLength())) {
387             Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
388             return JSONConverter.convertFailedToDelete(json);
389         }
390 
391         return new FailedToDeleteDataImpl();
392     }
393 
394     public void setContentStream(String repositoryId, Holder<String> objectId, Boolean overwriteFlag,
395             Holder<String> changeToken, ContentStream contentStream, ExtensionsData extension) {
396         // we need an object id
397         if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
398             throw new CmisInvalidArgumentException("Object id must be set!");
399         }
400 
401         // build URL
402         UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
403 
404         // prepare form data
405         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_SET_CONTENT, contentStream);
406         formData.addParameter(Constants.PARAM_OVERWRITE_FLAG, overwriteFlag);
407         formData.addParameter(Constants.PARAM_CHANGE_TOKEN, (changeToken == null ? null : changeToken.getValue()));
408 
409         // send and parse
410         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
411             public void write(OutputStream out) throws Exception {
412                 formData.write(out);
413             }
414         });
415 
416         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
417         ObjectData newObj = JSONConverter.convertObject(json);
418 
419         objectId.setValue(newObj == null ? null : newObj.getId());
420 
421         if (changeToken != null && newObj.getProperties() != null) {
422             Object ct = newObj.getProperties().getProperties().get(PropertyIds.CHANGE_TOKEN);
423             changeToken.setValue(ct == null ? null : ct.toString());
424         }
425     }
426 
427     public void deleteContentStream(String repositoryId, Holder<String> objectId, Holder<String> changeToken,
428             ExtensionsData extension) {
429         // we need an object id
430         if ((objectId == null) || (objectId.getValue() == null) || (objectId.getValue().length() == 0)) {
431             throw new CmisInvalidArgumentException("Object id must be set!");
432         }
433 
434         // build URL
435         UrlBuilder url = getObjectUrl(repositoryId, objectId.getValue());
436 
437         // prepare form data
438         final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_DELETE_CONTENT);
439         formData.addParameter(Constants.PARAM_CHANGE_TOKEN, (changeToken == null ? null : changeToken.getValue()));
440 
441         // send and parse
442         HttpUtils.Response resp = post(url, formData.getContentType(), new HttpUtils.Output() {
443             public void write(OutputStream out) throws Exception {
444                 formData.write(out);
445             }
446         });
447 
448         Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
449         ObjectData newObj = JSONConverter.convertObject(json);
450 
451         objectId.setValue(newObj == null ? null : newObj.getId());
452 
453         if (changeToken != null && newObj.getProperties() != null) {
454             Object ct = newObj.getProperties().getProperties().get(PropertyIds.CHANGE_TOKEN);
455             changeToken.setValue(ct == null ? null : ct.toString());
456         }
457     }
458 }