View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.elasticsearch.index.objects.event;
23  
24  import org.opencastproject.elasticsearch.index.objects.IndexObject;
25  import org.opencastproject.util.IoSupport;
26  import org.opencastproject.util.XmlSafeParser;
27  
28  import org.codehaus.jettison.json.JSONException;
29  import org.codehaus.jettison.json.JSONObject;
30  import org.codehaus.jettison.mapped.Configuration;
31  import org.codehaus.jettison.mapped.MappedNamespaceConvention;
32  import org.codehaus.jettison.mapped.MappedXMLStreamReader;
33  import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.xml.sax.SAXException;
37  
38  import java.io.BufferedReader;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.InputStreamReader;
42  import java.io.StringWriter;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import javax.xml.bind.JAXBContext;
47  import javax.xml.bind.JAXBException;
48  import javax.xml.bind.Marshaller;
49  import javax.xml.bind.Unmarshaller;
50  import javax.xml.bind.annotation.XmlAccessType;
51  import javax.xml.bind.annotation.XmlAccessorType;
52  import javax.xml.bind.annotation.XmlElement;
53  import javax.xml.bind.annotation.XmlRootElement;
54  import javax.xml.bind.annotation.XmlType;
55  import javax.xml.stream.XMLStreamException;
56  import javax.xml.stream.XMLStreamReader;
57  import javax.xml.stream.XMLStreamWriter;
58  
59  /**
60   * Object wrapper for a recording comment.
61   */
62  @XmlType(
63          name = "comment",
64          namespace = IndexObject.INDEX_XML_NAMESPACE,
65          propOrder = {
66                  "id", "reason", "text", "resolvedStatus"
67          }
68  )
69  @XmlRootElement(name = "comment", namespace = IndexObject.INDEX_XML_NAMESPACE)
70  @XmlAccessorType(XmlAccessType.NONE)
71  public class Comment implements IndexObject {
72  
73    /** The logger */
74    private static final Logger logger = LoggerFactory.getLogger(Comment.class);
75  
76    /** The document id */
77    public static final String DOCUMENT_TYPE = "comment";
78  
79    /** The name of the surrounding XML tag to wrap a result of multiple comments */
80    public static final String XML_SURROUNDING_TAG = "comments";
81  
82    /** The identifier */
83    @XmlElement(name = "id")
84    private String id = null;
85  
86    /** The organization identifier */
87    @XmlElement(name = "reason")
88    private String reason = null;
89  
90    /** The title */
91    @XmlElement(name = "text")
92    private String text = null;
93  
94    /** The title */
95    @XmlElement(name = "resolvedStatus")
96    private Boolean resolvedStatus = null;
97  
98    /** Context for serializing and deserializing */
99    private static JAXBContext context = null;
100 
101   /**
102    * Required default no arg constructor for JAXB.
103    */
104   public Comment() {
105 
106   }
107 
108   /**
109    * The recording identifier.
110    *
111    * @param id
112    *          the object id
113    * @param reason
114    *          the reason
115    */
116   public Comment(String id, String reason, String text, boolean resolvedStatus) {
117     this.id = id;
118     this.reason = reason;
119     this.text = text;
120     this.resolvedStatus = resolvedStatus;
121   }
122 
123   public String getId() {
124     return id;
125   }
126 
127   public void setId(String id) {
128     this.id = id;
129   }
130 
131   public String getReason() {
132     return reason;
133   }
134 
135   public void setReason(String reason) {
136     this.reason = reason;
137   }
138 
139   public String getText() {
140     return text;
141   }
142 
143   public void setText(String text) {
144     this.text = text;
145   }
146 
147   public Boolean isResolvedStatus() {
148     return resolvedStatus;
149   }
150 
151   public void setResolvedStatus(Boolean resolvedStatus) {
152     this.resolvedStatus = resolvedStatus;
153   }
154 
155   /**
156    * Reads the recording comment from the input stream.
157    *
158    * @param xml
159    *          the input stream
160    * @param unmarshaller the unmarshaller to use
161    * @return the deserialized recording comment
162    * @throws IOException
163    */
164   public static Comment valueOf(InputStream xml, Unmarshaller unmarshaller) throws IOException {
165     try {
166       if (context == null) {
167         createJAXBContext();
168       }
169       return unmarshaller.unmarshal(XmlSafeParser.parse(xml), Comment.class).getValue();
170     } catch (JAXBException e) {
171       throw new IOException(e.getLinkedException() != null ? e.getLinkedException() : e);
172     } catch (SAXException e) {
173       throw new IOException(e);
174     } finally {
175       IoSupport.closeQuietly(xml);
176     }
177   }
178 
179   /**
180    * Reads the recording comment from the input stream.
181    *
182    * @param json
183    *          the input stream
184    * @return the deserialized recording comment
185    * @throws JSONException
186    * @throws XMLStreamException
187    * @throws JAXBException
188    */
189   public static Comment valueOfJson(InputStream json)
190           throws IOException, JSONException, XMLStreamException, JAXBException {
191     // TODO Get this to work, it is currently returning null properties for all properties.
192     if (context == null) {
193       createJAXBContext();
194     }
195 
196     BufferedReader streamReader = new BufferedReader(new InputStreamReader(json, "UTF-8"));
197     StringBuilder jsonStringBuilder = new StringBuilder();
198     String inputStr;
199     while ((inputStr = streamReader.readLine()) != null) {
200       jsonStringBuilder.append(inputStr);
201     }
202 
203     JSONObject obj = new JSONObject(jsonStringBuilder.toString());
204     Configuration config = new Configuration();
205     config.setSupressAtAttributes(true);
206     Map<String, String> xmlToJsonNamespaces = new HashMap<String, String>(1);
207     xmlToJsonNamespaces.put(IndexObject.INDEX_XML_NAMESPACE, "");
208     config.setXmlToJsonNamespaces(xmlToJsonNamespaces);
209     MappedNamespaceConvention con = new MappedNamespaceConvention(config);
210     Unmarshaller unmarshaller = context.createUnmarshaller();
211     // CHECKSTYLE:OFF
212     // the xml is parsed from json and should be safe
213     XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
214     Comment comment = (Comment) unmarshaller.unmarshal(xmlStreamReader);
215     // CHECKSTYLE:ON
216     return comment;
217   }
218 
219   /**
220    * Initialize the JAXBContext.
221    */
222   private static void createJAXBContext() throws JAXBException {
223     context = JAXBContext.newInstance(Comment.class);
224   }
225 
226   /**
227    * Serializes the recording comment.
228    *
229    * @return the serialized recording comment
230    */
231   public String toJSON() {
232     try {
233       if (context == null) {
234         createJAXBContext();
235       }
236       Marshaller marshaller = Comment.context.createMarshaller();
237 
238       Configuration config = new Configuration();
239       config.setSupressAtAttributes(true);
240       MappedNamespaceConvention con = new MappedNamespaceConvention(config);
241       StringWriter writer = new StringWriter();
242       XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {
243         @Override
244         public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
245           super.writeStartElement("", local, "");
246         }
247 
248         @Override
249         public void writeStartElement(String uri, String local) throws XMLStreamException {
250           super.writeStartElement("", local, "");
251         }
252 
253         @Override
254         public void setPrefix(String pfx, String uri) throws XMLStreamException {
255         }
256 
257         @Override
258         public void setDefaultNamespace(String uri) throws XMLStreamException {
259         }
260       };
261 
262       marshaller.marshal(this, xmlStreamWriter);
263       return writer.toString();
264     } catch (JAXBException e) {
265       throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
266     }
267   }
268 
269   /**
270    * Serializes the recording comment to an XML format.
271    *
272    * @return A String with this comment's content as XML.
273    */
274   public String toXML() {
275     try {
276       if (context == null) {
277         createJAXBContext();
278       }
279       StringWriter writer = new StringWriter();
280       Marshaller marshaller = Comment.context.createMarshaller();
281       marshaller.marshal(this, writer);
282       return writer.toString();
283     } catch (JAXBException e) {
284       throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
285     }
286   }
287 
288   /**
289    * Create an unmarshaller for comments
290    * @return an unmarshaller for comments
291    * @throws IOException
292    */
293   public static Unmarshaller createUnmarshaller() throws IOException {
294     try {
295       if (context == null) {
296         createJAXBContext();
297       }
298       return context.createUnmarshaller();
299     } catch (JAXBException e) {
300       throw new IOException(e.getLinkedException() != null ? e.getLinkedException() : e);
301     }
302   }
303 
304 }