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