This project has retired. For details please refer to its Attic page.
AtomEntry 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 static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
22  
23  import javax.xml.bind.JAXBException;
24  import javax.xml.stream.XMLStreamException;
25  import javax.xml.stream.XMLStreamWriter;
26  
27  import org.apache.chemistry.opencmis.commons.data.ObjectData;
28  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
29  import org.apache.chemistry.opencmis.commons.impl.Constants;
30  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
31  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
32  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
33  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
34  
35  /**
36   * Atom Entry class.
37   */
38  public class AtomEntry extends AtomDocumentBase {
39  
40      private static final String DEFAULT_AUTHOR = "unknown";
41  
42      /**
43       * Creates an Atom entry document.
44       */
45      public AtomEntry() {
46      }
47  
48      /**
49       * Creates an Atom entry that is embedded somewhere.
50       */
51      public AtomEntry(XMLStreamWriter writer) {
52          setWriter(writer);
53      }
54  
55      /**
56       * Opens the entry tag.
57       */
58      public void startEntry(boolean isRoot) throws XMLStreamException {
59          getWriter().writeStartElement(Constants.NAMESPACE_ATOM, "entry");
60  
61          if (isRoot) {
62              writeNamespace(Constants.NAMESPACE_ATOM);
63              writeNamespace(Constants.NAMESPACE_CMIS);
64              writeNamespace(Constants.NAMESPACE_RESTATOM);
65              writeNamespace(Constants.NAMESPACE_APP);
66          }
67      }
68  
69      /**
70       * Closes the entry tag.
71       */
72      public void endEntry() throws XMLStreamException {
73          getWriter().writeEndElement();
74      }
75  
76      /**
77       * Writes an object.
78       */
79      public void writeObject(ObjectData object, ObjectInfo info, String contentSrc, String contentType,
80              String pathSegment, String relativePathSegment) throws XMLStreamException, JAXBException {
81          CmisObjectType objectJaxb = convert(object);
82          if (objectJaxb == null) {
83              return;
84          }
85  
86          writeAuthor(info.getCreatedBy());
87          writeId(generateAtomId(info.getId()));
88          writePublished(info.getCreationDate());
89          writeTitle(info.getName());
90          writeUpdated(info.getLastModificationDate());
91  
92          writeContent(contentSrc, contentType);
93  
94          JaxBHelper.marshal(JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createObject(objectJaxb), getWriter(), true);
95  
96          writePathSegment(pathSegment);
97          writeRelativePathSegment(relativePathSegment);
98      }
99  
100     /**
101      * Writes a delete object.
102      */
103     public void writeDeletedObject(ObjectData object) throws XMLStreamException, JAXBException {
104         CmisObjectType objectJaxb = convert(object);
105         if (objectJaxb == null) {
106             return;
107         }
108 
109         long now = System.currentTimeMillis();
110         
111         writeAuthor(DEFAULT_AUTHOR);
112         writeId(generateAtomId(object.getId()));
113         writePublished(now);
114         writeTitle(object.getId());
115         writeUpdated(now);
116 
117         JaxBHelper.marshal(JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createObject(objectJaxb), getWriter(), true);
118     }
119 
120     /**
121      * Writes a type.
122      * 
123      * @throws JAXBException
124      */
125     public void writeType(TypeDefinition type) throws XMLStreamException, JAXBException {
126         CmisTypeDefinitionType typeJaxb = convert(type);
127         if (typeJaxb == null) {
128             return;
129         }
130 
131         long now = System.currentTimeMillis();
132 
133         writeAuthor(DEFAULT_AUTHOR);
134         writeId(generateAtomId(type.getId()));
135         writeTitle(type.getDisplayName());
136         writeUpdated(now);
137 
138         JaxBHelper.marshal(JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createTypeDefinition(typeJaxb), getWriter(), true);
139     }
140 
141     /**
142      * Writes a content tag.
143      */
144     public void writeContent(String src, String type) throws XMLStreamException {
145         if (src == null) {
146             return;
147         }
148 
149         XMLStreamWriter xsw = getWriter();
150         xsw.writeStartElement(Constants.NAMESPACE_ATOM, "content");
151 
152         xsw.writeAttribute("src", src);
153         if (type != null) {
154             xsw.writeAttribute("type", type);
155         }
156 
157         xsw.writeEndElement();
158     }
159 }