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.atompub;
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 org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
25  import org.apache.chemistry.opencmis.commons.server.CallContext;
26  import org.apache.chemistry.opencmis.commons.server.CmisService;
27  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
28  import org.apache.chemistry.opencmis.commons.spi.Holder;
29  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_ENTRY;
30  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
31  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
32  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.writeObjectEntry;
33  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
34  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
35  
36  import java.util.List;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  
42  /**
43   * Versioning Service operations.
44   */
45  public class VersioningService
46  {
47      private VersioningService(  )
48      {
49      }
50  
51      /**
52       * Check Out.
53       */
54      public static void checkOut( CallContext context, CmisService service, String repositoryId,
55          HttpServletRequest request, HttpServletResponse response )
56          throws Exception
57      {
58          // get parameters
59          AtomEntryParser parser = new AtomEntryParser( context.getTempDirectory(  ), context.getMemoryThreshold(  ) );
60          parser.setIgnoreAtomContentSrc( true ); // needed for some clients
61          parser.parse( request.getInputStream(  ) );
62  
63          // execute
64          Holder<String> checkOutId = new Holder<String>( parser.getId(  ) );
65          service.checkOut( repositoryId, checkOutId, null, null );
66  
67          ObjectInfo objectInfo = service.getObjectInfo( repositoryId, checkOutId.getValue(  ) );
68  
69          if ( objectInfo == null )
70          {
71              throw new CmisRuntimeException( "Object Info is missing!" );
72          }
73  
74          ObjectData object = objectInfo.getObject(  );
75  
76          if ( object == null )
77          {
78              throw new CmisRuntimeException( "Object is null!" );
79          }
80  
81          if ( object.getId(  ) == null )
82          {
83              throw new CmisRuntimeException( "Object Id is null!" );
84          }
85  
86          // set headers
87          UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
88          String location = compileUrl( baseUrl, RESOURCE_ENTRY, object.getId(  ) );
89  
90          response.setStatus( HttpServletResponse.SC_CREATED );
91          response.setContentType( Constants.MEDIATYPE_ENTRY );
92          response.setHeader( "Content-Location", location );
93          response.setHeader( "Location", location );
94  
95          // write XML
96          AtomEntry entry = new AtomEntry(  );
97          entry.startDocument( response.getOutputStream(  ) );
98          writeObjectEntry( service, entry, object, null, repositoryId, null, null, baseUrl, true );
99          entry.endDocument(  );
100     }
101 
102     /**
103      * Get all versions.
104      */
105     public static void getAllVersions( CallContext context, CmisService service, String repositoryId,
106         HttpServletRequest request, HttpServletResponse response )
107         throws Exception
108     {
109         // get parameters
110         String objectId = getStringParameter( request, Constants.PARAM_ID );
111         String versionSeriesId = getStringParameter( request, Constants.PARAM_VERSION_SERIES_ID );
112         String filter = getStringParameter( request, Constants.PARAM_FILTER );
113         Boolean includeAllowableActions = getBooleanParameter( request, Constants.PARAM_ALLOWABLE_ACTIONS );
114 
115         // execute
116         List<ObjectData> versions = service.getAllVersions( repositoryId, objectId, versionSeriesId, filter,
117                 includeAllowableActions, null );
118 
119         if ( ( versions == null ) || versions.isEmpty(  ) )
120         {
121             throw new CmisRuntimeException( "Version list is null or empty!" );
122         }
123 
124         // set headers
125         response.setStatus( HttpServletResponse.SC_OK );
126         response.setContentType( Constants.MEDIATYPE_FEED );
127 
128         // write XML
129         AtomFeed feed = new AtomFeed(  );
130         feed.startDocument( response.getOutputStream(  ) );
131         feed.startFeed( true );
132 
133         // write basic Atom feed elements
134         ObjectInfo latestObjectInfo = service.getObjectInfo( repositoryId, versions.get( 0 ).getId(  ) );
135         ObjectInfo firstObjectInfo = service.getObjectInfo( repositoryId,
136                 versions.get( versions.size(  ) - 1 ).getId(  ) );
137 
138         feed.writeFeedElements( versionSeriesId, firstObjectInfo.getCreatedBy(  ), latestObjectInfo.getName(  ),
139             latestObjectInfo.getLastModificationDate(  ), null, null );
140 
141         // write links
142         UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
143 
144         feed.writeServiceLink( baseUrl.toString(  ), repositoryId );
145 
146         if ( objectId != null )
147         {
148             feed.writeViaLink( compileUrl( baseUrl, RESOURCE_ENTRY, objectId ) );
149         }
150 
151         // write entries
152         AtomEntry entry = new AtomEntry( feed.getWriter(  ) );
153 
154         for ( ObjectData object : versions )
155         {
156             if ( object == null )
157             {
158                 continue;
159             }
160 
161             writeObjectEntry( service, entry, object, null, repositoryId, null, null, baseUrl, false );
162         }
163 
164         // we are done
165         feed.endFeed(  );
166         feed.endDocument(  );
167     }
168 
169     /**
170      * Delete object.
171      */
172     public static void deleteAllVersions( CallContext context, CmisService service, String repositoryId,
173         HttpServletRequest request, HttpServletResponse response )
174     {
175         // get parameters
176         String objectId = getStringParameter( request, Constants.PARAM_ID );
177 
178         // execute
179         service.deleteObjectOrCancelCheckOut( repositoryId, objectId, Boolean.TRUE, null );
180 
181         // set headers
182         response.setStatus( HttpServletResponse.SC_NO_CONTENT );
183     }
184 }