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.atompub;
20  
21  import static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
22  
23  import java.io.OutputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomElement;
29  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomEntry;
30  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomFeed;
31  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomLink;
32  import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpUtils;
33  import org.apache.chemistry.opencmis.commons.PropertyIds;
34  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
35  import org.apache.chemistry.opencmis.commons.data.ObjectData;
36  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
37  import org.apache.chemistry.opencmis.commons.impl.Constants;
38  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
39  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
40  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisProperty;
41  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyId;
42  import org.apache.chemistry.opencmis.commons.spi.PolicyService;
43  
44  /**
45   * Policy Service AtomPub client.
46   */
47  public class PolicyServiceImpl extends AbstractAtomPubService implements PolicyService {
48  
49      /**
50       * Constructor.
51       */
52      public PolicyServiceImpl(BindingSession session) {
53          setSession(session);
54      }
55  
56      public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
57          // find the link
58          String link = loadLink(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
59  
60          if (link == null) {
61              throwLinkException(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
62          }
63  
64          UrlBuilder url = new UrlBuilder(link);
65  
66          // set up object and writer
67          final AtomEntryWriter entryWriter = new AtomEntryWriter(createIdObject(objectId));
68  
69          // post applyPolicy request
70          post(url, Constants.MEDIATYPE_ENTRY, new HttpUtils.Output() {
71              public void write(OutputStream out) throws Exception {
72                  entryWriter.write(out);
73              }
74          });
75      }
76  
77      public List<ObjectData> getAppliedPolicies(String repositoryId, String objectId, String filter,
78              ExtensionsData extension) {
79          List<ObjectData> result = new ArrayList<ObjectData>();
80  
81          // find the link
82          String link = loadLink(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
83  
84          if (link == null) {
85              throwLinkException(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
86          }
87  
88          UrlBuilder url = new UrlBuilder(link);
89          url.addParameter(Constants.PARAM_FILTER, filter);
90  
91          // read and parse
92          HttpUtils.Response resp = read(url);
93          AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
94  
95          // get the policies
96          if (!feed.getEntries().isEmpty()) {
97              for (AtomEntry entry : feed.getEntries()) {
98                  ObjectData policy = null;
99  
100                 // walk through the entry
101                 for (AtomElement element : entry.getElements()) {
102                     if (element.getObject() instanceof CmisObjectType) {
103                         policy = convert((CmisObjectType) element.getObject());
104                     }
105                 }
106 
107                 if (policy != null) {
108                     result.add(policy);
109                 }
110             }
111         }
112 
113         return result;
114     }
115 
116     public void removePolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
117         // we need a policy id
118         if (policyId == null) {
119             throw new CmisInvalidArgumentException("Policy id must be set!");
120         }
121 
122         // find the link
123         String link = loadLink(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
124 
125         if (link == null) {
126             throwLinkException(repositoryId, objectId, Constants.REL_POLICIES, Constants.MEDIATYPE_FEED);
127         }
128 
129         UrlBuilder url = new UrlBuilder(link);
130         url.addParameter(Constants.PARAM_FILTER, PropertyIds.OBJECT_ID);
131 
132         // read and parse
133         HttpUtils.Response resp = read(url);
134         AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
135 
136         // find the policy
137         String policyLink = null;
138         boolean found = false;
139 
140         if (!feed.getEntries().isEmpty()) {
141             for (AtomEntry entry : feed.getEntries()) {
142                 // walk through the entry
143                 for (AtomElement element : entry.getElements()) {
144                     if (element.getObject() instanceof AtomLink) {
145                         AtomLink atomLink = (AtomLink) element.getObject();
146                         if (Constants.REL_SELF.equals(atomLink.getRel())) {
147                             policyLink = atomLink.getHref();
148                         }
149                     } else if (element.getObject() instanceof CmisObjectType) {
150                         String id = findIdProperty((CmisObjectType) element.getObject());
151                         if (policyId.equals(id)) {
152                             found = true;
153                         }
154                     }
155                 }
156 
157                 if (found) {
158                     break;
159                 }
160             }
161         }
162 
163         // if found, delete it
164         if (found && (policyLink != null)) {
165             delete(new UrlBuilder(policyLink));
166         }
167     }
168 
169     /**
170      * Finds the id property within a CMIS object.
171      */
172     private static String findIdProperty(CmisObjectType object) {
173         if ((object == null) || (object.getProperties() == null)) {
174             return null;
175         }
176 
177         for (CmisProperty property : object.getProperties().getProperty()) {
178             if (PropertyIds.OBJECT_ID.equals(property.getPropertyDefinitionId())
179                     && (property instanceof CmisPropertyId)) {
180                 List<String> values = ((CmisPropertyId) property).getValue();
181                 if (values.size() == 1) {
182                     return values.get(0);
183                 }
184             }
185         }
186 
187         return null;
188     }
189 }