This project has retired. For details please refer to its Attic page.
AclService 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.commons.impl.Converter.convert;
22  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
23  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
24  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.xml.bind.JAXBElement;
29  import javax.xml.bind.Unmarshaller;
30  
31  import org.apache.chemistry.opencmis.commons.data.Acl;
32  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
33  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
35  import org.apache.chemistry.opencmis.commons.impl.Constants;
36  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
37  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisAccessControlListType;
38  import org.apache.chemistry.opencmis.commons.server.CallContext;
39  import org.apache.chemistry.opencmis.commons.server.CmisService;
40  
41  /**
42   * ACL Service operations.
43   */
44  public class AclService {
45  
46      private AclService() {
47      }
48  
49      /**
50       * Get ACL.
51       */
52      public static void getAcl(CallContext context, CmisService service, String repositoryId,
53              HttpServletRequest request, HttpServletResponse response) throws Exception {
54          // get parameters
55          String objectId = getStringParameter(request, Constants.PARAM_ID);
56          Boolean onlyBasicPermissions = getBooleanParameter(request, Constants.PARAM_ONLY_BASIC_PERMISSIONS);
57  
58          // execute
59          Acl acl = service.getAcl(repositoryId, objectId, onlyBasicPermissions, null);
60  
61          if (acl == null) {
62              throw new CmisRuntimeException("ACL is null!");
63          }
64  
65          // set headers
66          response.setStatus(HttpServletResponse.SC_OK);
67          response.setContentType(Constants.MEDIATYPE_ACL);
68  
69          // write XML
70          AclDocument aclDocument = new AclDocument();
71          aclDocument.writeAcl(acl, response.getOutputStream());
72      }
73  
74      /**
75       * Apply ACL.
76       */
77      public static void applyAcl(CallContext context, CmisService service, String repositoryId,
78              HttpServletRequest request, HttpServletResponse response) throws Exception {
79          // get parameters
80          String objectId = getStringParameter(request, Constants.PARAM_ID);
81          AclPropagation aclPropagation = getEnumParameter(request, Constants.PARAM_ACL_PROPAGATION, AclPropagation.class);
82  
83          Object aclRequest = null;
84          try {
85              Unmarshaller u = JaxBHelper.createUnmarshaller();
86              aclRequest = u.unmarshal(request.getInputStream());
87          } catch (Exception e) {
88              throw new CmisInvalidArgumentException("Invalid ACL request: " + e, e);
89          }
90  
91          if (!(aclRequest instanceof JAXBElement<?>)) {
92              throw new CmisInvalidArgumentException("Not an ACL document!");
93          }
94  
95          if (!(((JAXBElement<?>) aclRequest).getValue() instanceof CmisAccessControlListType)) {
96              throw new CmisInvalidArgumentException("Not an ACL document!");
97          }
98  
99          Acl aces = convert((CmisAccessControlListType) ((JAXBElement<?>) aclRequest).getValue(), null);
100 
101         // execute
102         Acl acl = service.applyAcl(repositoryId, objectId, aces, aclPropagation);
103 
104         // set headers
105         response.setStatus(HttpServletResponse.SC_CREATED);
106         response.setContentType(Constants.MEDIATYPE_ACL);
107 
108         // write XML
109         AclDocument aclDocument = new AclDocument();
110         aclDocument.writeAcl(acl, response.getOutputStream());
111     }
112 }