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.List;
24  
25  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
26  import org.apache.chemistry.opencmis.client.bindings.spi.http.Output;
27  import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
28  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
29  import org.apache.chemistry.opencmis.commons.data.ObjectData;
30  import org.apache.chemistry.opencmis.commons.impl.Constants;
31  import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
32  import org.apache.chemistry.opencmis.commons.impl.TypeCache;
33  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
34  import org.apache.chemistry.opencmis.commons.spi.PolicyService;
35  
36  /**
37   * Policy Service Browser Binding client.
38   */
39  public class PolicyServiceImpl extends AbstractBrowserBindingService implements PolicyService {
40  
41      /**
42       * Constructor.
43       */
44      public PolicyServiceImpl(BindingSession session) {
45          setSession(session);
46      }
47  
48      @Override
49      public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
50          // build URL
51          UrlBuilder url = getObjectUrl(repositoryId, objectId);
52  
53          // prepare form data
54          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_APPLY_POLICY);
55          formData.addPolicyIdParameter(policyId);
56  
57          // send
58          postAndConsume(url, formData.getContentType(), new Output() {
59              @Override
60              public void write(OutputStream out) throws IOException {
61                  formData.write(out);
62              }
63          });
64      }
65  
66      @Override
67      public void removePolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
68          // build URL
69          UrlBuilder url = getObjectUrl(repositoryId, objectId);
70  
71          // prepare form data
72          final FormDataWriter formData = new FormDataWriter(Constants.CMISACTION_REMOVE_POLICY);
73          formData.addPolicyIdParameter(policyId);
74  
75          // send
76          postAndConsume(url, formData.getContentType(), new Output() {
77              @Override
78              public void write(OutputStream out) throws IOException {
79                  formData.write(out);
80              }
81          });
82      }
83  
84      @Override
85      public List<ObjectData> getAppliedPolicies(String repositoryId, String objectId, String filter,
86              ExtensionsData extension) {
87          // build URL
88          UrlBuilder url = getObjectUrl(repositoryId, objectId, Constants.SELECTOR_POLICIES);
89          url.addParameter(Constants.PARAM_FILTER, filter);
90          url.addParameter(Constants.PARAM_SUCCINCT, getSuccinctParameter());
91          url.addParameter(Constants.PARAM_DATETIME_FORMAT, getDateTimeFormatParameter());
92  
93          // read and parse
94          Response resp = read(url);
95          List<Object> json = parseArray(resp.getStream(), resp.getCharset());
96  
97          TypeCache typeCache = new ClientTypeCacheImpl(repositoryId, this);
98  
99          return JSONConverter.convertObjects(json, typeCache);
100     }
101 }