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.browser;
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 static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FOLDER_ID;
25  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
26  import org.apache.chemistry.opencmis.commons.impl.TypeCache;
27  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
28  import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
29  import org.apache.chemistry.opencmis.commons.server.CallContext;
30  import org.apache.chemistry.opencmis.commons.server.CmisService;
31  import org.apache.chemistry.opencmis.commons.spi.Holder;
32  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_CONTENT;
33  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
34  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
35  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
36  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
37  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
38  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
39  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
40  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  
46  /**
47   * MultiFiling Service operations.
48   */
49  public class MultiFilingService
50  {
51      /*
52       * addObjectToFolder.
53       */
54      public static void addObjectToFolder( CallContext context, CmisService service, String repositoryId,
55          HttpServletRequest request, HttpServletResponse response )
56          throws Exception
57      {
58          // get parameters
59          String objectId = (String) context.get( CONTEXT_OBJECT_ID );
60          String folderId = getStringParameter( request, PARAM_FOLDER_ID );
61          Boolean allVersions = getBooleanParameter( request, Constants.PARAM_ALL_VERSIONS );
62  
63          // execute
64          Holder<String> objectIdHolder = new Holder<String>( objectId );
65          service.addObjectToFolder( repositoryId, objectId, folderId, allVersions, null );
66  
67          String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
68  
69          ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
70  
71          if ( object == null )
72          {
73              throw new CmisRuntimeException( "Object is null!" );
74          }
75  
76          // set headers
77          String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
78  
79          setStatus( request, response, HttpServletResponse.SC_CREATED );
80          response.setHeader( "Location", location );
81  
82          // return object
83          TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
84          JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
85  
86          writeJSON( jsonObject, request, response );
87      }
88  
89      /*
90       * removeObjectFromFolder.
91       */
92      public static void removeObjectFromFolder( CallContext context, CmisService service, String repositoryId,
93          HttpServletRequest request, HttpServletResponse response )
94          throws Exception
95      {
96          // get parameters
97          String objectId = (String) context.get( CONTEXT_OBJECT_ID );
98          String folderId = getStringParameter( request, PARAM_FOLDER_ID );
99  
100         // execute
101         Holder<String> objectIdHolder = new Holder<String>( objectId );
102         service.removeObjectFromFolder( repositoryId, objectId, folderId, null );
103 
104         String newObjectId = ( ( objectIdHolder.getValue(  ) == null ) ? objectId : objectIdHolder.getValue(  ) );
105 
106         ObjectData object = getSimpleObject( service, repositoryId, newObjectId );
107 
108         if ( object == null )
109         {
110             throw new CmisRuntimeException( "Object is null!" );
111         }
112 
113         // set headers
114         String location = compileUrl( compileBaseUrl( request, repositoryId ), RESOURCE_CONTENT, newObjectId );
115 
116         setStatus( request, response, HttpServletResponse.SC_CREATED );
117         response.setHeader( "Location", location );
118 
119         // return object
120         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
121         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
122 
123         writeJSON( jsonObject, request, response );
124     }
125 }