View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Utility class for rendering of ebXML stub POJOs and simplified
45   * XDS model classes into XML.
46   *
47   * @author Dmytro Rud
48   */
49  public abstract class XdsRenderingUtils {
50  
51      /**
52       * Correspondence between relevant XDS data types from
53       * ebXML model and IPF simplified model.
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          /* --------- REQUESTS --------- */
62  
63          // ITI-18, 38, 51, 63
64          TYPES_CORRESPONDENCE.put(QueryRegistry.class, AdhocQueryRequest.class);
65  
66          // ITI-41
67          TYPES_CORRESPONDENCE.put(ProvideAndRegisterDocumentSet.class, ProvideAndRegisterDocumentSetRequestType.class);
68  
69          // ITI-42, 57, 61
70          TYPES_CORRESPONDENCE.put(RegisterDocumentSet.class, SubmitObjectsRequest.class);
71  
72          // ITI-39, 43
73          TYPES_CORRESPONDENCE.put(RetrieveDocumentSet.class, RetrieveDocumentSetRequestType.class);
74  
75          // ITI-62
76          TYPES_CORRESPONDENCE.put(RemoveMetadata.class, RemoveObjectsRequest.class);
77  
78          // ITI-86
79          TYPES_CORRESPONDENCE.put(RemoveDocuments.class, RemoveDocumentsRequestType.class);
80  
81          // RAD-69, 75
82          TYPES_CORRESPONDENCE.put(RetrieveImagingDocumentSet.class, RetrieveImagingDocumentSetRequestType.class);
83  
84          /* --------- RESPONSES --------- */
85  
86          // ITI-18, 38, 51, 63
87          TYPES_CORRESPONDENCE.put(QueryResponse.class, AdhocQueryResponse.class);
88  
89          // ITI-41, 42, 57, 61, 62, 86
90          TYPES_CORRESPONDENCE.put(Response.class, RegistryResponseType.class);
91  
92          // ITI-39, ITI-43, RAD-69, RAD-75
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      * Constructor.
117      */
118     private XdsRenderingUtils() {
119         throw new IllegalStateException("Cannot instantiate helper class");
120     }
121 
122 
123     /**
124      * Renders an XDS object (either ebXML POJO or an object from the simplified model)
125      * contained in the input message of given Camel exchange.
126      *
127      * @param exchange
128      *      Camel exchange containing the XDS object in <code>exchange.in.body</code>.
129      * @return
130      *      XML representation of the XDS object contained in the given Camel exchange.
131      */
132     public static String render(Exchange exchange) {
133         return doRender(exchange, exchange.getIn().getBody());
134     }
135 
136 
137     /**
138      * Renders an XDS object (either ebXML POJO or an object from the simplified model)
139      * contained in the given Camel exchange.
140      *
141      * @param exchange
142      *      Camel exchange containing the XDS object.
143      * @param closure
144      *      Groovy closure to extract the XDS object from the exchange.
145      * @return
146      *      XML representation of the XDS object contained in the given Camel exchange.
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      * Renders an XDS object (either ebXML POJO or an object from the simplified model)
156      * contained in the given Camel exchange.
157      *
158      * @param exchange
159      *      Camel exchange containing the XDS object.
160      * @param expression
161      *      Camel expression to extract the XDS object from the exchange.
162      * @return
163      *      XML representation of the XDS object contained in the given Camel exchange.
164      */
165     public static String render(Exchange exchange, Expression expression) {
166         return doRender(exchange, expression.evaluate(exchange, Object.class));
167     }
168 
169 
170     /**
171      * Renders an XDS object (either ebXML POJO or an object from the simplified model).
172      *
173      * @param exchange
174      *      Camel exchange.
175      * @param body
176      *      XDS object (either ebXML POJO or an object from the simplified model).
177      * @return
178      *      XML representation of the given XDS object.
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      * Returns marshaled XML representation of the given ebXML POJO.
191      * @param ebXml
192      *      ebXML POJO.
193      * @return
194      *      XML string representing the given POJO.
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 }