1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
51
52 @XmlAccessorType(XmlAccessType.NONE)
53 @XmlRootElement(name = "smil-response", namespace = "http://smil.opencastproject.org")
54 public class SmilResponseImpl implements SmilResponse {
55
56
57
58
59 private Smil smil;
60
61
62
63 private SmilObject[] entities;
64
65
66
67
68 private SmilResponseImpl() {
69 smil = null;
70 entities = null;
71 }
72
73
74
75
76
77
78 public SmilResponseImpl(Smil smil) {
79 this(smil, new SmilObject[]{});
80 }
81
82
83
84
85
86
87
88 public SmilResponseImpl(Smil smil, SmilObject entity) {
89 this(smil, new SmilObject[]{entity});
90 }
91
92
93
94
95
96
97
98 public SmilResponseImpl(Smil smil, SmilObject[] entities) {
99 this.smil = smil;
100 this.entities = entities;
101 }
102
103
104
105
106 @XmlElement(type = SmilImpl.class, required = true)
107 @Override
108 public Smil getSmil() {
109 return smil;
110 }
111
112
113
114
115
116
117 private void setSmil(Smil smil) {
118 this.smil = smil;
119 }
120
121
122
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
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
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
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
167 marshaller.marshal(this, writer);
168 return writer.toString();
169 }
170
171
172
173
174
175
176
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
189
190
191
192
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
207
208
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
221
222
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
233
234
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 }