This project has retired. For details please refer to its Attic page.
PolicyService 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.server.impl.atompub;
20  
21  import static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
22  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_POLICIES;
23  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
24  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
25  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrlBuilder;
26  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
27  
28  import java.util.List;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.chemistry.opencmis.commons.data.ObjectData;
34  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
35  import org.apache.chemistry.opencmis.commons.impl.Constants;
36  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
37  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
38  import org.apache.chemistry.opencmis.commons.server.CallContext;
39  import org.apache.chemistry.opencmis.commons.server.CmisService;
40  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
41  
42  /**
43   * Policy Service operations.
44   */
45  public class PolicyService {
46  
47      private PolicyService() {
48      }
49  
50      /**
51       * Get applied policies.
52       */
53      public static void getAppliedPolicies(CallContext context, CmisService service, String repositoryId,
54              HttpServletRequest request, HttpServletResponse response) throws Exception {
55          // get parameters
56          String objectId = getStringParameter(request, Constants.PARAM_ID);
57          String filter = getStringParameter(request, Constants.PARAM_FILTER);
58  
59          // execute
60          List<ObjectData> policies = service.getAppliedPolicies(repositoryId, objectId, filter, null);
61  
62          if (policies == null) {
63              throw new CmisRuntimeException("Policies are null!");
64          }
65  
66          ObjectInfo objectInfo = service.getObjectInfo(repositoryId, objectId);
67          if (objectInfo == null) {
68              throw new CmisRuntimeException("Object Info is missing!");
69          }
70  
71          // set headers
72          response.setStatus(HttpServletResponse.SC_OK);
73          response.setContentType(Constants.MEDIATYPE_FEED);
74  
75          // write XML
76          AtomFeed feed = new AtomFeed();
77          feed.startDocument(response.getOutputStream());
78          feed.startFeed(true);
79  
80          // write basic Atom feed elements
81          feed.writeFeedElements(objectInfo.getId(), objectInfo.getCreatedBy(), objectInfo.getName(),
82                  objectInfo.getLastModificationDate(), null, null);
83  
84          // write links
85          UrlBuilder baseUrl = compileBaseUrl(request, repositoryId);
86  
87          feed.writeServiceLink(baseUrl.toString(), repositoryId);
88  
89          feed.writeSelfLink(compileUrl(baseUrl, RESOURCE_POLICIES, objectInfo.getId()), null);
90  
91          // write entries
92          AtomEntry entry = new AtomEntry(feed.getWriter());
93          for (ObjectData policy : policies) {
94              if (policy == null) {
95                  continue;
96              }
97              writePolicyEntry(service, entry, objectInfo.getId(), policy, repositoryId, baseUrl);
98          }
99  
100         // we are done
101         feed.endFeed();
102         feed.endDocument();
103     }
104 
105     /**
106      * Apply policy.
107      */
108     public static void applyPolicy(CallContext context, CmisService service, String repositoryId,
109             HttpServletRequest request, HttpServletResponse response) throws Exception {
110         // get parameters
111         String objectId = getStringParameter(request, Constants.PARAM_ID);
112 
113         AtomEntryParser parser = new AtomEntryParser(request.getInputStream(), context.getTempDirectory(),
114                 context.getMemoryThreshold());
115 
116         // execute
117         service.applyPolicy(repositoryId, parser.getId(), objectId, null);
118 
119         ObjectInfo objectInfo = service.getObjectInfo(repositoryId, parser.getId());
120         if (objectInfo == null) {
121             throw new CmisRuntimeException("Object Info is missing!");
122         }
123 
124         ObjectData policy = objectInfo.getObject();
125         if (policy == null) {
126             throw new CmisRuntimeException("Policy is null!");
127         }
128 
129         // set headers
130         UrlBuilder baseUrl = compileBaseUrl(request, repositoryId);
131         UrlBuilder location = compileUrlBuilder(baseUrl, RESOURCE_POLICIES, objectId);
132         location.addParameter(Constants.PARAM_POLICY_ID, policy.getId());
133 
134         response.setStatus(HttpServletResponse.SC_CREATED);
135         response.setContentType(Constants.MEDIATYPE_ENTRY);
136         response.setHeader("Content-Location", location.toString());
137         response.setHeader("Location", location.toString());
138 
139         // write XML
140         AtomEntry entry = new AtomEntry();
141         entry.startDocument(response.getOutputStream());
142         writePolicyEntry(service, entry, objectId, policy, repositoryId, baseUrl);
143         entry.endDocument();
144     }
145 
146     /**
147      * Remove policy.
148      */
149     public static void removePolicy(CallContext context, CmisService service, String repositoryId,
150             HttpServletRequest request, HttpServletResponse response) {
151         // get parameters
152         String objectId = getStringParameter(request, Constants.PARAM_ID);
153         String policyId = getStringParameter(request, Constants.PARAM_POLICY_ID);
154 
155         // execute
156         service.removePolicy(repositoryId, policyId, objectId, null);
157 
158         // set headers
159         response.setStatus(HttpServletResponse.SC_NO_CONTENT);
160     }
161 
162     /**
163      * Writes an entry that is attached to an object.
164      */
165     private static void writePolicyEntry(CmisService service, AtomEntry entry, String objectId, ObjectData policy,
166             String repositoryId, UrlBuilder baseUrl) throws Exception {
167         CmisObjectType resultJaxb = convert(policy);
168         if (resultJaxb == null) {
169             return;
170         }
171 
172         ObjectInfo info = service.getObjectInfo(repositoryId, policy.getId());
173         if (info == null) {
174             throw new CmisRuntimeException("Object Info not found!");
175         }
176 
177         // start
178         entry.startEntry(false);
179 
180         // write the object
181         entry.writeObject(policy, info, null, null, null, null);
182 
183         // write links
184         UrlBuilder selfLink = compileUrlBuilder(baseUrl, RESOURCE_POLICIES, objectId);
185         selfLink.addParameter(Constants.PARAM_POLICY_ID, info.getId());
186         entry.writeSelfLink(selfLink.toString(), null);
187 
188         // we are done
189         entry.endEntry();
190     }
191 }