This project has retired. For details please refer to its
Attic page.
PolicyService xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45 public class PolicyService {
46
47 private PolicyService() {
48 }
49
50
51
52
53 public static void getAppliedPolicies(CallContext context, CmisService service, String repositoryId,
54 HttpServletRequest request, HttpServletResponse response) throws Exception {
55
56 String objectId = getStringParameter(request, Constants.PARAM_ID);
57 String filter = getStringParameter(request, Constants.PARAM_FILTER);
58
59
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
72 response.setStatus(HttpServletResponse.SC_OK);
73 response.setContentType(Constants.MEDIATYPE_FEED);
74
75
76 AtomFeed feed = new AtomFeed();
77 feed.startDocument(response.getOutputStream());
78 feed.startFeed(true);
79
80
81 feed.writeFeedElements(objectInfo.getId(), objectInfo.getCreatedBy(), objectInfo.getName(),
82 objectInfo.getLastModificationDate(), null, null);
83
84
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
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
101 feed.endFeed();
102 feed.endDocument();
103 }
104
105
106
107
108 public static void applyPolicy(CallContext context, CmisService service, String repositoryId,
109 HttpServletRequest request, HttpServletResponse response) throws Exception {
110
111 String objectId = getStringParameter(request, Constants.PARAM_ID);
112
113 AtomEntryParser parser = new AtomEntryParser(request.getInputStream(), context.getTempDirectory(),
114 context.getMemoryThreshold());
115
116
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
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
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
148
149 public static void removePolicy(CallContext context, CmisService service, String repositoryId,
150 HttpServletRequest request, HttpServletResponse response) {
151
152 String objectId = getStringParameter(request, Constants.PARAM_ID);
153 String policyId = getStringParameter(request, Constants.PARAM_POLICY_ID);
154
155
156 service.removePolicy(repositoryId, policyId, objectId, null);
157
158
159 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
160 }
161
162
163
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
178 entry.startEntry(false);
179
180
181 entry.writeObject(policy, info, null, null, null, null);
182
183
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
189 entry.endEntry();
190 }
191 }