This project has retired. For details please refer to its Attic page.
PolicyService 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.browser;
20  
21  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FILTER;
22  import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_POLICY_ID;
23  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
24  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
25  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
26  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
27  
28  import java.util.List;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.chemistry.opencmis.commons.data.ObjectData;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
35  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
36  import org.apache.chemistry.opencmis.commons.impl.TypeCache;
37  import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
38  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
39  import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
40  import org.apache.chemistry.opencmis.commons.server.CallContext;
41  import org.apache.chemistry.opencmis.commons.server.CmisService;
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) throws Exception {
53          // get parameters
54          String objectId = (String) context.get(CONTEXT_OBJECT_ID);
55          String filter = getStringParameter(request, PARAM_FILTER);
56  
57          // execute
58          List<ObjectData> policies = service.getAppliedPolicies(repositoryId, objectId, filter, null);
59  
60          JSONArray jsonPolicies = new JSONArray();
61          if (policies != null) {
62              TypeCache typeCache = new TypeCacheImpl(repositoryId, service);
63              for (ObjectData policy : policies) {
64                  jsonPolicies.add(JSONConverter.convert(policy, typeCache, false));
65              }
66          }
67  
68          response.setStatus(HttpServletResponse.SC_OK);
69          writeJSON(jsonPolicies, request, response);
70      }
71  
72      /**
73       * applyPolicy.
74       */
75      public static void applyPolicy(CallContext context, CmisService service, String repositoryId,
76              HttpServletRequest request, HttpServletResponse response) throws Exception {
77          // get parameters
78          String objectId = (String) context.get(CONTEXT_OBJECT_ID);
79          String policyId = getStringParameter(request, PARAM_POLICY_ID);
80  
81          // execute
82          service.applyPolicy(repositoryId, policyId, objectId, null);
83  
84          ObjectData object = getSimpleObject(service, repositoryId, objectId);
85          if (object == null) {
86              throw new CmisRuntimeException("Object is null!");
87          }
88  
89          // return object
90          response.setStatus(HttpServletResponse.SC_OK);
91  
92          TypeCache typeCache = new TypeCacheImpl(repositoryId, service);
93          JSONObject jsonObject = JSONConverter.convert(object, typeCache, false);
94  
95          writeJSON(jsonObject, request, response);
96      }
97  
98      /**
99       * removePolicy.
100      */
101     public static void removePolicy(CallContext context, CmisService service, String repositoryId,
102             HttpServletRequest request, HttpServletResponse response) throws Exception {
103         // get parameters
104         String objectId = (String) context.get(CONTEXT_OBJECT_ID);
105         String policyId = getStringParameter(request, PARAM_POLICY_ID);
106 
107         // execute
108         service.removePolicy(repositoryId, policyId, objectId, null);
109 
110         ObjectData object = getSimpleObject(service, repositoryId, objectId);
111         if (object == null) {
112             throw new CmisRuntimeException("Object is null!");
113         }
114 
115         // return object
116         response.setStatus(HttpServletResponse.SC_OK);
117 
118         TypeCache typeCache = new TypeCacheImpl(repositoryId, service);
119         JSONObject jsonObject = JSONConverter.convert(object, typeCache, false);
120 
121         writeJSON(jsonObject, request, response);
122     }
123 }