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.client.bindings.spi.browser;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.client.bindings.spi.http.Output;
29  import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
30  import org.apache.chemistry.opencmis.commons.data.Ace;
31  import org.apache.chemistry.opencmis.commons.data.Acl;
32  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
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.UrlBuilder;
37  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlListImpl;
38  import org.apache.chemistry.opencmis.commons.spi.AclService;
39  import org.apache.chemistry.opencmis.commons.spi.ExtendedAclService;
40  
41  /**
42   * ACL Service Browser Binding client.
43   */
44  public class AclServiceImpl extends AbstractBrowserBindingService implements AclService, ExtendedAclService {
45  
46      /**
47       * Constructor.
48       */
49      public AclServiceImpl(BindingSession session) {
50          setSession(session);
51      }
52  
53      @Override
54      public Acl getAcl(String repositoryId, String objectId, Boolean onlyBasicPermissions, ExtensionsData extension) {
55          // build URL
56          UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_ACL);
57          url.addParameter(Constants.PARAM_ONLY_BASIC_PERMISSIONS, onlyBasicPermissions);
58  
59          // read and parse
60          Response resp = read(url);
61          Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
62  
63          return JSONConverter.convertAcl(json);
64      }
65  
66      @Override
67      public Acl applyAcl(String repositoryId, String objectId, Acl addAces, Acl removeAces,
68              AclPropagation aclPropagation, ExtensionsData extension) {
69          // build URL
70          UrlBuilder url = getObjectUrl(repositoryId, objectId);
71  
72          // prepare form data
73          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_APPLY_ACL);
74          formData.addAddAcesParameters(addAces);
75          formData.addRemoveAcesParameters(removeAces);
76          formData.addParameter(Constants.PARAM_ACL_PROPAGATION, aclPropagation);
77  
78          // send and parse
79          Response resp = post(url, formData.getContentType(), new Output() {
80              @Override
81              public void write(OutputStream out) throws IOException {
82                  formData.write(out);
83              }
84          });
85          Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
86  
87          return JSONConverter.convertAcl(json);
88      }
89  
90      @Override
91      public Acl setAcl(String repositoryId, String objectId, Acl aces) {
92          Acl currentAcl = getAcl(repositoryId, objectId, false, null);
93  
94          List<Ace> removeAces = new ArrayList<Ace>();
95          if (currentAcl.getAces() != null) {
96              for (Ace ace : currentAcl.getAces()) {
97                  if (ace.isDirect()) {
98                      removeAces.add(ace);
99                  }
100             }
101         }
102 
103         return applyAcl(repositoryId, objectId, aces, new AccessControlListImpl(removeAces), AclPropagation.OBJECTONLY,
104                 null);
105     }
106 }