1 | package gov.hhs.fha.nhinc.hiemunsubscribe;
|
---|
2 |
|
---|
3 |
|
---|
4 | import gov.hhs.fha.nhinc.common.nhinccommon.AssertionType;
|
---|
5 | import gov.hhs.fha.nhinc.common.nhinccommoninternalorch.UnsubscribeRequestType;
|
---|
6 | import gov.hhs.fha.nhinc.common.subscription.ReferenceParameterType;
|
---|
7 | import gov.hhs.fha.nhinc.common.subscription.ReferenceParametersType;
|
---|
8 | import gov.hhs.fha.nhinc.common.subscription.SubscriptionReferenceType;
|
---|
9 | import gov.hhs.fha.nhinc.common.subscription.UnsubscribeType;
|
---|
10 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
---|
11 | //import gov.hhs.fha.nhinc.common.subscription.;
|
---|
12 | import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractor;
|
---|
13 | import gov.hhs.fha.nhinc.saml.extraction.SamlTokenExtractorHelper;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 | import javax.xml.ws.WebServiceContext;
|
---|
17 | import javax.xml.ws.handler.MessageContext;
|
---|
18 | import org.apache.commons.logging.Log;
|
---|
19 | import org.apache.commons.logging.LogFactory;
|
---|
20 | import org.oasis_open.docs.wsn.b_2.Unsubscribe;
|
---|
21 | import org.oasis_open.docs.wsn.b_2.UnsubscribeResponse;
|
---|
22 | import gov.hhs.fha.nhinc.connectmgr.ConnectionManagerCache;
|
---|
23 | //import org.xmlsoap.schemas.ws._2004._08.addressing.ReferenceParametersType;
|
---|
24 | import gov.hhs.fha.nhinc.connectmgr.data.CMBindingTemplate;
|
---|
25 | import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessEntity;
|
---|
26 | import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessService;
|
---|
27 | import gov.hhs.fha.nhinc.connectmgr.data.CMBusinessServices;
|
---|
28 | import gov.hhs.fha.nhinc.nhinclib.NullChecker;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | *
|
---|
32 | * @author jhoppesc
|
---|
33 | */
|
---|
34 | public class HiemUnsubscribeImpl
|
---|
35 | {
|
---|
36 |
|
---|
37 | private static Log log = LogFactory.getLog(HiemUnsubscribeImpl.class);
|
---|
38 |
|
---|
39 | public UnsubscribeResponse unsubscribe(Unsubscribe unsubscribeRequest, WebServiceContext context) {
|
---|
40 | log.debug("Entering HiemUnsubscribeImpl.unsubscribe");
|
---|
41 |
|
---|
42 | // Response object handle
|
---|
43 | UnsubscribeResponse resp = null;
|
---|
44 |
|
---|
45 | // Build the unsubscribe request for the internal unsubscribe process
|
---|
46 | UnsubscribeRequestType request = new UnsubscribeRequestType();
|
---|
47 | UnsubscribeType unsubscribeType = new UnsubscribeType();
|
---|
48 |
|
---|
49 | request.setUnsubscribe(unsubscribeType);
|
---|
50 | AssertionType assertion = SamlTokenExtractor.GetAssertion(context);
|
---|
51 | request.setAssertion(assertion);
|
---|
52 |
|
---|
53 | // Collect the home community
|
---|
54 | String homeCommunityId = SamlTokenExtractorHelper.getHomeCommunityId();
|
---|
55 |
|
---|
56 | // Collect info and create the subscription reference
|
---|
57 | SubscriptionReferenceType subscriptionReference = createSubscriptionReference(homeCommunityId, context);
|
---|
58 | unsubscribeType.setSubscriptionReference(subscriptionReference);
|
---|
59 |
|
---|
60 | // Call the internal unsubscribe process
|
---|
61 | resp = callInternalUnsubscribe(homeCommunityId, request);
|
---|
62 |
|
---|
63 | log.debug("Exiting HiemUnsubscribeImpl.unsubscribe");
|
---|
64 |
|
---|
65 | return resp;
|
---|
66 | }
|
---|
67 |
|
---|
68 | private String collectSubscriptionId(WebServiceContext context)
|
---|
69 | {
|
---|
70 | String subscriptionId = null;
|
---|
71 | try
|
---|
72 | {
|
---|
73 | if(context != null)
|
---|
74 | {
|
---|
75 | log.debug("***###@@@ Web service context object type: " + context.getClass().getName());
|
---|
76 | MessageContext msgContext = context.getMessageContext();
|
---|
77 | if(msgContext != null)
|
---|
78 | {
|
---|
79 | @SuppressWarnings("unchecked")
|
---|
80 | Map<String, List<String>> msgCtxMap = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS);
|
---|
81 | if(msgCtxMap != null)
|
---|
82 | {
|
---|
83 | for(String key : msgCtxMap.keySet())
|
---|
84 | {
|
---|
85 | if("Subscriptionid".equalsIgnoreCase(key))
|
---|
86 | {
|
---|
87 | List<String> values = msgCtxMap.get(key);
|
---|
88 | if((values != null) && (!values.isEmpty()))
|
---|
89 | {
|
---|
90 | subscriptionId = values.get(0);
|
---|
91 | log.debug("Collected subscriptionid: " + subscriptionId);
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | log.debug("Message context was null.");
|
---|
101 | }
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | log.debug("@@@ Web service context was null.");
|
---|
106 | }
|
---|
107 | }
|
---|
108 | catch(Throwable t)
|
---|
109 | {
|
---|
110 | t.printStackTrace();
|
---|
111 | log.debug("Error printing headers: " + t.getMessage());
|
---|
112 | }
|
---|
113 | return subscriptionId;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private SubscriptionReferenceType createSubscriptionReference(String homeCommunityId, WebServiceContext context)
|
---|
117 | {
|
---|
118 | SubscriptionReferenceType subscriptionReference = new SubscriptionReferenceType();
|
---|
119 |
|
---|
120 | String subscriptionId = collectSubscriptionId(context);
|
---|
121 | if(NullChecker.isNotNullish(subscriptionId))
|
---|
122 | {
|
---|
123 | ReferenceParameterType refParam = new ReferenceParameterType();
|
---|
124 | refParam.setElementName("SubscriptionId");
|
---|
125 | refParam.setNamespace("http://www.hhs.gov/healthit/nhin");
|
---|
126 | refParam.setPrefix("nhin");
|
---|
127 | refParam.setValue(subscriptionId);
|
---|
128 |
|
---|
129 | ReferenceParametersType refParamsType = new ReferenceParametersType();
|
---|
130 | subscriptionReference.setReferenceParameters(refParamsType);
|
---|
131 | refParamsType.getReferenceParameter().add(refParam);
|
---|
132 | }
|
---|
133 |
|
---|
134 | subscriptionReference.setSubscriptionManagerEndpointAddress(getEndpointUrl(homeCommunityId, "subscriptionmanager"));
|
---|
135 |
|
---|
136 | return subscriptionReference;
|
---|
137 | }
|
---|
138 |
|
---|
139 | private String getEndpointUrl(String homeCommunityId, String serviceName)
|
---|
140 | {
|
---|
141 | String endpointUrl = null;
|
---|
142 | try
|
---|
143 | {
|
---|
144 | CMBusinessEntity businessEntity = ConnectionManagerCache.getBusinessEntityByServiceName(homeCommunityId, serviceName);
|
---|
145 | if(businessEntity != null)
|
---|
146 | {
|
---|
147 | CMBusinessServices businessServices = businessEntity.getBusinessServices();
|
---|
148 | if((businessServices != null) && (businessServices.getBusinessService() != null))
|
---|
149 | {
|
---|
150 | for(CMBusinessService businessService : businessServices.getBusinessService())
|
---|
151 | {
|
---|
152 | if((businessService.getBindingTemplates() != null) && (businessService.getBindingTemplates().getBindingTemplate() != null))
|
---|
153 | {
|
---|
154 | for(CMBindingTemplate bindingTemplate : businessService.getBindingTemplates().getBindingTemplate())
|
---|
155 | {
|
---|
156 | endpointUrl = bindingTemplate.getEndpointURL();
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | log.debug("No binding templates found for community '" + homeCommunityId + "' and service name '" + serviceName + "'.");
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | log.debug("No business services found for community '" + homeCommunityId + "' and service name '" + serviceName + "'.");
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | catch(Throwable t)
|
---|
174 | {
|
---|
175 | log.error("Error collecting endpoint url for community '" + homeCommunityId + "' and service name '" + serviceName + "': " + t.getMessage(), t);
|
---|
176 | }
|
---|
177 | return endpointUrl;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private org.oasis_open.docs.wsn.b_2.UnsubscribeResponse callInternalUnsubscribe(String homeCommunityId, UnsubscribeRequestType request)
|
---|
181 | {
|
---|
182 | org.oasis_open.docs.wsn.b_2.UnsubscribeResponse result = null;
|
---|
183 | try
|
---|
184 | { // Call Web Service Operation
|
---|
185 | gov.hhs.fha.nhinc.nhincsubscription.NhincSubscriptionManagerService service = new gov.hhs.fha.nhinc.nhincsubscription.NhincSubscriptionManagerService();
|
---|
186 | gov.hhs.fha.nhinc.nhincsubscription.SubscriptionManager port = service.getSubscriptionManagerPort();
|
---|
187 | ((javax.xml.ws.BindingProvider) port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, SamlTokenExtractorHelper.getEndpointURL(homeCommunityId, SamlTokenExtractorHelper.INTERNAL_HIEM_UNSUBSCRIBE));
|
---|
188 |
|
---|
189 | result = port.unsubscribe(request);
|
---|
190 | //result = new org.oasis_open.docs.wsn.b_2.UnsubscribeResponse();
|
---|
191 | }
|
---|
192 | catch (Exception ex)
|
---|
193 | {
|
---|
194 | log.error("Error calling internal unsubscribe: " + ex.getMessage(), ex);
|
---|
195 | result = new org.oasis_open.docs.wsn.b_2.UnsubscribeResponse();
|
---|
196 | }
|
---|
197 | return result;
|
---|
198 | }
|
---|
199 | }
|
---|