View Javadoc
1   /*
2    * Copyright 2017 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;
17  
18  import org.apache.cxf.binding.soap.SoapHeader;
19  import org.apache.cxf.headers.Header;
20  import org.openehealth.ipf.commons.core.DomBuildersThreadLocal;
21  import org.openehealth.ipf.commons.ihe.ws.JaxWsClientFactory;
22  import org.openehealth.ipf.commons.ihe.ws.WsTransactionConfiguration;
23  import org.openehealth.ipf.commons.ihe.xds.core.audit.XdsSubmitAuditDataset;
24  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSlotList30;
25  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary;
26  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
27  import org.openehealth.ipf.platform.camel.ihe.ws.AbstractWsEndpoint;
28  import org.openehealth.ipf.platform.camel.ihe.ws.AbstractWsProducer;
29  import org.openehealth.ipf.platform.camel.ihe.ws.HeaderUtils;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  
33  import javax.xml.namespace.QName;
34  import javax.xml.ws.BindingProvider;
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.Map;
38  
39  abstract public class XdsSubmissionProducer<InType, OutType> extends AbstractWsProducer<XdsSubmitAuditDataset, WsTransactionConfiguration<XdsSubmitAuditDataset>, InType, OutType> {
40  
41      private static final DomBuildersThreadLocal DOM_BUILDERS = new DomBuildersThreadLocal();
42  
43      public static final String TARGET_HCID_NS = "urn:ihe:iti:xdr:2014";
44      public static final String TARGET_HCID_NS_PREFIX = "xdr";
45      public static final String TARGET_HCID_BLOCK_LOCAL_PART = "homeCommunityBlock";
46      public static final String TARGET_HCID_LOCAL_PART = "homeCommunityId";
47      public static final QName  TARGET_HCID_HEADER_NAME = new QName(TARGET_HCID_NS, TARGET_HCID_BLOCK_LOCAL_PART, TARGET_HCID_NS_PREFIX);
48  
49      public XdsSubmissionProducer(
50              AbstractWsEndpoint<XdsSubmitAuditDataset, WsTransactionConfiguration<XdsSubmitAuditDataset>> endpoint,
51              JaxWsClientFactory<XdsSubmitAuditDataset> clientFactory,
52              Class<InType> requestClass,
53              Class<OutType> responseClass) {
54          super(endpoint, clientFactory, requestClass, responseClass);
55      }
56  
57      /**
58       * According to the XDR option "Transmit Home Community Id": when the request POJO contains the target home
59       * community ID, create a special SOAP header and copy the target home community ID into this header.
60       */
61      protected static void injectTargetHomeCommunityId(Object client, SubmitObjectsRequest request) {
62          String targetHomeCommunityId = null;
63          try {
64              EbXMLSlotList30 slotList = new EbXMLSlotList30(request.getRequestSlotList().getSlot());
65              targetHomeCommunityId = slotList.getSingleSlotValue(Vocabulary.SLOT_NAME_HOME_COMMUNITY_ID);
66          } catch (NullPointerException e) {
67              // nop
68          }
69  
70          if (targetHomeCommunityId != null) {
71              Document document = DOM_BUILDERS.get().newDocument();
72  
73              Element homeCommunityIdElement = document.createElementNS(TARGET_HCID_NS, TARGET_HCID_LOCAL_PART);
74              homeCommunityIdElement.setTextContent(targetHomeCommunityId);
75  
76              Element blockElement = document.createElementNS(TARGET_HCID_NS, TARGET_HCID_BLOCK_LOCAL_PART);
77              //blockElement.setAttributeNS(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "role", "urn:ihe:iti:xd:id");
78              //blockElement.setAttributeNS(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "relay", "true");
79              blockElement.appendChild(homeCommunityIdElement);
80  
81              BindingProvider bindingProvider = (BindingProvider) client;
82              Map<String, Object> requestContext = bindingProvider.getRequestContext();
83              List<Header> soapHeaders = HeaderUtils.getHeaders(requestContext, Header.HEADER_LIST, true, true, ArrayList::new);
84              soapHeaders.add(new SoapHeader(TARGET_HCID_HEADER_NAME, blockElement));
85          }
86      }
87  
88  }