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