This project has retired. For details please refer to its Attic page.
MultiFilingService xref

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 static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_ENTRY;
22  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
23  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
24  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.writeObjectEntry;
25  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
26  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.chemistry.opencmis.commons.data.ObjectData;
32  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
33  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
34  import org.apache.chemistry.opencmis.commons.impl.Constants;
35  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
36  import org.apache.chemistry.opencmis.commons.server.CallContext;
37  import org.apache.chemistry.opencmis.commons.server.CmisService;
38  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
39  
40  /**
41   * MultiFiling Service operations.
42   */
43  public class MultiFilingService {
44  
45      private MultiFilingService() {
46      }
47  
48      /**
49       * Remove object from folder.
50       */
51      public static void removeObjectFromFolder(CallContext context, CmisService service, String repositoryId,
52              HttpServletRequest request, HttpServletResponse response) throws Exception {
53          // get parameters
54          String removeFrom = getStringParameter(request, Constants.PARAM_REMOVE_FROM);
55  
56          AtomEntryParser parser = new AtomEntryParser(context.getTempDirectory(), context.getMemoryThreshold());
57          parser.setIgnoreAtomContentSrc(true); // needed for some clients
58          parser.parse(request.getInputStream());
59  
60          String objectId = parser.getId();
61  
62          if (objectId == null && removeFrom == null) {
63              // create unfiled object
64              createUnfiledObject(context, service, repositoryId, request, response, parser);
65              return;
66          }
67  
68          // execute
69          service.removeObjectFromFolder(repositoryId, objectId, removeFrom, null);
70  
71          ObjectInfo objectInfo = service.getObjectInfo(repositoryId, objectId);
72          if (objectInfo == null) {
73              throw new CmisRuntimeException("Object Info is missing!");
74          }
75  
76          ObjectData object = objectInfo.getObject();
77          if (object == null) {
78              throw new CmisRuntimeException("Object is null!");
79          }
80  
81          if (object.getId() == null) {
82              throw new CmisRuntimeException("Object Id is null!");
83          }
84  
85          // set headers
86          UrlBuilder baseUrl = compileBaseUrl(request, repositoryId);
87  
88          response.setStatus(HttpServletResponse.SC_CREATED);
89          response.setContentType(Constants.MEDIATYPE_ENTRY);
90          response.setHeader("Location", compileUrl(baseUrl, RESOURCE_ENTRY, object.getId()));
91  
92          // write XML
93          AtomEntry entry = new AtomEntry();
94          entry.startDocument(response.getOutputStream());
95          writeObjectEntry(service, entry, object, null, repositoryId, null, null, baseUrl, true);
96          entry.endDocument();
97      }
98  
99      /**
100      * Create unfiled object.
101      * 
102      * (Creation of unfiled objects via AtomPub is not defined in the CMIS 1.0
103      * specification. This implementation follow the CMIS 1.1 draft.)
104      */
105     private static void createUnfiledObject(CallContext context, CmisService service, String repositoryId,
106             HttpServletRequest request, HttpServletResponse response, AtomEntryParser parser) throws Exception {
107         // get additional parameters
108         VersioningState versioningState = getEnumParameter(request, Constants.PARAM_VERSIONIG_STATE,
109                 VersioningState.class);
110 
111         // create
112         String newObjectId = service.create(repositoryId, parser.getProperties(), null, parser.getContentStream(),
113                 versioningState, parser.getPolicyIds(), null);
114 
115         ObjectInfo objectInfo = service.getObjectInfo(repositoryId, newObjectId);
116         if (objectInfo == null) {
117             throw new CmisRuntimeException("Object Info is missing!");
118         }
119 
120         ObjectData object = objectInfo.getObject();
121         if (object == null) {
122             throw new CmisRuntimeException("Object is null!");
123         }
124 
125         // set headers
126         UrlBuilder baseUrl = compileBaseUrl(request, repositoryId);
127 
128         response.setStatus(HttpServletResponse.SC_CREATED);
129         response.setContentType(Constants.MEDIATYPE_ENTRY);
130         response.setHeader("Location", compileUrl(baseUrl, RESOURCE_ENTRY, newObjectId));
131 
132         // write XML
133         AtomEntry entry = new AtomEntry();
134         entry.startDocument(response.getOutputStream());
135         writeObjectEntry(service, entry, object, null, repositoryId, null, null, baseUrl, true);
136         entry.endDocument();
137     }
138 }