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.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   * MultiFiling Service operations.
42   */
43  public class MultiFilingService
44  {
45      private MultiFilingService(  )
46      {
47      }
48  
49      /**
50       * Remove object from folder.
51       */
52      public static void removeObjectFromFolder( CallContext context, CmisService service, String repositoryId,
53          HttpServletRequest request, HttpServletResponse response )
54          throws Exception
55      {
56          // get parameters
57          String removeFrom = getStringParameter( request, Constants.PARAM_REMOVE_FROM );
58  
59          AtomEntryParser parser = new AtomEntryParser( context.getTempDirectory(  ), context.getMemoryThreshold(  ) );
60          parser.setIgnoreAtomContentSrc( true ); // needed for some clients
61          parser.parse( request.getInputStream(  ) );
62  
63          String objectId = parser.getId(  );
64  
65          if ( ( objectId == null ) && ( removeFrom == null ) )
66          {
67              // create unfiled object
68              createUnfiledObject( context, service, repositoryId, request, response, parser );
69  
70              return;
71          }
72  
73          // execute
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          // set headers
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         // write XML
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      * Create unfiled object.
111      *
112      * (Creation of unfiled objects via AtomPub is not defined in the CMIS 1.0
113      * specification. This implementation follow the CMIS 1.1 draft.)
114      */
115     private static void createUnfiledObject( CallContext context, CmisService service, String repositoryId,
116         HttpServletRequest request, HttpServletResponse response, AtomEntryParser parser )
117         throws Exception
118     {
119         // get additional parameters
120         VersioningState versioningState = getEnumParameter( request, Constants.PARAM_VERSIONIG_STATE,
121                 VersioningState.class );
122 
123         // create
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         // set headers
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         // write XML
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 }