From 5dd5959e9ddd730452d0007fbf2c091d2c2506e1 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 26 Feb 2014 13:54:44 +0100 Subject: parse the SOAP response --- .../stork2/EHvdAttributeProviderPlugin.java | 143 +++++++++++++++++---- 1 file changed, 117 insertions(+), 26 deletions(-) diff --git a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/protocols/stork2/EHvdAttributeProviderPlugin.java b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/protocols/stork2/EHvdAttributeProviderPlugin.java index 42652464b..fdf0806b8 100644 --- a/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/protocols/stork2/EHvdAttributeProviderPlugin.java +++ b/id/server/idserverlib/src/main/java/at/gv/egovernment/moa/id/protocols/stork2/EHvdAttributeProviderPlugin.java @@ -1,10 +1,13 @@ package at.gv.egovernment.moa.id.protocols.stork2; +import java.io.StringWriter; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; @@ -14,6 +17,12 @@ import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -58,13 +67,13 @@ public class EHvdAttributeProviderPlugin implements AttributeProvider { SOAPConnection soapConnection = SOAPConnectionFactory.newInstance().createConnection(); // assemble SOAP request - MessageFactory messageFactory = MessageFactory.newInstance(); - SOAPMessage requestMessage = messageFactory.createMessage(); - SOAPPart requestPart = requestMessage.getSOAPPart(); - - // (soap 1.1 relevant part. could not find a solution to use soap 1.2 in time. - requestMessage.getMimeHeaders().setHeader("SOAPAction", "http://gesundheit.gv.at/BAGDAD/DataAccessService/IsHealthcareProfessional"); - + MessageFactory messageFactory = MessageFactory.newInstance(); + SOAPMessage requestMessage = messageFactory.createMessage(); + SOAPPart requestPart = requestMessage.getSOAPPart(); + + // (soap 1.1 relevant part. could not find a solution to use soap 1.2 in time. + requestMessage.getMimeHeaders().setHeader("SOAPAction", "http://gesundheit.gv.at/BAGDAD/DataAccessService/IsHealthcareProfessional"); + /* Construct SOAP Request Message: @@ -74,34 +83,116 @@ public class EHvdAttributeProviderPlugin implements AttributeProvider { - + see https://stork.ehealth.gv.at/GDAService.asmx?op=IsHealthcareProfessional */ - // SOAP Envelope - SOAPEnvelope envelope = requestPart.getEnvelope(); + // SOAP Envelope + SOAPEnvelope envelope = requestPart.getEnvelope(); + + // SOAP Body + SOAPBody requestBody = envelope.getBody(); + SOAPElement requestBodyElem = requestBody.addChildElement("IsHealthcareProfessional"); + SOAPElement requestBodyElem1 = requestBodyElem.addChildElement("bPK"); + // TODO fetch bpk_gh from somewhere + requestBodyElem1.addTextNode("bpk_gh"); - // SOAP Body - SOAPBody requestBody = envelope.getBody(); - SOAPElement requestBodyElem = requestBody.addChildElement("IsHealthcareProfessional"); - SOAPElement requestBodyElem1 = requestBodyElem.addChildElement("bPK"); - // TODO fetch bpk_gh from somewhere - requestBodyElem1.addTextNode("bpk_gh"); + requestMessage.saveChanges(); - requestMessage.saveChanges(); - // perform SOAP call - SOAPMessage responseMessage = soapConnection.call(requestMessage, destination); - + SOAPMessage responseMessage = soapConnection.call(requestMessage, destination); + // parse SOAP response - - // assemble attribute - PersonalAttribute acquiredAttribute = new PersonalAttribute(); - + + /* + + + + + boolean + string + boolean + string + string + string + + + + + + see https://stork.ehealth.gv.at/GDAService.asmx?op=IsHealthcareProfessional + */ + SOAPBody responseBody = responseMessage.getSOAPBody(); + + // iterate through tree + SOAPElement responseElement = (SOAPElement) responseBody.getChildElements().next(); + SOAPElement resultElement = (SOAPElement) responseElement.getChildElements().next(); + SOAPElement tmp; + + // collect all info in a map + Iterator it = resultElement.getChildElements(); + Map collection = new HashMap(); + while (it.hasNext()) { + SOAPElement current = (SOAPElement) it.next(); + + collection.put(current.getNodeName(), current.getTextContent()); + + } + + // check if there is anything valid in the map + if (collection.isEmpty() || collection.size() != 6) { + // TODO report error + } + + // - fetch request validity + if (collection.get("RequestOK").equals("false")) { + // TODO report error + } + + PersonalAttribute acquiredAttribute = null; + + if (collection.get("IsHealthcareProfessional").equals("false")) { + // the citizen is no HCP + acquiredAttribute = new PersonalAttribute("isHCP", false, new ArrayList(), "NotAvailable"); + } else { + // go on and parse the data + + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + + Element orgname = doc.createElement("nameOfOrganisation"); + orgname.appendChild(doc.createTextNode(collection.get("NameOfOrganization"))); + doc.appendChild(orgname); + + Element type = doc.createElement("HCP"); + // TODO fix value mapping + if (collection.get("Type").equals("Medical Doctors")) + type.appendChild(doc.createTextNode("D")); + doc.appendChild(type); + + Element specialization = doc.createElement("specialisation"); + // TODO fix value mapping + specialization.appendChild(doc.createTextNode(collection.get("Specialization").substring(0, 2))); + doc.appendChild(specialization); + + // get string from dom tree + Source source = new DOMSource(doc); + StringWriter out = new StringWriter(); + Result result = new StreamResult(out); + + TransformerFactory tFactory = TransformerFactory.newInstance(); + Transformer transformer = tFactory.newTransformer(); + transformer.transform(source, result); + + ArrayList value = new ArrayList(); + value.add(out.toString()); + + acquiredAttribute = new PersonalAttribute("isHCP", false, value, "Available"); + } + // pack and return the result PersonalAttributeList result = new PersonalAttributeList(); result.add(acquiredAttribute); - + return result; } catch (Exception e) { // TODO in case of an error, we might want to inform someone somehow different than by just saying nothing -- cgit v1.2.3