View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl.atompub;
20  
21  import org.apache.chemistry.opencmis.commons.impl.Constants;
22  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
23  
24  import java.math.BigInteger;
25  
26  import java.util.GregorianCalendar;
27  
28  import javax.xml.stream.XMLStreamException;
29  import javax.xml.stream.XMLStreamWriter;
30  
31  
32  /**
33   * Atom Feed class.
34   *
35   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
36   *
37   */
38  public class AtomFeed extends AtomDocumentBase
39  {
40      public static final BigInteger DEFAULT_PAGE_SIZE = BigInteger.valueOf( 100 );
41  
42      /**
43       * Creates an Atom feed document.
44       */
45      public AtomFeed(  )
46      {
47      }
48  
49      /**
50       * Creates an Atom feed that is embedded somewhere.
51       */
52      public AtomFeed( XMLStreamWriter writer )
53      {
54          setWriter( writer );
55      }
56  
57      /**
58       * Opens the feed tag.
59       */
60      public void startFeed( boolean isRoot ) throws XMLStreamException
61      {
62          getWriter(  ).writeStartElement( Constants.NAMESPACE_ATOM, "feed" );
63  
64          if ( isRoot )
65          {
66              writeNamespace( Constants.NAMESPACE_ATOM );
67              writeNamespace( Constants.NAMESPACE_CMIS );
68              writeNamespace( Constants.NAMESPACE_RESTATOM );
69              writeNamespace( Constants.NAMESPACE_APP );
70          }
71      }
72  
73      /**
74       * Opens the children tag.
75       */
76      public void startChildren(  ) throws XMLStreamException
77      {
78          XMLStreamWriter writer = getWriter(  );
79          writer.writeStartElement( Constants.NAMESPACE_RESTATOM, "children" );
80      }
81  
82      /**
83       * Closes the feed tag.
84       */
85      public void endChildren(  ) throws XMLStreamException
86      {
87          getWriter(  ).writeEndElement(  );
88      }
89  
90      /**
91       * Closes the feed tag.
92       */
93      public void endFeed(  ) throws XMLStreamException
94      {
95          getWriter(  ).writeEndElement(  );
96      }
97  
98      /**
99       * Writes the feed elements that are required by Atom.
100      */
101     public void writeFeedElements( String id, String author, String title, GregorianCalendar updated,
102         String pathSegment, BigInteger numItems ) throws XMLStreamException
103     {
104         writeAuthor( author );
105         writeId( generateAtomId( id ) );
106         writeTitle( title );
107         writeUpdated( updated );
108         writePathSegment( pathSegment );
109         writeNumItems( numItems );
110     }
111 
112     /**
113      * Writes a CMIS numItems tag.
114      */
115     public void writeNumItems( BigInteger numItems ) throws XMLStreamException
116     {
117         if ( numItems == null )
118         {
119             return;
120         }
121 
122         writeSimpleTag( Constants.NAMESPACE_RESTATOM, "numItems", numItems.toString(  ) );
123     }
124 
125     /**
126      * Writes paging links.
127      */
128     public void writePagingLinks( UrlBuilder pagingUrl, BigInteger maxItems, BigInteger skipCount, BigInteger numItems,
129         Boolean hasMoreItems, BigInteger pageSize ) throws XMLStreamException
130     {
131         if ( ( skipCount == null ) || ( skipCount.compareTo( BigInteger.ZERO ) == -1 ) )
132         {
133             skipCount = BigInteger.ZERO;
134         }
135 
136         if ( ( maxItems == null ) || ( maxItems.compareTo( BigInteger.ZERO ) == -1 ) )
137         {
138             if ( ( pageSize == null ) || ( pageSize.compareTo( BigInteger.ZERO ) == -1 ) )
139             {
140                 maxItems = DEFAULT_PAGE_SIZE;
141             }
142             else
143             {
144                 maxItems = pageSize;
145             }
146         }
147 
148         // if not first page -> add "first" and "previous" link
149         if ( skipCount.compareTo( BigInteger.ZERO ) == 1 )
150         {
151             // first link
152             UrlBuilder firstLink = new UrlBuilder( pagingUrl );
153             firstLink.addParameter( Constants.PARAM_SKIP_COUNT, "0" );
154             firstLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
155             writeFirstLink( firstLink.toString(  ) );
156 
157             // previous link
158             UrlBuilder previousLink = new UrlBuilder( pagingUrl );
159             previousLink.addParameter( Constants.PARAM_SKIP_COUNT, skipCount.subtract( maxItems ).max( BigInteger.ZERO ) );
160             previousLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
161             writePreviousLink( previousLink.toString(  ) );
162         }
163 
164         // if has more -> add "next" link
165         if ( ( hasMoreItems != null ) && hasMoreItems.booleanValue(  ) )
166         {
167             // next link
168             UrlBuilder nextLink = new UrlBuilder( pagingUrl );
169             nextLink.addParameter( Constants.PARAM_SKIP_COUNT, skipCount.add( maxItems ) );
170             nextLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
171             writeNextLink( nextLink.toString(  ) );
172         }
173 
174         // if not last page -> add "last" link
175         if ( ( numItems != null ) && ( numItems.compareTo( BigInteger.ZERO ) == 1 ) )
176         {
177             BigInteger lastSkip = numItems.subtract( maxItems ).max( BigInteger.ZERO );
178 
179             if ( lastSkip.compareTo( BigInteger.ZERO ) == 1 )
180             {
181                 // last link
182                 UrlBuilder lastLink = new UrlBuilder( pagingUrl );
183                 lastLink.addParameter( Constants.PARAM_SKIP_COUNT, lastSkip );
184                 lastLink.addParameter( Constants.PARAM_MAX_ITEMS, maxItems );
185                 writeLastLink( lastLink.toString(  ) );
186             }
187         }
188     }
189 }