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.smil.impl;
23  
24  import org.opencastproject.smil.api.SmilException;
25  import org.opencastproject.smil.api.SmilResponse;
26  import org.opencastproject.smil.entity.SmilImpl;
27  import org.opencastproject.smil.entity.SmilObjectImpl;
28  import org.opencastproject.smil.entity.api.Smil;
29  import org.opencastproject.smil.entity.api.SmilObject;
30  import org.opencastproject.util.XmlSafeParser;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.xml.sax.SAXException;
34  
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.io.StringWriter;
38  
39  import javax.xml.bind.JAXBContext;
40  import javax.xml.bind.JAXBException;
41  import javax.xml.bind.Marshaller;
42  import javax.xml.bind.Unmarshaller;
43  import javax.xml.bind.annotation.XmlAccessType;
44  import javax.xml.bind.annotation.XmlAccessorType;
45  import javax.xml.bind.annotation.XmlElement;
46  import javax.xml.bind.annotation.XmlElementRef;
47  import javax.xml.bind.annotation.XmlRootElement;
48  
49  /**
50   * {@link SmilResponse} implementation.
51   */
52  @XmlAccessorType(XmlAccessType.NONE)
53  @XmlRootElement(name = "smil-response", namespace = "http://smil.opencastproject.org")
54  public class SmilResponseImpl implements SmilResponse {
55  
56    /**
57     * Smil
58     */
59    private Smil smil;
60    /**
61     * Entities
62     */
63    private SmilObject[] entities;
64  
65    /**
66     * Empty constructor (needed for JAXB).
67     */
68    private SmilResponseImpl() {
69      smil = null;
70      entities = null;
71    }
72  
73    /**
74     * Constructor
75     *
76     * @param smil to set
77     */
78    public SmilResponseImpl(Smil smil) {
79      this(smil, new SmilObject[]{});
80    }
81  
82    /**
83     * Constructor.
84     *
85     * @param smil to set
86     * @param entity to set
87     */
88    public SmilResponseImpl(Smil smil, SmilObject entity) {
89      this(smil, new SmilObject[]{entity});
90    }
91  
92    /**
93     * Constructor.
94     *
95     * @param smil to set
96     * @param entities to set
97     */
98    public SmilResponseImpl(Smil smil, SmilObject[] entities) {
99      this.smil = smil;
100     this.entities = entities;
101   }
102 
103   /**
104    * {@inheritDoc }
105    */
106   @XmlElement(type = SmilImpl.class, required = true)
107   @Override
108   public Smil getSmil() {
109     return smil;
110   }
111 
112   /**
113    * Set {@link Smil}.
114    *
115    * @param smil to set
116    */
117   private void setSmil(Smil smil) {
118     this.smil = smil;
119   }
120 
121   /**
122    * {@inheritDoc }
123    */
124   @Override
125   public int getEntitiesCount() {
126     if (entities == null) {
127       return 0;
128     } else {
129       return entities.length;
130     }
131   }
132 
133   /**
134    * {@inheritDoc }
135    */
136   @Override
137   public SmilObject getEntity() throws SmilException {
138     if (entities.length == 0) {
139       throw new SmilException("There is no entity.");
140     }
141     if (entities.length > 1) {
142       throw new SmilException("There is more than one entity.");
143     }
144     return entities[0];
145   }
146 
147   /**
148    * {@inheritDoc }
149    */
150   @Override
151   public SmilObject[] getEntities() throws SmilException {
152     if (entities.length == 0) {
153       throw new SmilException("There are no entities.");
154     }
155     return entities;
156   }
157 
158   /**
159    * {@inheritDoc }
160    */
161   @Override
162   public String toXml() throws JAXBException {
163     StringWriter writer = new StringWriter();
164     JAXBContext ctx = JAXBContext.newInstance(SmilResponseImpl.class);
165     Marshaller marshaller = ctx.createMarshaller();
166     // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
167     marshaller.marshal(this, writer);
168     return writer.toString();
169   }
170 
171   /**
172    * Deserialize {@link SmilResponse} from XML.
173    *
174    * @param smilResponseXml {@link SmilResponse} as XML
175    * @return {@link SmilResponse} object
176    * @throws JAXBException if deserialization fail
177    */
178   public static SmilResponse fromXml(String smilResponseXml) throws JAXBException {
179     InputStream smilStream = IOUtils.toInputStream(smilResponseXml);
180     try {
181       return fromXml(smilStream);
182     } finally {
183       IOUtils.closeQuietly(smilStream);
184     }
185   }
186 
187   /**
188    * Deserialize {@link SmilResponse} from XML.
189    *
190    * @param smilResponseXml {@link SmilResponse} as XML {@link InputStream}
191    * @return {@link SmilResponse} object
192    * @throws JAXBException if deserialization fail
193    */
194   protected static SmilResponse fromXml(InputStream smilResponseXml) throws JAXBException {
195     JAXBContext ctx = JAXBContext.newInstance(SmilResponseImpl.class);
196     Unmarshaller unmarshaller = ctx.createUnmarshaller();
197     try {
198       return (SmilResponse) unmarshaller.unmarshal(XmlSafeParser.parse(smilResponseXml));
199     } catch (IOException | SAXException e) {
200       throw new JAXBException(e);
201     }
202 
203   }
204 
205   /**
206    * JAXB helper method.
207    *
208    * @return
209    */
210   @XmlElementRef
211   private SmilResponseEntity<SmilObject>[] getResponseEntities() {
212     SmilResponseEntity[] entitiesWrapped = new SmilResponseEntity[entities.length];
213     for (int i = 0; i < entities.length; i++) {
214       entitiesWrapped[i] = new SmilResponseEntity(entities[i]);
215     }
216     return entitiesWrapped;
217   }
218 
219   /**
220    * JAXB helper method.
221    *
222    * @param entities
223    */
224   private void setResponseEntities(SmilResponseEntity<SmilObject>[] entities) {
225     this.entities = new SmilObject[entities.length];
226     for (int e = 0; e < entities.length; e++) {
227       this.entities[e] = entities[e].getEntity();
228     }
229   }
230 
231   /**
232    * {@link SmilObject} wrapper class for serialization.
233    *
234    * @param <SmilObject>
235    */
236   @XmlRootElement(name = "entity", namespace = "http://smil.opencastproject.org")
237   private static class SmilResponseEntity<SmilObject> {
238 
239     private SmilObject entity;
240 
241     SmilResponseEntity() {
242     }
243 
244     SmilResponseEntity(SmilObject entity) {
245       this.entity = entity;
246     }
247 
248     @XmlElementRef(type = SmilObjectImpl.class)
249     public SmilObject getEntity() {
250       return entity;
251     }
252 
253     private void setEntity(SmilObject entity) {
254       this.entity = entity;
255     }
256   }
257 }