This project has retired. For details please refer to its Attic page.
AtomEntryWriter 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.client.bindings.spi.atompub;
20  
21  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_ATOM_ID;
22  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_ATOM_TITLE;
23  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_ATOM_UPDATED;
24  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_CONTENT;
25  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_CONTENT_BASE64;
26  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_CONTENT_MEDIATYPE;
27  import static org.apache.chemistry.opencmis.client.bindings.spi.atompub.CmisAtomPubConstants.TAG_ENTRY;
28  
29  import java.io.BufferedInputStream;
30  import java.io.ByteArrayInputStream;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.text.SimpleDateFormat;
34  import java.util.Date;
35  import java.util.List;
36  import java.util.TimeZone;
37  
38  import javax.xml.stream.XMLOutputFactory;
39  import javax.xml.stream.XMLStreamWriter;
40  
41  import org.apache.chemistry.opencmis.commons.PropertyIds;
42  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
43  import org.apache.chemistry.opencmis.commons.impl.Base64;
44  import org.apache.chemistry.opencmis.commons.impl.Constants;
45  import org.apache.chemistry.opencmis.commons.impl.JaxBHelper;
46  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
47  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisProperty;
48  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyString;
49  
50  /**
51   * Writes a CMIS Atom entry to an output stream.
52   */
53  public class AtomEntryWriter {
54  
55      private static final String PREFIX_ATOM = "atom";
56      private static final String PREFIX_CMIS = "cmis";
57      private static final String PREFIX_RESTATOM = "cmisra";
58  
59      private static final int BUFFER_SIZE = 64 * 1024;
60  
61      private final CmisObjectType object;
62      private final InputStream stream;
63      private final String mediaType;
64  
65      /**
66       * Constructor.
67       */
68      public AtomEntryWriter(CmisObjectType object) {
69          this(object, null, null);
70      }
71  
72      /**
73       * Constructor.
74       */
75      public AtomEntryWriter(CmisObjectType object, String mediaType, InputStream stream) {
76          if ((object == null) || (object.getProperties() == null)) {
77              throw new CmisInvalidArgumentException("Object and properties must not be null!");
78          }
79  
80          if ((stream != null) && (mediaType == null)) {
81              throw new CmisInvalidArgumentException("Media type must be set if a stream is present!");
82          }
83  
84          this.object = object;
85          this.mediaType = mediaType;
86  
87          if (stream != null && !(stream instanceof BufferedInputStream) && !(stream instanceof ByteArrayInputStream)) {
88              // avoid double buffering
89              stream = new BufferedInputStream(stream, BUFFER_SIZE);
90          }
91  
92          this.stream = stream;
93      }
94  
95      /**
96       * Writes the entry to an output stream.
97       */
98      public void write(OutputStream out) throws Exception {
99          XMLOutputFactory factory = XMLOutputFactory.newInstance();
100         XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8");
101 
102         writer.setPrefix(PREFIX_ATOM, Constants.NAMESPACE_ATOM);
103         writer.setPrefix(PREFIX_CMIS, Constants.NAMESPACE_CMIS);
104         writer.setPrefix(PREFIX_RESTATOM, Constants.NAMESPACE_RESTATOM);
105 
106         // start doc
107         writer.writeStartDocument();
108 
109         // start entry
110         writer.writeStartElement(Constants.NAMESPACE_ATOM, TAG_ENTRY);
111         writer.writeNamespace(PREFIX_ATOM, Constants.NAMESPACE_ATOM);
112         writer.writeNamespace(PREFIX_CMIS, Constants.NAMESPACE_CMIS);
113         writer.writeNamespace(PREFIX_RESTATOM, Constants.NAMESPACE_RESTATOM);
114 
115         // atom:id
116         writer.writeStartElement(Constants.NAMESPACE_ATOM, TAG_ATOM_ID);
117         writer.writeCharacters("urn:uuid:00000000-0000-0000-0000-00000000000");
118         writer.writeEndElement();
119 
120         // atom:title
121         writer.writeStartElement(Constants.NAMESPACE_ATOM, TAG_ATOM_TITLE);
122         writer.writeCharacters(getTitle());
123         writer.writeEndElement();
124 
125         // atom:updated
126         writer.writeStartElement(Constants.NAMESPACE_ATOM, TAG_ATOM_UPDATED);
127         writer.writeCharacters(getUpdated());
128         writer.writeEndElement();
129 
130         // content
131         if (stream != null) {
132             writer.writeStartElement(Constants.NAMESPACE_RESTATOM, TAG_CONTENT);
133 
134             writer.writeStartElement(Constants.NAMESPACE_RESTATOM, TAG_CONTENT_MEDIATYPE);
135             writer.writeCharacters(mediaType);
136             writer.writeEndElement();
137 
138             writer.writeStartElement(Constants.NAMESPACE_RESTATOM, TAG_CONTENT_BASE64);
139             writeContent(writer);
140             writer.writeEndElement();
141 
142             writer.writeEndElement();
143         }
144 
145         // object
146         JaxBHelper.marshal(JaxBHelper.CMIS_EXTRA_OBJECT_FACTORY.createObject(object), writer, true);
147 
148         // end entry
149         writer.writeEndElement();
150 
151         // end document
152         writer.writeEndDocument();
153 
154         writer.flush();
155     }
156 
157     // ---- internal ----
158 
159     private String getTitle() {
160         String result = "";
161 
162         for (CmisProperty property : object.getProperties().getProperty()) {
163             if (PropertyIds.NAME.equals(property.getPropertyDefinitionId()) && (property instanceof CmisPropertyString)) {
164                 List<String> values = ((CmisPropertyString) property).getValue();
165                 if (!values.isEmpty()) {
166                     return values.get(0);
167                 }
168             }
169         }
170 
171         return result;
172     }
173 
174     private static String getUpdated() {
175         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
176         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
177 
178         return sdf.format(new Date());
179     }
180 
181     private void writeContent(XMLStreamWriter writer) throws Exception {
182         Base64.InputStream b64stream = new Base64.InputStream(stream, Base64.ENCODE);
183 
184         byte[] buffer = new byte[BUFFER_SIZE * 3 / 4];
185         int b;
186         while ((b = b64stream.read(buffer)) > -1) {
187             if (b > 0) {
188                 writer.writeCharacters(new String(buffer, 0, b, "US-ASCII"));
189             }
190         }
191         
192         b64stream.close();
193     }
194 }