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 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
45
46 public class PolicyServiceImpl extends AbstractAtomPubService implements PolicyService {
47
48
49
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
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(policyId), getCmisVersion(repositoryId));
68
69
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
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
94 Response resp = read(url);
95 AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
96
97
98 if (!feed.getEntries().isEmpty()) {
99 for (AtomEntry entry : feed.getEntries()) {
100 ObjectData policy = null;
101
102
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
121 if (policyId == null) {
122 throw new CmisInvalidArgumentException("Policy id must be set!");
123 }
124
125
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
136 Response resp = read(url);
137 AtomFeed feed = parse(resp.getStream(), AtomFeed.class);
138
139
140 String policyLink = null;
141 boolean found = false;
142
143 if (!feed.getEntries().isEmpty()) {
144 for (AtomEntry entry : feed.getEntries()) {
145
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
167 if (found && (policyLink != null)) {
168 delete(new UrlBuilder(policyLink));
169 }
170 }
171 }