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.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
22  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createAddAcl;
23  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createRemoveAcl;
24  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
25  import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
26  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
27  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.chemistry.opencmis.commons.data.Acl;
33  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
34  import org.apache.chemistry.opencmis.commons.impl.Constants;
35  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
36  import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
37  import org.apache.chemistry.opencmis.commons.server.CallContext;
38  import org.apache.chemistry.opencmis.commons.server.CmisService;
39  
40  /**
41   * ACL Service operations.
42   */
43  public class AclService {
44  
45      /**
46       * getACL.
47       */
48      public static void getACL(CallContext context, CmisService service, String repositoryId,
49              HttpServletRequest request, HttpServletResponse response) throws Exception {
50          // get parameters
51          String objectId = (String) context.get(CONTEXT_OBJECT_ID);
52          Boolean onlyBasicPermissions = getBooleanParameter(request, Constants.PARAM_ONLY_BASIC_PERMISSIONS);
53  
54          // execute
55          Acl acl = service.getAcl(repositoryId, objectId, onlyBasicPermissions, null);
56  
57          // return ACL
58          response.setStatus(HttpServletResponse.SC_OK);
59  
60          JSONObject jsonObject = JSONConverter.convert(acl);
61          if (jsonObject == null) {
62              jsonObject = new JSONObject();
63          }
64  
65          writeJSON(jsonObject, request, response);
66      }
67  
68      /**
69       * applyACL.
70       */
71      public static void applyACL(CallContext context, CmisService service, String repositoryId,
72              HttpServletRequest request, HttpServletResponse response) throws Exception {
73          // get parameters
74          String objectId = (String) context.get(CONTEXT_OBJECT_ID);
75          AclPropagation aclPropagation = getEnumParameter(request, Constants.PARAM_ACL_PROPAGATION, AclPropagation.class);
76  
77          // execute
78          ControlParser cp = new ControlParser(request);
79  
80          Acl acl = service.applyAcl(repositoryId, objectId, createAddAcl(cp), createRemoveAcl(cp), aclPropagation, null);
81  
82          // return ACL
83          setStatus(request, response, HttpServletResponse.SC_CREATED);
84  
85          JSONObject jsonObject = JSONConverter.convert(acl);
86          if (jsonObject == null) {
87              jsonObject = new JSONObject();
88          }
89  
90          writeJSON(jsonObject, request, response);
91      }
92  }