View Javadoc
1   /*
2    * Copyright 2014 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.tutorials.iheclient;
17  
18  import org.apache.camel.CamelContext;
19  import org.apache.camel.CamelContextAware;
20  import org.apache.camel.Exchange;
21  import org.apache.camel.ProducerTemplate;
22  import org.apache.camel.impl.DefaultExchange;
23  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
24  import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
25  import org.openehealth.ipf.commons.ihe.xds.core.requests.RetrieveDocumentSet;
26  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.StoredQuery;
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.xml.CombinedXmlValidator;
31  import org.openehealth.ipf.platform.camel.core.util.Exchanges;
32  
33  import static org.openehealth.ipf.commons.ihe.hl7v3.PDQV3.Interactions.ITI_47;
34  import static org.openehealth.ipf.commons.ihe.hl7v3.PIXV3.Interactions.ITI_44_PIX;
35  import static org.openehealth.ipf.commons.ihe.hl7v3.PIXV3.Interactions.ITI_45;
36  
37  /**
38   * This is a simple IHE client helper class that provides a couple of sender methods
39   */
40  public class IHEWebServiceClient implements CamelContextAware {
41  
42      private CamelContext camelContext;
43      private CombinedXmlValidator validator = new CombinedXmlValidator();
44  
45      @Override
46      public void setCamelContext(CamelContext camelContext) {
47          this.camelContext = camelContext;
48      }
49  
50      @Override
51      public CamelContext getCamelContext() {
52          return camelContext;
53      }
54  
55  
56      // ===================
57      // XDS-related queries
58      // ===================
59  
60      public QueryResponse iti18StoredQuery(StoredQuery query, String host, int port, String pathAndParameters) throws Exception {
61          QueryRegistry storedQuery = new QueryRegistry(query);
62          String endpoint = String.format("xds-iti18://%s:%d/%s", host, port, pathAndParameters);
63          return send(endpoint, storedQuery, QueryResponse.class);
64      }
65  
66      // Use this for RAD-68, too
67      public Response iti41ProvideAndRegister(ProvideAndRegisterDocumentSet documentSet, String host, int port, String pathAndParameters) throws Exception {
68          String endpoint = String.format("xds-iti41://%s:%d/%s", host, port, pathAndParameters);
69          return send(endpoint, documentSet, Response.class);
70      }
71  
72      public RetrievedDocumentSet iti43RetrieveDocumentSet(RetrieveDocumentSet retrieve, String host, int port, String pathAndParameters) throws Exception {
73          String endpoint = String.format("xds-iti43://%s:%d/%s", host, port, pathAndParameters);
74          return send(endpoint, retrieve, RetrievedDocumentSet.class);
75      }
76  
77  
78      // =======================
79      // PIX/PDQ-related queries
80      // incl validation option
81      // =======================
82  
83      public String iti44PatientFeed(String message, String host, int port, String pathAndParameters, boolean validate) throws Exception {
84          if (validate) {
85              validator.validate(message, ITI_44_PIX.getRequestValidationProfile());
86          }
87          String endpoint = String.format("pixv3-iti44://%s:%d/%s", host, port, pathAndParameters);
88          String response = send(endpoint, message, String.class);
89          if (validate) {
90              validator.validate(response, ITI_44_PIX.getResponseValidationProfile());
91          }
92          return response;
93      }
94  
95      public String iti45PatientQuery(String message, String host, int port, String pathAndParameters, boolean validate) throws Exception {
96          if (validate) {
97              validator.validate(message, ITI_45.getRequestValidationProfile());
98          }
99          String endpoint = String.format("pixv3-iti45://%s:%d/%s", host, port, pathAndParameters);
100         String response = send(endpoint, message, String.class);
101         if (validate) {
102             validator.validate(response, ITI_45.getResponseValidationProfile());
103         }
104         return response;
105     }
106 
107     public String iti47PatientDemographicsQuery(String message, String host, int port, String pathAndParameters, boolean validate) throws Exception {
108         if (validate) {
109             validator.validate(message, ITI_47.getRequestValidationProfile());
110         }
111         String endpoint = String.format("pdqv3-iti47://%s:%d/%s", host, port, pathAndParameters);
112         String response = send(endpoint, message, String.class);
113         if (validate) {
114             validator.validate(response, ITI_47.getResponseValidationProfile());
115         }
116         return response;
117     }
118 
119     // =======
120     // Helpers
121     // =======
122 
123     private <T> T send(String endpoint, Object input, Class<T> outType) throws Exception {
124         Exchange result = send(endpoint, input);
125         return Exchanges.resultMessage(result).getBody(outType);
126     }
127 
128     private Exchange send(String endpoint, Object body) throws Exception {
129         Exchange exchange = new DefaultExchange(getCamelContext());
130         exchange.getIn().setBody(body);
131         /*
132         if (headers != null && !headers.isEmpty()) {
133             exchange.getIn().getHeaders().putAll(headers);
134         }
135         */
136         ProducerTemplate template = camelContext.createProducerTemplate();
137         Exchange result = template.send(endpoint, exchange);
138         if (result.getException() != null) {
139             throw result.getException();
140         }
141         return result;
142     }
143 
144 }