1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.ihe.xds.core.converters;
17
18 import groovy.lang.Closure;
19 import groovy.transform.stc.ClosureParams;
20 import groovy.transform.stc.SimpleType;
21 import org.apache.camel.Exchange;
22 import org.apache.camel.Expression;
23 import org.apache.camel.TypeConverter;
24 import org.openehealth.ipf.commons.ihe.ws.cxf.NonReadingAttachmentMarshaller;
25 import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.*;
26 import org.openehealth.ipf.commons.ihe.xds.core.requests.*;
27 import org.openehealth.ipf.commons.ihe.xds.core.responses.QueryResponse;
28 import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
29 import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocumentSet;
30 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.RemoveObjectsRequest;
31 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
32 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
33 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
34 import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
35
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.Marshaller;
39 import java.io.StringWriter;
40 import java.util.HashMap;
41 import java.util.Map;
42
43
44
45
46
47
48
49 public abstract class XdsRenderingUtils {
50
51
52
53
54
55 private static final Map<Class<?>, Class<?>> TYPES_CORRESPONDENCE;
56 private static final JAXBContext JAXB_CONTEXT;
57
58 static {
59 TYPES_CORRESPONDENCE = new HashMap<>();
60
61
62
63
64 TYPES_CORRESPONDENCE.put(QueryRegistry.class, AdhocQueryRequest.class);
65
66
67 TYPES_CORRESPONDENCE.put(ProvideAndRegisterDocumentSet.class, ProvideAndRegisterDocumentSetRequestType.class);
68
69
70 TYPES_CORRESPONDENCE.put(RegisterDocumentSet.class, SubmitObjectsRequest.class);
71
72
73 TYPES_CORRESPONDENCE.put(RetrieveDocumentSet.class, RetrieveDocumentSetRequestType.class);
74
75
76 TYPES_CORRESPONDENCE.put(RemoveMetadata.class, RemoveObjectsRequest.class);
77
78
79 TYPES_CORRESPONDENCE.put(RemoveDocuments.class, RemoveDocumentsRequestType.class);
80
81
82 TYPES_CORRESPONDENCE.put(RetrieveImagingDocumentSet.class, RetrieveImagingDocumentSetRequestType.class);
83
84
85
86
87 TYPES_CORRESPONDENCE.put(QueryResponse.class, AdhocQueryResponse.class);
88
89
90 TYPES_CORRESPONDENCE.put(Response.class, RegistryResponseType.class);
91
92
93 TYPES_CORRESPONDENCE.put(RetrievedDocumentSet.class, RetrieveDocumentSetResponseType.class);
94
95
96 try {
97 JAXB_CONTEXT = JAXBContext.newInstance(
98 AdhocQueryRequest.class,
99 ProvideAndRegisterDocumentSetRequestType.class,
100 SubmitObjectsRequest.class,
101 RetrieveDocumentSetRequestType.class,
102 RemoveObjectsRequest.class,
103 RemoveDocumentsRequestType.class,
104 RetrieveImagingDocumentSetRequestType.class,
105 AdhocQueryResponse.class,
106 RegistryResponseType.class,
107 RetrieveDocumentSetResponseType.class
108 );
109 } catch (JAXBException e) {
110 throw new RuntimeException(e);
111 }
112 }
113
114
115
116
117
118 private XdsRenderingUtils() {
119 throw new IllegalStateException("Cannot instantiate helper class");
120 }
121
122
123
124
125
126
127
128
129
130
131
132 public static String render(Exchange exchange) {
133 return doRender(exchange, exchange.getIn().getBody());
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public static String render(Exchange exchange, @ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Exchange"})
149 Closure closure) {
150 return doRender(exchange, closure.call(exchange));
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public static String render(Exchange exchange, Expression expression) {
166 return doRender(exchange, expression.evaluate(exchange, Object.class));
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180 public static String doRender(Exchange exchange, Object body) {
181 if (TYPES_CORRESPONDENCE.containsKey(body.getClass())) {
182 TypeConverter converter = exchange.getContext().getTypeConverter();
183 body = converter.convertTo(TYPES_CORRESPONDENCE.get(body.getClass()), exchange, body);
184 }
185 return renderEbxml(body);
186 }
187
188
189
190
191
192
193
194
195
196 public static String renderEbxml(Object ebXml) {
197 try {
198 StringWriter writer = new StringWriter();
199 Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
200 marshaller.setAttachmentMarshaller(new NonReadingAttachmentMarshaller());
201 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
202 marshaller.marshal(ebXml, writer);
203 return writer.toString();
204 } catch (JAXBException e) {
205 throw new RuntimeException(e);
206 }
207 }
208
209 }