package gov.hhs.fha.nhinc.hiemunsubscribe; import gov.hhs.fha.nhinc.common.nhinccommon.AssertionType; import gov.hhs.fha.nhinc.common.nhinccommoninternalorch.UnsubscribeRequestType; import gov.hhs.fha.nhinc.common.subscription.ReferenceParameterType; import gov.hhs.fha.nhinc.common.subscription.ReferenceParametersType; import gov.hhs.fha.nhinc.common.subscription.SubscriptionReferenceType; import gov.hhs.fha.nhinc.common.subscription.UnsubscribeType; import gov.hhs.fha.nhinc.properties.PropertyAccessor; //import gov.hhs.fha.nhinc.common.subscription.; import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractor; import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractorHelper; import java.util.List; import java.util.Map; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.oasis_open.docs.wsn.b_2.Unsubscribe; import org.oasis_open.docs.wsn.b_2.UnsubscribeResponse; import gov.hhs.fha.nhinc.connectmgr.ConnectionManagerCache; //import org.xmlsoap.schemas.ws._2004._08.addressing.ReferenceParametersType; import gov.hhs.fha.nhinc.connectmgr.data.CMBindingTemplate; import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessEntity; import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessService; import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessServices; import gov.hhs.fha.nhinc.nhinclib.NullChecker; /** * * @author jhoppesc */ public class HiemUnsubscribeImpl { private static Log log = LogFactory.getLog(HiemUnsubscribeImpl.class); public UnsubscribeResponse unsubscribe(Unsubscribe unsubscribeRequest, WebServiceContext context) { log.debug("Entering HiemUnsubscribeImpl.unsubscribe"); // Response object handle UnsubscribeResponse resp = null; // Build the unsubscribe request for the internal unsubscribe process UnsubscribeRequestType request = new UnsubscribeRequestType(); UnsubscribeType unsubscribeType = new UnsubscribeType(); request.setUnsubscribe(unsubscribeType); AssertionType assertion = SamlTokenExtractor.GetAssertion(context); request.setAssertion(assertion); // Collect the home community String homeCommunityId = SamlTokenExtractorHelper.getHomeCommunityId(); // Collect info and create the subscription reference SubscriptionReferenceType subscriptionReference = createSubscriptionReference(homeCommunityId, context); unsubscribeType.setSubscriptionReference(subscriptionReference); // Call the internal unsubscribe process resp = callInternalUnsubscribe(homeCommunityId, request); log.debug("Exiting HiemUnsubscribeImpl.unsubscribe"); return resp; } private String collectSubscriptionId(WebServiceContext context) { String subscriptionId = null; try { if(context != null) { log.debug("***###@@@ Web service context object type: " + context.getClass().getName()); MessageContext msgContext = context.getMessageContext(); if(msgContext != null) { @SuppressWarnings("unchecked") Map> msgCtxMap = (Map>) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS); if(msgCtxMap != null) { for(String key : msgCtxMap.keySet()) { if("Subscriptionid".equalsIgnoreCase(key)) { List values = msgCtxMap.get(key); if((values != null) && (!values.isEmpty())) { subscriptionId = values.get(0); log.debug("Collected subscriptionid: " + subscriptionId); break; } } } } } else { log.debug("Message context was null."); } } else { log.debug("@@@ Web service context was null."); } } catch(Throwable t) { t.printStackTrace(); log.debug("Error printing headers: " + t.getMessage()); } return subscriptionId; } private SubscriptionReferenceType createSubscriptionReference(String homeCommunityId, WebServiceContext context) { SubscriptionReferenceType subscriptionReference = new SubscriptionReferenceType(); String subscriptionId = collectSubscriptionId(context); if(NullChecker.isNotNullish(subscriptionId)) { ReferenceParameterType refParam = new ReferenceParameterType(); refParam.setElementName("SubscriptionId"); refParam.setNamespace("http://www.hhs.gov/healthit/nhin"); refParam.setPrefix("nhin"); refParam.setValue(subscriptionId); ReferenceParametersType refParamsType = new ReferenceParametersType(); subscriptionReference.setReferenceParameters(refParamsType); refParamsType.getReferenceParameter().add(refParam); } subscriptionReference.setSubscriptionManagerEndpointAddress(getEndpointUrl(homeCommunityId, "subscriptionmanager")); return subscriptionReference; } private String getEndpointUrl(String homeCommunityId, String serviceName) { String endpointUrl = null; try { CMBusinessEntity businessEntity = ConnectionManagerCache.getBusinessEntityByServiceName(homeCommunityId, serviceName); if(businessEntity != null) { CMBusinessServices businessServices = businessEntity.getBusinessServices(); if((businessServices != null) && (businessServices.getBusinessService() != null)) { for(CMBusinessService businessService : businessServices.getBusinessService()) { if((businessService.getBindingTemplates() != null) && (businessService.getBindingTemplates().getBindingTemplate() != null)) { for(CMBindingTemplate bindingTemplate : businessService.getBindingTemplates().getBindingTemplate()) { endpointUrl = bindingTemplate.getEndpointURL(); break; } break; } else { log.debug("No binding templates found for community '" + homeCommunityId + "' and service name '" + serviceName + "'."); } } } else { log.debug("No business services found for community '" + homeCommunityId + "' and service name '" + serviceName + "'."); } } } catch(Throwable t) { log.error("Error collecting endpoint url for community '" + homeCommunityId + "' and service name '" + serviceName + "': " + t.getMessage(), t); } return endpointUrl; } private org.oasis_open.docs.wsn.b_2.UnsubscribeResponse callInternalUnsubscribe(String homeCommunityId, UnsubscribeRequestType request) { org.oasis_open.docs.wsn.b_2.UnsubscribeResponse result = null; try { // Call Web Service Operation gov.hhs.fha.nhinc.nhincsubscription.NhincSubscriptionManagerService service = new gov.hhs.fha.nhinc.nhincsubscription.NhincSubscriptionManagerService(); gov.hhs.fha.nhinc.nhincsubscription.SubscriptionManager port = service.getSubscriptionManagerPort(); ((javax.xml.ws.BindingProvider) port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, SamlTokenExtractorHelper.getEndpointURL(homeCommunityId, SamlTokenExtractorHelper.INTERNAL_HIEM_UNSUBSCRIBE)); result = port.unsubscribe(request); //result = new org.oasis_open.docs.wsn.b_2.UnsubscribeResponse(); } catch (Exception ex) { log.error("Error calling internal unsubscribe: " + ex.getMessage(), ex); result = new org.oasis_open.docs.wsn.b_2.UnsubscribeResponse(); } return result; } }