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 static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FILTER;
24  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_POLICY_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.JSONArray;
28  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
29  import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
30  import org.apache.chemistry.opencmis.commons.server.CallContext;
31  import org.apache.chemistry.opencmis.commons.server.CmisService;
32  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
33  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
34  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
35  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
36  
37  import java.util.List;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  
43  /**
44   * Policy Service operations.
45   */
46  public class PolicyService
47  {
48      /**
49       * getAppliedPolicies.
50       */
51      public static void getAppliedPolicies( CallContext context, CmisService service, String repositoryId,
52          HttpServletRequest request, HttpServletResponse response )
53          throws Exception
54      {
55          // get parameters
56          String objectId = (String) context.get( CONTEXT_OBJECT_ID );
57          String filter = getStringParameter( request, PARAM_FILTER );
58  
59          // execute
60          List<ObjectData> policies = service.getAppliedPolicies( repositoryId, objectId, filter, null );
61  
62          JSONArray jsonPolicies = new JSONArray(  );
63  
64          if ( policies != null )
65          {
66              TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
67  
68              for ( ObjectData policy : policies )
69              {
70                  jsonPolicies.add( JSONConverter.convert( policy, typeCache, false ) );
71              }
72          }
73  
74          response.setStatus( HttpServletResponse.SC_OK );
75          writeJSON( jsonPolicies, request, response );
76      }
77  
78      /**
79       * applyPolicy.
80       */
81      public static void applyPolicy( CallContext context, CmisService service, String repositoryId,
82          HttpServletRequest request, HttpServletResponse response )
83          throws Exception
84      {
85          // get parameters
86          String objectId = (String) context.get( CONTEXT_OBJECT_ID );
87          String policyId = getStringParameter( request, PARAM_POLICY_ID );
88  
89          // execute
90          service.applyPolicy( repositoryId, policyId, objectId, null );
91  
92          ObjectData object = getSimpleObject( service, repositoryId, objectId );
93  
94          if ( object == null )
95          {
96              throw new CmisRuntimeException( "Object is null!" );
97          }
98  
99          // return object
100         response.setStatus( HttpServletResponse.SC_OK );
101 
102         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
103         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
104 
105         writeJSON( jsonObject, request, response );
106     }
107 
108     /**
109      * removePolicy.
110      */
111     public static void removePolicy( CallContext context, CmisService service, String repositoryId,
112         HttpServletRequest request, HttpServletResponse response )
113         throws Exception
114     {
115         // get parameters
116         String objectId = (String) context.get( CONTEXT_OBJECT_ID );
117         String policyId = getStringParameter( request, PARAM_POLICY_ID );
118 
119         // execute
120         service.removePolicy( repositoryId, policyId, objectId, null );
121 
122         ObjectData object = getSimpleObject( service, repositoryId, objectId );
123 
124         if ( object == null )
125         {
126             throw new CmisRuntimeException( "Object is null!" );
127         }
128 
129         // return object
130         response.setStatus( HttpServletResponse.SC_OK );
131 
132         TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
133         JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
134 
135         writeJSON( jsonObject, request, response );
136     }
137 }