1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.chemistry.opencmis.server.impl.browser;
20
21 import org.apache.chemistry.opencmis.commons.data.ObjectData;
22 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
23 import org.apache.chemistry.opencmis.commons.impl.Constants;
24 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FOLDER_ID;
25 import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
26 import org.apache.chemistry.opencmis.commons.impl.TypeCache;
27 import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
28 import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
29 import org.apache.chemistry.opencmis.commons.server.CallContext;
30 import org.apache.chemistry.opencmis.commons.server.CmisService;
31 import org.apache.chemistry.opencmis.commons.spi.Holder;
32 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_CONTENT;
33 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
34 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
35 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
36 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
37 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
38 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
39 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
40 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45
46
47
48
49 public class MultiFilingService
50 {
51
52
53
54 public static void addObjectToFolder( CallContext context, CmisService service, String repositoryId,
55 HttpServletRequest request, HttpServletResponse response )
56 throws Exception
57 {
58
59 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
60 String folderId = getStringParameter( request, PARAM_FOLDER_ID );
61 Boolean allVersions = getBooleanParameter( request, Constants.PARAM_ALL_VERSIONS );
62
63
64 Holder<String> objectIdHolder = new Holder<String>( objectId );
65 service.addObjectToFolder( repositoryId, objectId, folderId, allVersions, null );
66
67 String newObjectId = ( ( objectIdHolder.getValue( ) == null ) ? objectId : objectIdHolder.getValue( ) );
68
69 ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
70
71 if ( object == null )
72 {
73 throw new CmisRuntimeException( "Object is null!" );
74 }
75
76
77 String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
78
79 setStatus( request, response, HttpServletResponse.SC_CREATED );
80 response.setHeader( "Location", location );
81
82
83 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
84 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
85
86 writeJSON( jsonObject, request, response );
87 }
88
89
90
91
92 public static void removeObjectFromFolder( CallContext context, CmisService service, String repositoryId,
93 HttpServletRequest request, HttpServletResponse response )
94 throws Exception
95 {
96
97 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
98 String folderId = getStringParameter( request, PARAM_FOLDER_ID );
99
100
101 Holder<String> objectIdHolder = new Holder<String>( objectId );
102 service.removeObjectFromFolder( repositoryId, objectId, folderId, null );
103
104 String newObjectId = ( ( objectIdHolder.getValue( ) == null ) ? objectId : objectIdHolder.getValue( ) );
105
106 ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
107
108 if ( object == null )
109 {
110 throw new CmisRuntimeException( "Object is null!" );
111 }
112
113
114 String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
115
116 setStatus( request, response, HttpServletResponse.SC_CREATED );
117 response.setHeader( "Location", location );
118
119
120 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
121 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
122
123 writeJSON( jsonObject, request, response );
124 }
125 }