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.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
44
45 public class VersioningService
46 {
47 private VersioningService( )
48 {
49 }
50
51
52
53
54 public static void checkOut( CallContext context, CmisService service, String repositoryId,
55 HttpServletRequest request, HttpServletResponse response )
56 throws Exception
57 {
58
59 AtomEntryParser parser = new AtomEntryParser( context.getTempDirectory( ), context.getMemoryThreshold( ) );
60 parser.setIgnoreAtomContentSrc( true );
61 parser.parse( request.getInputStream( ) );
62
63
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
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
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
104
105 public static void getAllVersions( CallContext context, CmisService service, String repositoryId,
106 HttpServletRequest request, HttpServletResponse response )
107 throws Exception
108 {
109
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
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
125 response.setStatus( HttpServletResponse.SC_OK );
126 response.setContentType( Constants.MEDIATYPE_FEED );
127
128
129 AtomFeed feed = new AtomFeed( );
130 feed.startDocument( response.getOutputStream( ) );
131 feed.startFeed( true );
132
133
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
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
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
165 feed.endFeed( );
166 feed.endDocument( );
167 }
168
169
170
171
172 public static void deleteAllVersions( CallContext context, CmisService service, String repositoryId,
173 HttpServletRequest request, HttpServletResponse response )
174 {
175
176 String objectId = getStringParameter( request, Constants.PARAM_ID );
177
178
179 service.deleteObjectOrCancelCheckOut( repositoryId, objectId, Boolean.TRUE, null );
180
181
182 response.setStatus( HttpServletResponse.SC_NO_CONTENT );
183 }
184 }