/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gov.hhs.fha.nhinc.subscription.dte; import gov.hhs.fha.nhinc.common.subscription.ReferenceParameterType; import gov.hhs.fha.nhinc.common.subscription.ReferenceParametersType; import gov.hhs.fha.nhinc.common.subscription.SubscriptionItemType; import gov.hhs.fha.nhinc.common.subscription.SubscriptionReferenceType; import gov.hhs.fha.nhinc.common.subscriptiondte.TransformNhinSubscribeResponseToSubscriptionReferenceResponseType; import gov.hhs.fha.nhinc.subscription.*; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.ws.wsaddressing.*; import org.oasis_open.docs.wsn.b_2.*; import org.w3c.dom.*; /** * * @author rayj */ public class SubscriptionReferenceHelper { private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SubscriptionReferenceHelper.class); public static TransformNhinSubscribeResponseToSubscriptionReferenceResponseType transformNhinSubscribeResponseToSubscriptionReference(SubscribeResponse subscribeResponse) { TransformNhinSubscribeResponseToSubscriptionReferenceResponseType response = new TransformNhinSubscribeResponseToSubscriptionReferenceResponseType(); W3CEndpointReference nhinSubscriptionReference = subscribeResponse.getSubscriptionReference(); SubscriptionReferenceType internalSubscriptionReference = transformNhinSubscriptionReferenceToInternalSubscriptionReference(nhinSubscriptionReference); response.setSubscriptionReference(internalSubscriptionReference); return response; } public static SubscriptionReferenceType transformNhinSubscriptionReferenceToInternalSubscriptionReference(W3CEndpointReference nhinSubscriptionReference) { SubscriptionReferenceType internalSubscriptionReference = new SubscriptionReferenceType(); Document document = XmlHelper.transformEndpointReferenceToDocument(nhinSubscriptionReference); internalSubscriptionReference.setSubscriptionManagerEndpointAddress(getAddress(document)); internalSubscriptionReference.setReferenceParameters(new ReferenceParametersType()); NodeList referenceParameterNodes = XmlHelper.getSingleNodeChildren(document, "ReferenceParameters"); if (referenceParameterNodes != null) { for (int i = 0; i < referenceParameterNodes.getLength(); i++) { Node nhinReferenceParamterNode = referenceParameterNodes.item(i); ReferenceParameterType internalReferenceParameter = new ReferenceParameterType(); internalReferenceParameter.setValue(XmlHelper.getNodeValue(nhinReferenceParamterNode)); internalReferenceParameter.setNamespace(nhinReferenceParamterNode.getNamespaceURI()); internalReferenceParameter.setElementName(nhinReferenceParamterNode.getLocalName()); internalSubscriptionReference.getReferenceParameters().getReferenceParameter().add(internalReferenceParameter); } } return internalSubscriptionReference; } public static W3CEndpointReference createNhinSubscriptionReference(SubscriptionItemType subscriptionItem) { return createNhinSubscriptionReference(subscriptionItem.getSubscriptionReference()); } public static W3CEndpointReference createNhinSubscriptionReference(SubscriptionReferenceType internalSubscriptionRef) { log.info("begin createNhinSubscriptionReference"); W3CEndpointReference subRef; W3CEndpointReferenceBuilder resultBuilder = new W3CEndpointReferenceBuilder(); log.info("internalSubscriptionRef.getSubscriptionManagerEndpointAddress()=" + internalSubscriptionRef.getSubscriptionManagerEndpointAddress()); resultBuilder.address(internalSubscriptionRef.getSubscriptionManagerEndpointAddress()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); Document doc = null; try { doc = docBuilderFactory.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException ex) { throw new RuntimeException(ex); } doc.setXmlStandalone(true); ReferenceParametersType referenceParamters = internalSubscriptionRef.getReferenceParameters(); if (referenceParamters != null) { for (ReferenceParameterType referenceParamter : referenceParamters.getReferenceParameter()) { log.info("handling reference parameter " + referenceParamter.getNamespace() + ";" + referenceParamter.getElementName() + ";" + referenceParamter.getValue()); Element subscriptionElem = doc.createElementNS(referenceParamter.getNamespace(), referenceParamter.getElementName()); subscriptionElem.setTextContent(referenceParamter.getValue()); resultBuilder.referenceParameter(subscriptionElem); } } else { log.info("referenceParamters==null"); } log.info("building.. resultBuilder.build()"); subRef = resultBuilder.build(); log.info("end createNhinSubscriptionReference"); return subRef; } public static String getAddress(W3CEndpointReference subscriptionReference) { Document document = XmlHelper.transformEndpointReferenceToDocument(subscriptionReference); return getAddress(document); } public static String getAddress(Document document) { return XmlHelper.getNodeValue(document, "Address"); } public static NodeList getReferenceParameters(W3CEndpointReference subscriptionReference) { Document document = XmlHelper.transformEndpointReferenceToDocument(subscriptionReference); return getReferenceParameters(document); } public static NodeList getReferenceParameters(Document document) { return XmlHelper.getSingleNodeChildren(document, "ReferenceParameters"); } public static String getReferenceParameterValue(W3CEndpointReference subscriptionReference, String parameterName) { Document document = XmlHelper.transformEndpointReferenceToDocument(subscriptionReference); return getReferenceParameterValue(document, parameterName); } public static String getReferenceParameterValue(Document document, String parameterName) { String parameterValue = null; NodeList referenceParameterNodes = getReferenceParameters(document); if (referenceParameterNodes != null) { for (int i = 0; i < referenceParameterNodes.getLength(); i++) { Node nhinReferenceParamterNode = referenceParameterNodes.item(i); String elementName = nhinReferenceParamterNode.getLocalName(); if ((elementName != null) && (elementName.contentEquals(parameterName))) { parameterValue = XmlHelper.getNodeValue(nhinReferenceParamterNode); } } } return parameterValue; } }