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 static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_ALLOWABLE_ACTIONS;
24 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_CHECKIN_COMMENT;
25 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FILTER;
26 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_MAJOR;
27 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_TRANSACTION;
28 import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
29 import org.apache.chemistry.opencmis.commons.impl.TypeCache;
30 import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
31 import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
32 import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
33 import org.apache.chemistry.opencmis.commons.server.CallContext;
34 import org.apache.chemistry.opencmis.commons.server.CmisService;
35 import org.apache.chemistry.opencmis.commons.spi.Holder;
36 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_CONTENT;
37 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
38 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
39 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
40 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_TYPE_ID;
41 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createAddAcl;
42 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createContentStream;
43 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createCookieValue;
44 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createPolicies;
45 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createProperties;
46 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createRemoveAcl;
47 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
48 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setCookie;
49 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
50 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeEmpty;
51 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
52 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
53 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
54
55 import java.util.List;
56
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpServletResponse;
59
60
61
62
63
64 public class VersioningService
65 {
66 private VersioningService( )
67 {
68 }
69
70
71
72
73 public static void checkOut( CallContext context, CmisService service, String repositoryId,
74 HttpServletRequest request, HttpServletResponse response )
75 throws Exception
76 {
77
78 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
79 String transaction = getStringParameter( request, PARAM_TRANSACTION );
80
81
82 Holder<String> checkOutId = new Holder<String>( objectId );
83 service.checkOut( repositoryId, checkOutId, null, null );
84
85 ObjectData object = getSimpleObject( service, repositoryId, checkOutId.getValue( ) );
86
87 if ( object == null )
88 {
89 throw new CmisRuntimeException( "PWC is null!" );
90 }
91
92
93 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
94 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
95
96
97 String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, object.getId( ) );
98
99 setStatus( request, response, HttpServletResponse.SC_CREATED );
100 response.setHeader( "Location", location );
101
102 setCookie( request, response, repositoryId, transaction,
103 createCookieValue( HttpServletResponse.SC_CREATED, object.getId( ), null, null ) );
104
105 writeJSON( jsonObject, request, response );
106 }
107
108
109
110
111 public static void cancelCheckOut( CallContext context, CmisService service, String repositoryId,
112 HttpServletRequest request, HttpServletResponse response )
113 throws Exception
114 {
115
116 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
117
118
119 service.cancelCheckOut( repositoryId, objectId, null );
120
121 response.setStatus( HttpServletResponse.SC_OK );
122 writeEmpty( request, response );
123 }
124
125
126
127
128 public static void checkIn( CallContext context, CmisService service, String repositoryId,
129 HttpServletRequest request, HttpServletResponse response )
130 throws Exception
131 {
132
133 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
134 String typeId = (String) context.get( CONTEXT_OBJECT_TYPE_ID );
135 Boolean major = getBooleanParameter( request, PARAM_MAJOR );
136 String checkinComment = getStringParameter( request, PARAM_CHECKIN_COMMENT );
137 String transaction = getStringParameter( request, PARAM_TRANSACTION );
138
139
140 ControlParser cp = new ControlParser( request );
141 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
142 Holder<String> objectIdHolder = new Holder<String>( objectId );
143
144 service.checkIn( repositoryId, objectIdHolder, major, createProperties( cp, typeId, typeCache ),
145 createContentStream( request ), checkinComment, createPolicies( cp ), createAddAcl( cp ),
146 createRemoveAcl( cp ), null );
147
148 String newObjectId = ( ( objectIdHolder.getValue( ) == null ) ? objectId : objectIdHolder.getValue( ) );
149
150 ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
151
152 if ( object == null )
153 {
154 throw new CmisRuntimeException( "New version is null!" );
155 }
156
157
158 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
159
160 String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, object.getId( ) );
161
162 setStatus( request, response, HttpServletResponse.SC_CREATED );
163 response.setHeader( "Location", location );
164
165 setCookie( request, response, repositoryId, transaction,
166 createCookieValue( HttpServletResponse.SC_CREATED, object.getId( ), null, null ) );
167
168 writeJSON( jsonObject, request, response );
169 }
170
171
172
173
174 public static void getAllVersions( CallContext context, CmisService service, String repositoryId,
175 HttpServletRequest request, HttpServletResponse response )
176 throws Exception
177 {
178
179 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
180 String filter = getStringParameter( request, PARAM_FILTER );
181 Boolean includeAllowableActions = getBooleanParameter( request, PARAM_ALLOWABLE_ACTIONS );
182
183
184 List<ObjectData> versions = service.getAllVersions( repositoryId, objectId, null, filter,
185 includeAllowableActions, null );
186
187 if ( versions == null )
188 {
189 throw new CmisRuntimeException( "Versions are null!" );
190 }
191
192 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
193 JSONArray jsonVersions = new JSONArray( );
194
195 for ( ObjectData version : versions )
196 {
197 jsonVersions.add( JSONConverter.convert( version, typeCache, false ) );
198 }
199
200 response.setStatus( HttpServletResponse.SC_OK );
201 writeJSON( jsonVersions, request, response );
202 }
203 }