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.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
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;
import at.gv.egovernment.moa.id.auth.data.AuthenticationSession;
import at.gv.egovernment.moa.id.auth.exception.MOAIDException;
import at.gv.egovernment.moa.id.config.auth.OAAuthParameter;
import at.gv.egovernment.moa.logging.Logger;
import eu.stork.peps.auth.commons.IPersonalAttributeList;
import eu.stork.peps.auth.commons.PersonalAttribute;
import eu.stork.peps.auth.commons.PersonalAttributeList;
/**
* Fetches the attribute IsHealthcareProfessional from the BAGDAD SOAP service
*/
public class EHvdAttributeProviderPlugin implements AttributeProvider {
/** The destination. */
private Object destination;
/**
* Instantiates a new e hvd attribute provider plugin.
*
* @param url the service url
*/
public EHvdAttributeProviderPlugin(String url) {
destination = url;
}
/* (non-Javadoc)
* @see at.gv.egovernment.moa.id.protocols.stork2.AttributeProvider#acquire(eu.stork.peps.auth.commons.PersonalAttribute)
*/
public IPersonalAttributeList acquire(PersonalAttribute attributes, AuthenticationSession moasession)
throws UnsupportedAttributeException,
ExternalAttributeRequestRequiredException, MOAIDException {
// break when we cannot handle the requested attribute
if(!attributes.getFriendlyName().equals("isHCP"))
throw new UnsupportedAttributeException();
try {
// create SOAP connection
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");
/*
Construct SOAP Request Message:
string
see https://stork.ehealth.gv.at/GDAService.asmx?op=IsHealthcareProfessional
*/
// SOAP Envelope
SOAPEnvelope envelope = requestPart.getEnvelope();
// SOAP Body
SOAPBody requestBody = envelope.getBody();
SOAPElement requestBodyElem = requestBody.addChildElement("IsHealthcareProfessional");
SOAPElement requestBodyElem1 = requestBodyElem.addChildElement("bPK");
requestBodyElem1.addTextNode(moasession.getIdentityLink().getIdentificationValue());
requestMessage.saveChanges();
// perform SOAP call
SOAPMessage responseMessage = soapConnection.call(requestMessage, destination);
// parse SOAP response
/*
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();
// 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) {
Logger.warn("eHVD returned an unexpected count of values. Expected 6 got " + collection.size());
throw new IndexOutOfBoundsException("response attributes not like specified");
}
// - fetch request validity
if (collection.get("RequestOK").equals("false")) {
Logger.warn("eHVD reported an invalid request. The error message is: " + collection.get("Message"));
throw new Exception("eHVD reported an invalid request");
}
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) {
throw new MOAIDException("stork.13", new Object[] { e });
}
}
/* (non-Javadoc)
* @see at.gv.egovernment.moa.id.protocols.stork2.AttributeProvider#performRedirect(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, at.gv.egovernment.moa.id.config.auth.OAAuthParameter)
*/
public void performRedirect(String url, String citizenCountyCode,
HttpServletRequest req, HttpServletResponse resp,
OAAuthParameter oaParam) throws MOAIDException {
// there is no redirect required
}
/* (non-Javadoc)
* @see at.gv.egovernment.moa.id.protocols.stork2.AttributeProvider#parse(javax.servlet.http.HttpServletRequest)
*/
public IPersonalAttributeList parse(HttpServletRequest httpReq)
throws UnsupportedAttributeException, MOAIDException {
// there is no redirect required, so we throw an exception when someone asks us to parse a response
throw new UnsupportedAttributeException();
}
}