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.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   * Versioning Service operations.
63   */
64  public class VersioningService
65  {
66      private VersioningService(  )
67      {
68      }
69  
70      /**
71       * checkOut.
72       */
73      public static void checkOut( CallContext context, CmisService service, String repositoryId,
74          HttpServletRequest request, HttpServletResponse response )
75          throws Exception
76      {
77          // get parameters
78          String objectId = (String) context.get( CONTEXT_OBJECT_ID );
79          String transaction = getStringParameter( request, PARAM_TRANSACTION );
80  
81          // execute
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          // return object
93          TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
94          JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
95  
96          // set headers
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      * checkOut.
110      */
111     public static void cancelCheckOut( CallContext context, CmisService service, String repositoryId,
112         HttpServletRequest request, HttpServletResponse response )
113         throws Exception
114     {
115         // get parameters
116         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
117 
118         // execute
119         service.cancelCheckOut( repositoryId, objectId, null );
120 
121         response.setStatus( HttpServletResponse.SC_OK );
122         writeEmpty( request, response );
123     }
124 
125     /**
126      * checkIn.
127      */
128     public static void checkIn( CallContext context, CmisService service, String repositoryId,
129         HttpServletRequest request, HttpServletResponse response )
130         throws Exception
131     {
132         // get parameters
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         // execute
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         // return object
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      * getAllVersions.
173      */
174     public static void getAllVersions( CallContext context, CmisService service, String repositoryId,
175         HttpServletRequest request, HttpServletResponse response )
176         throws Exception
177     {
178         // get parameters
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         // execute
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 }