This project has retired. For details please refer to its
Attic page.
PolicyServiceImpl 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.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
46
47 public class PolicyServiceImpl extends AbstractAtomPubService implements PolicyService {
48
49
50
51
52 public PolicyServiceImpl(BindingSession session) {
53 setSession(session);
54 }
55
56 public void applyPolicy(String repositoryId, String policyId, String objectId, ExtensionsData extension) {
57
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
67 final AtomEntryWriter entryWriter = new AtomEntryWriter(createIdObject(objectId));
68
69
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
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
92 HttpUtils.Response resp = read(url);
93 AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
94
95
96 if (!feed.getEntries().isEmpty()) {
97 for (AtomEntry entry : feed.getEntries()) {
98 ObjectData policy = null;
99
100
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
118 if (policyId == null) {
119 throw new CmisInvalidArgumentException("Policy id must be set!");
120 }
121
122
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
133 HttpUtils.Response resp = read(url);
134 AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
135
136
137 String policyLink = null;
138 boolean found = false;
139
140 if (!feed.getEntries().isEmpty()) {
141 for (AtomEntry entry : feed.getEntries()) {
142
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
164 if (found && (policyLink != null)) {
165 delete(new UrlBuilder(policyLink));
166 }
167 }
168
169
170
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 }