This project has retired. For details please refer to its
Attic page.
AtomDocumentBase 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 java.io.UnsupportedEncodingException;
22 import java.math.BigInteger;
23 import java.net.URLEncoder;
24 import java.text.SimpleDateFormat;
25 import java.util.GregorianCalendar;
26 import java.util.Locale;
27 import java.util.TimeZone;
28
29 import javax.xml.stream.XMLStreamException;
30 import javax.xml.stream.XMLStreamWriter;
31
32 import org.apache.chemistry.opencmis.commons.impl.Constants;
33 import org.apache.commons.codec.binary.Base64;
34
35
36
37
38 public abstract class AtomDocumentBase extends XMLDocumentBase {
39
40 private static final String ID_PREFIX = "http://chemistry.apache.org/";
41 private static final String ID_DUMMY = "http://chemistry.apache.org/no-id";
42
43 private SimpleDateFormat dateFormater;
44
45
46
47
48 public String formatDate(long millis) {
49 if (dateFormater == null) {
50 dateFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
51 dateFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
52 }
53
54 return dateFormater.format(millis);
55 }
56
57
58
59
60 public String generateAtomId(String input) {
61 if (input == null) {
62 return ID_DUMMY;
63 }
64
65 try {
66 return ID_PREFIX + (new String(Base64.encodeBase64(input.getBytes("UTF-8"))));
67 } catch (UnsupportedEncodingException e) {
68 return ID_DUMMY;
69 }
70 }
71
72
73
74
75 public void writeSimpleTag(String namespace, String name, String value) throws XMLStreamException {
76 if (value == null) {
77 return;
78 }
79
80 XMLStreamWriter xsw = getWriter();
81
82 xsw.writeStartElement(namespace, name);
83 xsw.writeCharacters(value);
84 xsw.writeEndElement();
85 }
86
87
88
89
90 public void writeSimpleDate(String namespace, String name, GregorianCalendar value) throws XMLStreamException {
91 if (value == null) {
92 return;
93 }
94
95 writeSimpleTag(namespace, name, formatDate(value.getTimeInMillis()));
96 }
97
98
99
100
101 public void writeSimpleDate(String namespace, String name, long millis) throws XMLStreamException {
102 writeSimpleTag(namespace, name, formatDate(millis));
103 }
104
105
106
107
108 public void writeId(String id) throws XMLStreamException {
109 writeSimpleTag(Constants.NAMESPACE_ATOM, "id", id);
110 }
111
112
113
114
115 public void writeTitle(String title) throws XMLStreamException {
116 writeSimpleTag(Constants.NAMESPACE_ATOM, "title", title);
117 }
118
119
120
121
122 public void writeAuthor(String author) throws XMLStreamException {
123 XMLStreamWriter xsw = getWriter();
124
125 xsw.writeStartElement(Constants.NAMESPACE_ATOM, "author");
126 writeSimpleTag(Constants.NAMESPACE_ATOM, "name", author);
127 xsw.writeEndElement();
128 }
129
130
131
132
133 public void writeUpdated(GregorianCalendar updated) throws XMLStreamException {
134 writeSimpleDate(Constants.NAMESPACE_APP, "edited", updated);
135 writeSimpleDate(Constants.NAMESPACE_ATOM, "updated", updated);
136 }
137
138
139
140
141 public void writeUpdated(long updated) throws XMLStreamException {
142 writeSimpleDate(Constants.NAMESPACE_APP, "edited", updated);
143 writeSimpleDate(Constants.NAMESPACE_ATOM, "updated", updated);
144 }
145
146
147
148
149 public void writePublished(GregorianCalendar published) throws XMLStreamException {
150 writeSimpleDate(Constants.NAMESPACE_ATOM, "published", published);
151 }
152
153
154
155
156 public void writePublished(long published) throws XMLStreamException {
157 writeSimpleDate(Constants.NAMESPACE_ATOM, "published", published);
158 }
159
160
161
162
163 public void writePathSegment(String pathSegment) throws XMLStreamException {
164 writeSimpleTag(Constants.NAMESPACE_RESTATOM, "pathSegment", pathSegment);
165 }
166
167
168
169
170 public void writeRelativePathSegment(String relativePathSegment) throws XMLStreamException {
171 writeSimpleTag(Constants.NAMESPACE_RESTATOM, "relativePathSegment", relativePathSegment);
172 }
173
174
175
176
177 public void writeCollection(String href, String collectionType, String text, String... accept)
178 throws XMLStreamException {
179 XMLStreamWriter xsw = getWriter();
180
181 xsw.writeStartElement(Constants.NAMESPACE_APP, "collection");
182 xsw.writeAttribute("href", href);
183
184 if (collectionType != null) {
185 xsw.writeStartElement(Constants.NAMESPACE_RESTATOM, "collectionType");
186 xsw.writeCharacters(collectionType);
187 xsw.writeEndElement();
188 }
189
190 xsw.writeStartElement(Constants.NAMESPACE_ATOM, "title");
191 xsw.writeAttribute("type", "text");
192 xsw.writeCharacters(text);
193 xsw.writeEndElement();
194
195 for (String ct : accept) {
196 xsw.writeStartElement(Constants.NAMESPACE_APP, "accept");
197 xsw.writeCharacters(ct);
198 xsw.writeEndElement();
199 }
200
201 xsw.writeEndElement();
202 }
203
204
205
206
207 public void writeLink(String rel, String href, String type, String id) throws XMLStreamException {
208 XMLStreamWriter xsw = getWriter();
209
210 xsw.writeStartElement(Constants.NAMESPACE_ATOM, "link");
211
212 xsw.writeAttribute("rel", rel);
213 xsw.writeAttribute("href", href);
214 if (type != null) {
215 xsw.writeAttribute("type", type);
216 }
217 if (id != null) {
218 xsw.writeAttribute(Constants.NAMESPACE_RESTATOM, "id", id);
219 }
220
221 xsw.writeEndElement();
222 }
223
224 public void writeServiceLink(String href, String repositoryId) throws XMLStreamException {
225 try {
226 writeLink(Constants.REL_SERVICE, href + "?repositoryId=" + URLEncoder.encode(repositoryId, "UTF-8"),
227 Constants.MEDIATYPE_SERVICE, null);
228 } catch (UnsupportedEncodingException e) {
229 }
230 }
231
232 public void writeSelfLink(String href, String id) throws XMLStreamException {
233 writeLink(Constants.REL_SELF, href, Constants.MEDIATYPE_ENTRY, id);
234 }
235
236 public void writeEnclosureLink(String href) throws XMLStreamException {
237 writeLink(Constants.REL_ENCLOSURE, href, Constants.MEDIATYPE_ENTRY, null);
238 }
239
240 public void writeEditLink(String href) throws XMLStreamException {
241 writeLink(Constants.REL_EDIT, href, Constants.MEDIATYPE_ENTRY, null);
242 }
243
244 public void writeAlternateLink(String href, String type, String kind, String title, BigInteger length)
245 throws XMLStreamException {
246 XMLStreamWriter xsw = getWriter();
247
248 xsw.writeStartElement(Constants.NAMESPACE_ATOM, "link");
249
250 xsw.writeAttribute("rel", Constants.REL_ALTERNATE);
251 xsw.writeAttribute("href", href);
252 if (type != null) {
253 xsw.writeAttribute("type", type);
254 }
255 if (kind != null) {
256 xsw.writeAttribute(Constants.NAMESPACE_RESTATOM, "renditionKind", kind);
257 }
258 if (title != null) {
259 xsw.writeAttribute("title", title);
260 }
261 if (length != null) {
262 xsw.writeAttribute("length", length.toString());
263 }
264
265 xsw.writeEndElement();
266 }
267
268 public void writeWorkingCopyLink(String href) throws XMLStreamException {
269 writeLink(Constants.REL_WORKINGCOPY, href, Constants.MEDIATYPE_ENTRY, null);
270 }
271
272 public void writeUpLink(String href, String type) throws XMLStreamException {
273 writeLink(Constants.REL_UP, href, type, null);
274 }
275
276 public void writeDownLink(String href, String type) throws XMLStreamException {
277 writeLink(Constants.REL_DOWN, href, type, null);
278 }
279
280 public void writeVersionHistoryLink(String href) throws XMLStreamException {
281 writeLink(Constants.REL_VERSIONHISTORY, href, Constants.MEDIATYPE_FEED, null);
282 }
283
284 public void writeCurrentVerionsLink(String href) throws XMLStreamException {
285 writeLink(Constants.REL_CURRENTVERSION, href, Constants.MEDIATYPE_ENTRY, null);
286 }
287
288 public void writeEditMediaLink(String href, String type) throws XMLStreamException {
289 writeLink(Constants.REL_EDITMEDIA, href, type, null);
290 }
291
292 public void writeDescribedByLink(String href) throws XMLStreamException {
293 writeLink(Constants.REL_DESCRIBEDBY, href, Constants.MEDIATYPE_ENTRY, null);
294 }
295
296 public void writeAllowableActionsLink(String href) throws XMLStreamException {
297 writeLink(Constants.REL_ALLOWABLEACTIONS, href, Constants.MEDIATYPE_ALLOWABLEACTION, null);
298 }
299
300 public void writeAclLink(String href) throws XMLStreamException {
301 writeLink(Constants.REL_ACL, href, Constants.MEDIATYPE_ACL, null);
302 }
303
304 public void writePoliciesLink(String href) throws XMLStreamException {
305 writeLink(Constants.REL_POLICIES, href, Constants.MEDIATYPE_FEED, null);
306 }
307
308 public void writeRelationshipsLink(String href) throws XMLStreamException {
309 writeLink(Constants.REL_RELATIONSHIPS, href, Constants.MEDIATYPE_FEED, null);
310 }
311
312 public void writeRelationshipSourceLink(String href) throws XMLStreamException {
313 writeLink(Constants.REL_SOURCE, href, Constants.MEDIATYPE_ENTRY, null);
314 }
315
316 public void writeRelationshipTargetLink(String href) throws XMLStreamException {
317 writeLink(Constants.REL_TARGET, href, Constants.MEDIATYPE_ENTRY, null);
318 }
319
320 public void writeFolderTreeLink(String href) throws XMLStreamException {
321 writeLink(Constants.REL_FOLDERTREE, href, Constants.MEDIATYPE_DESCENDANTS, null);
322 }
323
324 public void writeTypeUpLink(String href, String type) throws XMLStreamException {
325 writeLink(Constants.REL_UP, href, type, null);
326 }
327
328 public void writeTypeDownLink(String href, String type) throws XMLStreamException {
329 writeLink(Constants.REL_DOWN, href, type, null);
330 }
331
332 public void writeViaLink(String href) throws XMLStreamException {
333 writeLink(Constants.REL_VIA, href, Constants.MEDIATYPE_ENTRY, null);
334 }
335
336 public void writeFirstLink(String href) throws XMLStreamException {
337 writeLink(Constants.REL_FIRST, href, Constants.MEDIATYPE_FEED, null);
338 }
339
340 public void writeLastLink(String href) throws XMLStreamException {
341 writeLink(Constants.REL_LAST, href, Constants.MEDIATYPE_FEED, null);
342 }
343
344 public void writePreviousLink(String href) throws XMLStreamException {
345 writeLink(Constants.REL_PREV, href, Constants.MEDIATYPE_FEED, null);
346 }
347
348 public void writeNextLink(String href) throws XMLStreamException {
349 writeLink(Constants.REL_NEXT, href, Constants.MEDIATYPE_FEED, null);
350 }
351 }