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.enums.VersioningState;
23 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
24 import org.apache.chemistry.opencmis.commons.impl.Constants;
25 import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
26 import org.apache.chemistry.opencmis.commons.server.CallContext;
27 import org.apache.chemistry.opencmis.commons.server.CmisService;
28 import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
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.getEnumParameter;
34 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38
39
40
41
42
43 public class MultiFilingService
44 {
45 private MultiFilingService( )
46 {
47 }
48
49
50
51
52 public static void removeObjectFromFolder( CallContext context, CmisService service, String repositoryId,
53 HttpServletRequest request, HttpServletResponse response )
54 throws Exception
55 {
56
57 String removeFrom = getStringParameter( request, Constants.PARAM_REMOVE_FROM );
58
59 AtomEntryParser parser = new AtomEntryParser( context.getTempDirectory( ), context.getMemoryThreshold( ) );
60 parser.setIgnoreAtomContentSrc( true );
61 parser.parse( request.getInputStream( ) );
62
63 String objectId = parser.getId( );
64
65 if ( ( objectId == null ) && ( removeFrom == null ) )
66 {
67
68 createUnfiledObject( context, service, repositoryId, request, response, parser );
69
70 return;
71 }
72
73
74 service.removeObjectFromFolder( repositoryId, objectId, removeFrom, null );
75
76 ObjectInfo objectInfo = service.getObjectInfo( repositoryId, objectId );
77
78 if ( objectInfo == null )
79 {
80 throw new CmisRuntimeException( "Object Info is missing!" );
81 }
82
83 ObjectData object = objectInfo.getObject( );
84
85 if ( object == null )
86 {
87 throw new CmisRuntimeException( "Object is null!" );
88 }
89
90 if ( object.getId( ) == null )
91 {
92 throw new CmisRuntimeException( "Object Id is null!" );
93 }
94
95
96 UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
97
98 response.setStatus( HttpServletResponse.SC_CREATED );
99 response.setContentType( Constants.MEDIATYPE_ENTRY );
100 response.setHeader( "Location", compileUrl( baseUrl, RESOURCE_ENTRY, object.getId( ) ) );
101
102
103 AtomEntry entry = new AtomEntry( );
104 entry.startDocument( response.getOutputStream( ) );
105 writeObjectEntry( service, entry, object, null, repositoryId, null, null, baseUrl, true );
106 entry.endDocument( );
107 }
108
109
110
111
112
113
114
115 private static void createUnfiledObject( CallContext context, CmisService service, String repositoryId,
116 HttpServletRequest request, HttpServletResponse response, AtomEntryParser parser )
117 throws Exception
118 {
119
120 VersioningState versioningState = getEnumParameter( request, Constants.PARAM_VERSIONIG_STATE,
121 VersioningState.class );
122
123
124 String newObjectId = service.create( repositoryId, parser.getProperties( ), null, parser.getContentStream( ),
125 versioningState, parser.getPolicyIds( ), null );
126
127 ObjectInfo objectInfo = service.getObjectInfo( repositoryId, newObjectId );
128
129 if ( objectInfo == null )
130 {
131 throw new CmisRuntimeException( "Object Info is missing!" );
132 }
133
134 ObjectData object = objectInfo.getObject( );
135
136 if ( object == null )
137 {
138 throw new CmisRuntimeException( "Object is null!" );
139 }
140
141
142 UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
143
144 response.setStatus( HttpServletResponse.SC_CREATED );
145 response.setContentType( Constants.MEDIATYPE_ENTRY );
146 response.setHeader( "Location", compileUrl( baseUrl, RESOURCE_ENTRY, newObjectId ) );
147
148
149 AtomEntry entry = new AtomEntry( );
150 entry.startDocument( response.getOutputStream( ) );
151 writeObjectEntry( service, entry, object, null, repositoryId, null, null, baseUrl, true );
152 entry.endDocument( );
153 }
154 }