This project has retired. For details please refer to its Attic page.
AtomFeed xref

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 java.math.BigInteger;
22  import java.util.GregorianCalendar;
23  
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.XMLStreamWriter;
26  
27  import org.apache.chemistry.opencmis.commons.impl.Constants;
28  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
29  
30  /**
31   * Atom Feed class.
32   * 
33   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
34   * 
35   */
36  public class AtomFeed extends AtomDocumentBase {
37  
38      public static final BigInteger DEFAULT_PAGE_SIZE = BigInteger.valueOf(100);
39  
40      /**
41       * Creates an Atom feed document.
42       */
43      public AtomFeed() {
44      }
45  
46      /**
47       * Creates an Atom feed that is embedded somewhere.
48       */
49      public AtomFeed(XMLStreamWriter writer) {
50          setWriter(writer);
51      }
52  
53      /**
54       * Opens the feed tag.
55       */
56      public void startFeed(boolean isRoot) throws XMLStreamException {
57          getWriter().writeStartElement(Constants.NAMESPACE_ATOM, "feed");
58  
59          if (isRoot) {
60              writeNamespace(Constants.NAMESPACE_ATOM);
61              writeNamespace(Constants.NAMESPACE_CMIS);
62              writeNamespace(Constants.NAMESPACE_RESTATOM);
63              writeNamespace(Constants.NAMESPACE_APP);
64          }
65      }
66  
67      /**
68       * Opens the children tag.
69       */
70      public void startChildren() throws XMLStreamException {
71          XMLStreamWriter writer = getWriter();
72          writer.writeStartElement(Constants.NAMESPACE_RESTATOM, "children");
73      }
74  
75      /**
76       * Closes the feed tag.
77       */
78      public void endChildren() throws XMLStreamException {
79          getWriter().writeEndElement();
80      }
81  
82      /**
83       * Closes the feed tag.
84       */
85      public void endFeed() throws XMLStreamException {
86          getWriter().writeEndElement();
87      }
88  
89      /**
90       * Writes the feed elements that are required by Atom.
91       */
92      public void writeFeedElements(String id, String author, String title, GregorianCalendar updated,
93              String pathSegment, BigInteger numItems) throws XMLStreamException {
94          writeAuthor(author);
95          writeId(generateAtomId(id));
96          writeTitle(title);
97          writeUpdated(updated);
98          writePathSegment(pathSegment);
99          writeNumItems(numItems);
100     }
101 
102     /**
103      * Writes a CMIS numItems tag.
104      */
105     public void writeNumItems(BigInteger numItems) throws XMLStreamException {
106         if (numItems == null) {
107             return;
108         }
109 
110         writeSimpleTag(Constants.NAMESPACE_RESTATOM, "numItems", numItems.toString());
111     }
112 
113     /**
114      * Writes paging links.
115      */
116     public void writePagingLinks(UrlBuilder pagingUrl, BigInteger maxItems, BigInteger skipCount, BigInteger numItems,
117             Boolean hasMoreItems, BigInteger pageSize) throws XMLStreamException {
118 
119         if ((skipCount == null) || (skipCount.compareTo(BigInteger.ZERO) == -1)) {
120             skipCount = BigInteger.ZERO;
121         }
122 
123         if ((maxItems == null) || (maxItems.compareTo(BigInteger.ZERO) == -1)) {
124             if ((pageSize == null) || (pageSize.compareTo(BigInteger.ZERO) == -1)) {
125                 maxItems = DEFAULT_PAGE_SIZE;
126             } else {
127                 maxItems = pageSize;
128             }
129         }
130 
131         // if not first page -> add "first" and "previous" link
132         if (skipCount.compareTo(BigInteger.ZERO) == 1) {
133             // first link
134             UrlBuilder firstLink = new UrlBuilder(pagingUrl);
135             firstLink.addParameter(Constants.PARAM_SKIP_COUNT, "0");
136             firstLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
137             writeFirstLink(firstLink.toString());
138 
139             // previous link
140             UrlBuilder previousLink = new UrlBuilder(pagingUrl);
141             previousLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.subtract(maxItems).max(BigInteger.ZERO));
142             previousLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
143             writePreviousLink(previousLink.toString());
144         }
145 
146         // if has more -> add "next" link
147         if ((hasMoreItems != null) && hasMoreItems.booleanValue()) {
148             // next link
149             UrlBuilder nextLink = new UrlBuilder(pagingUrl);
150             nextLink.addParameter(Constants.PARAM_SKIP_COUNT, skipCount.add(maxItems));
151             nextLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
152             writeNextLink(nextLink.toString());
153         }
154 
155         // if not last page -> add "last" link
156         if ((numItems != null) && (numItems.compareTo(BigInteger.ZERO) == 1)) {
157             BigInteger lastSkip = numItems.subtract(maxItems).max(BigInteger.ZERO);
158             if (lastSkip.compareTo(BigInteger.ZERO) == 1) {
159                 // last link
160                 UrlBuilder lastLink = new UrlBuilder(pagingUrl);
161                 lastLink.addParameter(Constants.PARAM_SKIP_COUNT, lastSkip);
162                 lastLink.addParameter(Constants.PARAM_MAX_ITEMS, maxItems);
163                 writeLastLink(lastLink.toString());
164             }
165         }
166     }
167 }