This project has retired. For details please refer to its Attic page.
PolicyServiceImpl 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.client.bindings.spi.browser;
20  
21  import java.io.OutputStream;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
26  import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils;
27  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
28  import org.apache.chemistry.opencmis.commons.data.ObjectData;
29  import org.apache.chemistry.opencmis.commons.impl.Constants;
30  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
31  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
32  import org.apache.chemistry.opencmis.commons.spi.PolicyService;
33  
34  /**
35   * Policy Service Browser Binding client.
36   */
37  public class PolicyServiceImpl extends AbstractBrowserBindingService implements PolicyService {
38  
39      /**
40       * Constructor.
41       */
42      public PolicyServiceImpl(BindingSession session) {
43          setSession(session);
44      }
45  
46      public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
47          // build URL
48          UrlBuilder url = getObjectUrl(repositoryId, objectId);
49  
50          // prepare form data
51          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_APPLY_POLICY);
52          formData.addPoliciesParameters(Collections.singletonList(policyId));
53  
54          // send
55          postAndConsume(url, formData.getContentType(), new HttpUtils.Output() {
56              public void write(OutputStream out) throws Exception {
57                  formData.write(out);
58              }
59          });
60      }
61  
62      public void removePolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
63          // build URL
64          UrlBuilder url = getObjectUrl(repositoryId, objectId);
65  
66          // prepare form data
67          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_REMOVE_POLICY);
68          formData.addPoliciesParameters(Collections.singletonList(policyId));
69  
70          // send
71          postAndConsume(url, formData.getContentType(), new HttpUtils.Output() {
72              public void write(OutputStream out) throws Exception {
73                  formData.write(out);
74              }
75          });
76      }
77  
78      public List<ObjectData> getAppliedPolicies(String repositoryId, String objectId, String filter,
79              ExtensionsData extension) {
80          // build URL
81          UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_POLICIES);
82          url.addParameter(Constants.PARAM_FILTER, filter);
83  
84          // read and parse
85          HttpUtils.Response resp = read(url);
86          List<Object> json = parseArray(resp.getStream(), resp.getCharset());
87  
88          return JSONConverter.convertObjects(json);
89      }
90  }