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.data.ObjectData;
22  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
23  import org.apache.chemistry.opencmis.commons.impl.Constants;
24  import static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
25  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
26  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
27  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisTypeDefinitionType;
28  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
29  
30  import javax.xml.bind.JAXBException;
31  import javax.xml.stream.XMLStreamException;
32  import javax.xml.stream.XMLStreamWriter;
33  
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      /**
50       * Creates an Atom entry that is embedded somewhere.
51       */
52      public AtomEntry( XMLStreamWriter writer )
53      {
54          setWriter( writer );
55      }
56  
57      /**
58       * Opens the entry tag.
59       */
60      public void startEntry( boolean isRoot ) throws XMLStreamException
61      {
62          getWriter(  ).writeStartElement( Constants.NAMESPACE_ATOM, "entry" );
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       * Closes the entry tag.
75       */
76      public void endEntry(  ) throws XMLStreamException
77      {
78          getWriter(  ).writeEndElement(  );
79      }
80  
81      /**
82       * Writes an object.
83       */
84      public void writeObject( ObjectData object, ObjectInfo info, String contentSrc, String contentType,
85          String pathSegment, String relativePathSegment )
86          throws XMLStreamException, JAXBException
87      {
88          CmisObjectType objectJaxb = convert( object );
89  
90          if ( objectJaxb == null )
91          {
92              return;
93          }
94  
95          writeAuthor( info.getCreatedBy(  ) );
96          writeId( generateAtomId( info.getId(  ) ) );
97          writePublished( info.getCreationDate(  ) );
98          writeTitle( info.getName(  ) );
99          writeUpdated( info.getLastModificationDate(  ) );
100 
101         writeContent( contentSrc, contentType );
102 
103         JaxBHelper.marshal( JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createObject( objectJaxb ), getWriter(  ), true );
104 
105         writePathSegment( pathSegment );
106         writeRelativePathSegment( relativePathSegment );
107     }
108 
109     /**
110      * Writes a delete object.
111      */
112     public void writeDeletedObject( ObjectData object )
113         throws XMLStreamException, JAXBException
114     {
115         CmisObjectType objectJaxb = convert( object );
116 
117         if ( objectJaxb == null )
118         {
119             return;
120         }
121 
122         long now = System.currentTimeMillis(  );
123 
124         writeAuthor( DEFAULT_AUTHOR );
125         writeId( generateAtomId( object.getId(  ) ) );
126         writePublished( now );
127         writeTitle( object.getId(  ) );
128         writeUpdated( now );
129 
130         JaxBHelper.marshal( JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createObject( objectJaxb ), getWriter(  ), true );
131     }
132 
133     /**
134      * Writes a type.
135      *
136      * @throws JAXBException
137      */
138     public void writeType( TypeDefinition type ) throws XMLStreamException, JAXBException
139     {
140         CmisTypeDefinitionType typeJaxb = convert( type );
141 
142         if ( typeJaxb == null )
143         {
144             return;
145         }
146 
147         long now = System.currentTimeMillis(  );
148 
149         writeAuthor( DEFAULT_AUTHOR );
150         writeId( generateAtomId( type.getId(  ) ) );
151         writeTitle( type.getDisplayName(  ) );
152         writeUpdated( now );
153 
154         JaxBHelper.marshal( JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createTypeDefinition( typeJaxb ), getWriter(  ), true );
155     }
156 
157     /**
158      * Writes a content tag.
159      */
160     public void writeContent( String src, String type )
161         throws XMLStreamException
162     {
163         if ( src == null )
164         {
165             return;
166         }
167 
168         XMLStreamWriter xsw = getWriter(  );
169         xsw.writeStartElement( Constants.NAMESPACE_ATOM, "content" );
170 
171         xsw.writeAttribute( "src", src );
172 
173         if ( type != null )
174         {
175             xsw.writeAttribute( "type", type );
176         }
177 
178         xsw.writeEndElement(  );
179     }
180 }