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.server.impl.browser;
20  
21  import org.apache.chemistry.opencmis.commons.PropertyIds;
22  import org.apache.chemistry.opencmis.commons.data.AllowableActions;
23  import org.apache.chemistry.opencmis.commons.data.ContentStream;
24  import org.apache.chemistry.opencmis.commons.data.FailedToDeleteData;
25  import org.apache.chemistry.opencmis.commons.data.ObjectData;
26  import org.apache.chemistry.opencmis.commons.data.Properties;
27  import org.apache.chemistry.opencmis.commons.data.PropertyData;
28  import org.apache.chemistry.opencmis.commons.data.RenditionData;
29  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
30  import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
31  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
32  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
33  import org.apache.chemistry.opencmis.commons.impl.Constants;
34  import static org.apache.chemistry.opencmis.commons.impl.Constants.MEDIATYPE_OCTETSTREAM;
35  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_ACL;
36  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_ALLOWABLE_ACTIONS;
37  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FILTER;
38  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_POLICY_IDS;
39  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_RELATIONSHIPS;
40  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_RENDITION_FILTER;
41  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_RETURN_VERSION;
42  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_SOURCE_FOLDER_ID;
43  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_SOURCE_ID;
44  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_STREAM_ID;
45  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_TARGET_FOLDER_ID;
46  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_TRANSACTION;
47  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_VERSIONIG_STATE;
48  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
49  import org.apache.chemistry.opencmis.commons.impl.MimeHelper;
50  import org.apache.chemistry.opencmis.commons.impl.ReturnVersion;
51  import org.apache.chemistry.opencmis.commons.impl.TypeCache;
52  import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
53  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
54  import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
55  import org.apache.chemistry.opencmis.commons.server.CallContext;
56  import org.apache.chemistry.opencmis.commons.server.CmisService;
57  import org.apache.chemistry.opencmis.commons.spi.Holder;
58  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_CONTENT;
59  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
60  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
61  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
62  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_TYPE_ID;
63  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createAddAcl;
64  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createContentStream;
65  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createCookieValue;
66  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createPolicies;
67  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createProperties;
68  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createRemoveAcl;
69  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
70  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setCookie;
71  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
72  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeEmpty;
73  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
74  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBigIntegerParameter;
75  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
76  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
77  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
78  
79  import java.io.BufferedInputStream;
80  import java.io.BufferedOutputStream;
81  import java.io.InputStream;
82  import java.io.OutputStream;
83  
84  import java.math.BigInteger;
85  
86  import java.util.List;
87  
88  import javax.servlet.http.HttpServletRequest;
89  import javax.servlet.http.HttpServletResponse;
90  
91  
92  /**
93   * Object Service operations.
94   */
95  public final class ObjectService
96  {
97      private static final int BUFFER_SIZE = 64 * 1024;
98  
99      private ObjectService(  )
100     {
101     }
102 
103     /**
104      * Create document.
105      */
106     public static void createDocument( CallContext context, CmisService service, String repositoryId,
107         HttpServletRequest request, HttpServletResponse response )
108         throws Exception
109     {
110         // get parameters
111         String folderId = (String) context.get( CONTEXT_OBJECT_ID );
112         VersioningState versioningState = getEnumParameter( request, PARAM_VERSIONIG_STATE, VersioningState.class );
113         String transaction = getStringParameter( request, PARAM_TRANSACTION );
114 
115         // execute
116         ControlParser cp = new ControlParser( request );
117         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
118 
119         String newObjectId = service.createDocument( repositoryId, createProperties( cp, null, typeCache ), folderId,
120                 createContentStream( request ), versioningState, createPolicies( cp ), createAddAcl( cp ),
121                 createRemoveAcl( cp ), null );
122 
123         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
124 
125         if ( object == null )
126         {
127             throw new CmisRuntimeException( "New document is null!" );
128         }
129 
130         // return object
131         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
132 
133         setStatus( request, response, HttpServletResponse.SC_CREATED );
134         setCookie( request, response, repositoryId, transaction,
135             createCookieValue( HttpServletResponse.SC_CREATED, object.getId(  ), null, null ) );
136 
137         writeJSON( jsonObject, request, response );
138     }
139 
140     /**
141      * Create document from source.
142      */
143     public static void createDocumentFromSource( CallContext context, CmisService service, String repositoryId,
144         HttpServletRequest request, HttpServletResponse response )
145         throws Exception
146     {
147         // get parameters
148         String folderId = (String) context.get( CONTEXT_OBJECT_ID );
149         String sourceId = getStringParameter( request, PARAM_SOURCE_ID );
150         VersioningState versioningState = getEnumParameter( request, PARAM_VERSIONIG_STATE, VersioningState.class );
151         String transaction = getStringParameter( request, PARAM_TRANSACTION );
152 
153         // execute
154         ControlParser cp = new ControlParser( request );
155         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
156 
157         ObjectData sourceDoc = getSimpleObject( service, repositoryId, sourceId );
158         PropertyData<?> sourceTypeId = sourceDoc.getProperties(  ).getProperties(  ).get( PropertyIds.OBJECT_TYPE_ID );
159 
160         if ( ( sourceTypeId == null ) || ( sourceTypeId.getFirstValue(  ) == null ) )
161         {
162             throw new CmisRuntimeException( "Source object has no type!?!" );
163         }
164 
165         String newObjectId = service.createDocumentFromSource( repositoryId, sourceId,
166                 createProperties( cp, sourceTypeId.getFirstValue(  ).toString(  ), typeCache ), folderId,
167                 versioningState, createPolicies( cp ), createAddAcl( cp ), createRemoveAcl( cp ), null );
168 
169         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
170 
171         if ( object == null )
172         {
173             throw new CmisRuntimeException( "New document is null!" );
174         }
175 
176         // return object
177         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
178 
179         setStatus( request, response, HttpServletResponse.SC_CREATED );
180         setCookie( request, response, repositoryId, transaction,
181             createCookieValue( HttpServletResponse.SC_CREATED, object.getId(  ), null, null ) );
182 
183         writeJSON( jsonObject, request, response );
184     }
185 
186     /**
187      * Create folder.
188      */
189     public static void createFolder( CallContext context, CmisService service, String repositoryId,
190         HttpServletRequest request, HttpServletResponse response )
191         throws Exception
192     {
193         // get parameters
194         String folderId = (String) context.get( CONTEXT_OBJECT_ID );
195         String transaction = getStringParameter( request, PARAM_TRANSACTION );
196 
197         // execute
198         ControlParser cp = new ControlParser( request );
199         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
200 
201         String newObjectId = service.createFolder( repositoryId, createProperties( cp, null, typeCache ), folderId,
202                 createPolicies( cp ), createAddAcl( cp ), createRemoveAcl( cp ), null );
203 
204         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
205 
206         if ( object == null )
207         {
208             throw new CmisRuntimeException( "New folder is null!" );
209         }
210 
211         // return object
212         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
213 
214         setStatus( request, response, HttpServletResponse.SC_CREATED );
215         setCookie( request, response, repositoryId, transaction,
216             createCookieValue( HttpServletResponse.SC_CREATED, object.getId(  ), null, null ) );
217 
218         writeJSON( jsonObject, request, response );
219     }
220 
221     /**
222      * Create policy.
223      */
224     public static void createPolicy( CallContext context, CmisService service, String repositoryId,
225         HttpServletRequest request, HttpServletResponse response )
226         throws Exception
227     {
228         // get parameters
229         String folderId = (String) context.get( CONTEXT_OBJECT_ID );
230         String transaction = getStringParameter( request, PARAM_TRANSACTION );
231 
232         // execute
233         ControlParser cp = new ControlParser( request );
234         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
235 
236         String newObjectId = service.createPolicy( repositoryId, createProperties( cp, null, typeCache ), folderId,
237                 createPolicies( cp ), createAddAcl( cp ), createRemoveAcl( cp ), null );
238 
239         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
240 
241         if ( object == null )
242         {
243             throw new CmisRuntimeException( "New policy is null!" );
244         }
245 
246         // return object
247         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
248 
249         setStatus( request, response, HttpServletResponse.SC_CREATED );
250         setCookie( request, response, repositoryId, transaction,
251             createCookieValue( HttpServletResponse.SC_CREATED, object.getId(  ), null, null ) );
252 
253         writeJSON( jsonObject, request, response );
254     }
255 
256     /**
257      * Create relationship.
258      */
259     public static void createRelationship( CallContext context, CmisService service, String repositoryId,
260         HttpServletRequest request, HttpServletResponse response )
261         throws Exception
262     {
263         // get parameters
264         String transaction = getStringParameter( request, PARAM_TRANSACTION );
265 
266         // execute
267         ControlParser cp = new ControlParser( request );
268         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
269 
270         String newObjectId = service.createRelationship( repositoryId, createProperties( cp, null, typeCache ),
271                 createPolicies( cp ), createAddAcl( cp ), createRemoveAcl( cp ), null );
272 
273         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
274 
275         if ( object == null )
276         {
277             throw new CmisRuntimeException( "New relationship is null!" );
278         }
279 
280         // return object
281         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
282 
283         setStatus( request, response, HttpServletResponse.SC_CREATED );
284         setCookie( request, response, repositoryId, transaction,
285             createCookieValue( HttpServletResponse.SC_CREATED, object.getId(  ), null, null ) );
286 
287         writeJSON( jsonObject, request, response );
288     }
289 
290     /**
291      * updateProperties.
292      */
293     public static void updateProperties( CallContext context, CmisService service, String repositoryId,
294         HttpServletRequest request, HttpServletResponse response )
295         throws Exception
296     {
297         // get parameters
298         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
299         String typeId = (String) context.get( CONTEXT_OBJECT_TYPE_ID );
300         String changeToken = getStringParameter( request, Constants.PARAM_CHANGE_TOKEN );
301         String transaction = getStringParameter( request, PARAM_TRANSACTION );
302 
303         // execute
304         ControlParser cp = new ControlParser( request );
305         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
306         Holder<String> objectIdHolder = new Holder<String>( objectId );
307         Holder<String> changeTokenHolder = ( ( changeToken == null ) ? null : new Holder<String>( changeToken ) );
308 
309         service.updateProperties( repositoryId, objectIdHolder, changeTokenHolder,
310             createProperties( cp, typeId, typeCache ), null );
311 
312         String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
313 
314         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
315 
316         if ( object == null )
317         {
318             throw new CmisRuntimeException( "Object is null!" );
319         }
320 
321         // return object
322         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
323 
324         int status = HttpServletResponse.SC_OK;
325 
326         if ( !objectId.equals( newObjectId ) )
327         {
328             status = HttpServletResponse.SC_CREATED;
329         }
330 
331         setStatus( request, response, status );
332         setCookie( request, response, repositoryId, transaction,
333             createCookieValue( status, object.getId(  ), null, null ) );
334 
335         writeJSON( jsonObject, request, response );
336     }
337 
338     /**
339      * getProperties.
340      */
341     public static void getProperties( CallContext context, CmisService service, String repositoryId,
342         HttpServletRequest request, HttpServletResponse response )
343         throws Exception
344     {
345         // get parameters
346         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
347         ReturnVersion returnVersion = getEnumParameter( request, PARAM_RETURN_VERSION, ReturnVersion.class );
348         String filter = getStringParameter( request, PARAM_FILTER );
349 
350         // execute
351         Properties properties;
352 
353         if ( ( returnVersion == ReturnVersion.LATEST ) || ( returnVersion == ReturnVersion.LASTESTMAJOR ) )
354         {
355             properties = service.getPropertiesOfLatestVersion( repositoryId, objectId, null,
356                     returnVersion == ReturnVersion.LASTESTMAJOR, filter, null );
357         }
358         else
359         {
360             properties = service.getProperties( repositoryId, objectId, filter, null );
361         }
362 
363         if ( properties == null )
364         {
365             throw new CmisRuntimeException( "Properties are null!" );
366         }
367 
368         // return object
369         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
370         JSONObject jsonObject = JSONConverter.convert( properties, objectId, typeCache, false );
371 
372         response.setStatus( HttpServletResponse.SC_OK );
373         writeJSON( jsonObject, request, response );
374     }
375 
376     /**
377      * getObject.
378      */
379     public static void getObject( CallContext context, CmisService service, String repositoryId,
380         HttpServletRequest request, HttpServletResponse response )
381         throws Exception
382     {
383         // get parameters
384         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
385         ReturnVersion returnVersion = getEnumParameter( request, PARAM_RETURN_VERSION, ReturnVersion.class );
386         String filter = getStringParameter( request, PARAM_FILTER );
387         Boolean includeAllowableActions = getBooleanParameter( request, PARAM_ALLOWABLE_ACTIONS );
388         IncludeRelationships includeRelationships = getEnumParameter( request, PARAM_RELATIONSHIPS,
389                 IncludeRelationships.class );
390         String renditionFilter = getStringParameter( request, PARAM_RENDITION_FILTER );
391         Boolean includePolicyIds = getBooleanParameter( request, PARAM_POLICY_IDS );
392         Boolean includeAcl = getBooleanParameter( request, PARAM_ACL );
393 
394         // execute
395         ObjectData object;
396 
397         if ( ( returnVersion == ReturnVersion.LATEST ) || ( returnVersion == ReturnVersion.LASTESTMAJOR ) )
398         {
399             object = service.getObjectOfLatestVersion( repositoryId, objectId, null,
400                     returnVersion == ReturnVersion.LASTESTMAJOR, filter, includeAllowableActions, includeRelationships,
401                     renditionFilter, includePolicyIds, includeAcl, null );
402         }
403         else
404         {
405             object = service.getObject( repositoryId, objectId, filter, includeAllowableActions, includeRelationships,
406                     renditionFilter, includePolicyIds, includeAcl, null );
407         }
408 
409         if ( object == null )
410         {
411             throw new CmisRuntimeException( "Object is null!" );
412         }
413 
414         // return object
415         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
416         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
417 
418         response.setStatus( HttpServletResponse.SC_OK );
419         writeJSON( jsonObject, request, response );
420     }
421 
422     /**
423      * getAllowableActions.
424      */
425     public static void getAllowableActions( CallContext context, CmisService service, String repositoryId,
426         HttpServletRequest request, HttpServletResponse response )
427         throws Exception
428     {
429         // get parameters
430         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
431 
432         AllowableActions allowableActions = service.getAllowableActions( repositoryId, objectId, null );
433 
434         JSONObject jsonAllowableActions = JSONConverter.convert( allowableActions );
435 
436         response.setStatus( HttpServletResponse.SC_OK );
437         writeJSON( jsonAllowableActions, request, response );
438     }
439 
440     /**
441      * getRenditions.
442      */
443     public static void getRenditions( CallContext context, CmisService service, String repositoryId,
444         HttpServletRequest request, HttpServletResponse response )
445         throws Exception
446     {
447         // get parameters
448         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
449         String renditionFilter = getStringParameter( request, PARAM_RENDITION_FILTER );
450         BigInteger maxItems = getBigIntegerParameter( request, Constants.PARAM_MAX_ITEMS );
451         BigInteger skipCount = getBigIntegerParameter( request, Constants.PARAM_SKIP_COUNT );
452 
453         // execute
454         List<RenditionData> renditions = service.getRenditions( repositoryId, objectId, renditionFilter, maxItems,
455                 skipCount, null );
456 
457         JSONArray jsonRenditions = new JSONArray(  );
458 
459         if ( renditions != null )
460         {
461             for ( RenditionData rendition : renditions )
462             {
463                 jsonRenditions.add( JSONConverter.convert( rendition ) );
464             }
465         }
466 
467         response.setStatus( HttpServletResponse.SC_OK );
468         writeJSON( jsonRenditions, request, response );
469     }
470 
471     /**
472      * getContentStream.
473      */
474     public static void getContentStream( CallContext context, CmisService service, String repositoryId,
475         HttpServletRequest request, HttpServletResponse response )
476         throws Exception
477     {
478         // get parameters
479         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
480         String streamId = getStringParameter( request, PARAM_STREAM_ID );
481 
482         BigInteger offset = context.getOffset(  );
483         BigInteger length = context.getLength(  );
484 
485         // execute
486         ContentStream content = service.getContentStream( repositoryId, objectId, streamId, offset, length, null );
487 
488         if ( ( content == null ) || ( content.getStream(  ) == null ) )
489         {
490             throw new CmisRuntimeException( "Content stream is null!" );
491         }
492 
493         String contentType = content.getMimeType(  );
494 
495         if ( contentType == null )
496         {
497             contentType = MEDIATYPE_OCTETSTREAM;
498         }
499 
500         // set headers
501         if ( ( offset == null ) && ( length == null ) )
502         {
503             response.setStatus( HttpServletResponse.SC_OK );
504         }
505         else
506         {
507             setStatus( request, response, HttpServletResponse.SC_PARTIAL_CONTENT );
508         }
509 
510         response.setContentType( contentType );
511 
512         if ( content.getFileName(  ) != null )
513         {
514             response.setHeader( MimeHelper.CONTENT_DISPOSITION,
515                 MimeHelper.encodeContentDisposition( MimeHelper.DISPOSITION_INLINE, content.getFileName(  ) ) );
516         }
517 
518         // send content
519         InputStream in = new BufferedInputStream( content.getStream(  ), BUFFER_SIZE );
520         OutputStream out = new BufferedOutputStream( response.getOutputStream(  ) );
521 
522         byte[] buffer = new byte[BUFFER_SIZE];
523         int b;
524 
525         while ( ( b = in.read( buffer ) ) > -1 )
526         {
527             out.write( buffer, 0, b );
528         }
529 
530         in.close(  );
531         out.flush(  );
532     }
533 
534     /**
535      * deleteObject.
536      */
537     public static void deleteObject( CallContext context, CmisService service, String repositoryId,
538         HttpServletRequest request, HttpServletResponse response )
539         throws Exception
540     {
541         // get parameters
542         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
543         Boolean allVersions = getBooleanParameter( request, Constants.PARAM_ALL_VERSIONS );
544 
545         service.deleteObject( repositoryId, objectId, allVersions, null );
546 
547         response.setStatus( HttpServletResponse.SC_OK );
548         writeEmpty( request, response );
549     }
550 
551     /**
552      * deleteTree.
553      */
554     public static void deleteTree( CallContext context, CmisService service, String repositoryId,
555         HttpServletRequest request, HttpServletResponse response )
556         throws Exception
557     {
558         // get parameters
559         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
560         Boolean allVersions = getBooleanParameter( request, Constants.PARAM_ALL_VERSIONS );
561         UnfileObject unfileObjects = getEnumParameter( request, Constants.PARAM_UNFILE_OBJECTS, UnfileObject.class );
562         Boolean continueOnFailure = getBooleanParameter( request, Constants.PARAM_CONTINUE_ON_FAILURE );
563 
564         // execute
565         FailedToDeleteData ftd = service.deleteTree( repositoryId, objectId, allVersions, unfileObjects,
566                 continueOnFailure, null );
567 
568         response.setStatus( HttpServletResponse.SC_OK );
569 
570         if ( ( ftd != null ) && ( ftd.getIds(  ) != null ) && ( ftd.getIds(  ).size(  ) > 0 ) )
571         {
572             JSONObject jsonObject = JSONConverter.convert( ftd );
573             writeJSON( jsonObject, request, response );
574 
575             return;
576         }
577 
578         writeEmpty( request, response );
579     }
580 
581     /**
582      * Delete content stream.
583      */
584     public static void deleteContentStream( CallContext context, CmisService service, String repositoryId,
585         HttpServletRequest request, HttpServletResponse response )
586         throws Exception
587     {
588         // get parameters
589         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
590         String changeToken = getStringParameter( request, Constants.PARAM_CHANGE_TOKEN );
591 
592         // execute
593         Holder<String> objectIdHolder = new Holder<String>( objectId );
594         Holder<String> changeTokenHolder = ( ( changeToken == null ) ? null : new Holder<String>( changeToken ) );
595         service.deleteContentStream( repositoryId, objectIdHolder, changeTokenHolder, null );
596 
597         String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
598 
599         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
600 
601         if ( object == null )
602         {
603             throw new CmisRuntimeException( "Object is null!" );
604         }
605 
606         response.setStatus( HttpServletResponse.SC_OK );
607 
608         // return object
609         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
610         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
611 
612         writeJSON( jsonObject, request, response );
613     }
614 
615     /**
616      * Set content stream.
617      */
618     public static void setContentStream( CallContext context, CmisService service, String repositoryId,
619         HttpServletRequest request, HttpServletResponse response )
620         throws Exception
621     {
622         // get parameters
623         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
624         String changeToken = getStringParameter( request, Constants.PARAM_CHANGE_TOKEN );
625         Boolean overwriteFlag = getBooleanParameter( request, Constants.PARAM_OVERWRITE_FLAG );
626 
627         // execute
628         Holder<String> objectIdHolder = new Holder<String>( objectId );
629         Holder<String> changeTokenHolder = ( ( changeToken == null ) ? null : new Holder<String>( changeToken ) );
630         service.setContentStream( repositoryId, objectIdHolder, overwriteFlag, changeTokenHolder,
631             createContentStream( request ), null );
632 
633         String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
634 
635         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
636 
637         if ( object == null )
638         {
639             throw new CmisRuntimeException( "Object is null!" );
640         }
641 
642         // set headers
643         String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
644 
645         setStatus( request, response, HttpServletResponse.SC_CREATED );
646         response.setHeader( "Location", location );
647 
648         // return object
649         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
650         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
651 
652         writeJSON( jsonObject, request, response );
653     }
654 
655     /**
656      * moveObject.
657      */
658     public static void moveObject( CallContext context, CmisService service, String repositoryId,
659         HttpServletRequest request, HttpServletResponse response )
660         throws Exception
661     {
662         // get parameters
663         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
664         String targetFolderId = getStringParameter( request, PARAM_TARGET_FOLDER_ID );
665         String sourceFolderId = getStringParameter( request, PARAM_SOURCE_FOLDER_ID );
666 
667         // execute
668         Holder<String> objectIdHolder = new Holder<String>( objectId );
669         service.moveObject( repositoryId, objectIdHolder, targetFolderId, sourceFolderId, null );
670 
671         String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
672 
673         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
674 
675         if ( object == null )
676         {
677             throw new CmisRuntimeException( "Object is null!" );
678         }
679 
680         // set headers
681         String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
682 
683         setStatus( request, response, HttpServletResponse.SC_CREATED );
684         response.setHeader( "Location", location );
685 
686         // return object
687         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
688         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
689 
690         writeJSON( jsonObject, request, response );
691     }
692 }