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