From defceef8afef538555c13d33e344a89a828a3d97 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Fri, 20 Dec 2013 12:35:28 +0100 Subject: inital --- src/main/java/at/gv/util/client/szr/SZRClient.java | 167 +++++++++++++++++++++ .../at/gv/util/client/szr/SZRClientException.java | 23 +++ .../java/at/gv/util/client/szr/SZRSOAPHandler.java | 117 +++++++++++++++ 3 files changed, 307 insertions(+) create mode 100644 src/main/java/at/gv/util/client/szr/SZRClient.java create mode 100644 src/main/java/at/gv/util/client/szr/SZRClientException.java create mode 100644 src/main/java/at/gv/util/client/szr/SZRSOAPHandler.java (limited to 'src/main/java/at/gv/util/client/szr') diff --git a/src/main/java/at/gv/util/client/szr/SZRClient.java b/src/main/java/at/gv/util/client/szr/SZRClient.java new file mode 100644 index 0000000..cb98210 --- /dev/null +++ b/src/main/java/at/gv/util/client/szr/SZRClient.java @@ -0,0 +1,167 @@ +package at.gv.util.client.szr; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.annotation.Resource; +import javax.net.ssl.SSLContext; +import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.Holder; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.Handler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.util.LaxHostNameVerifier; +import at.gv.util.LoggingHandler; +import at.gv.util.MiscUtil; +import at.gv.util.config.EgovUtilConfiguration; +import at.gv.util.ex.EgovUtilException; +import at.gv.util.wsdl.szr.SZR; +import at.gv.util.wsdl.szr.SZRException; +import at.gv.util.wsdl.szr.SZRService; +import at.gv.util.xsd.szr.FremdBPKRequestType; +import at.gv.util.xsd.szr.FremdBPKType; +import at.gv.util.xsd.szr.IdentityLinkType; +import at.gv.util.xsd.szr.PersonInfoType; +import at.gv.util.xsd.szr.pvp.PvpTokenType; +import at.gv.util.xsd.szr.xmldsig.KeyValueType; + +import com.sun.xml.ws.developer.JAXWSProperties; + +public class SZRClient { + + @Resource + WebServiceContext wsContext; + + private EgovUtilConfiguration config = null; + Logger log = LoggerFactory.getLogger(SZRClient.class); + + private SZR szr = null; + + public SZRClient(EgovUtilConfiguration config) throws EgovUtilException { + MiscUtil.assertNotNull(config, "config"); + this.config = config; + initialize(); + } + + public IdentityLinkType getIdentityLink(PersonInfoType personInfo, List keyValue, Boolean insertERnP) throws SZRException { + MiscUtil.assertNotNull(personInfo, "personInfo"); + MiscUtil.assertNotNull(keyValue, "keyValue"); + return this.szr.getIdentityLink(personInfo, keyValue, insertERnP); + } + + public String getStammzahl(PersonInfoType personInfo) throws SZRException { + MiscUtil.assertNotNull(personInfo, "personInfo"); + return this.szr.getStammzahl(personInfo); + } + + public String getBPK(PersonInfoType personInfo, String target, String vkz) throws SZRException, EgovUtilException { + MiscUtil.assertNotNull(personInfo, "personInfo"); + MiscUtil.assertNotNull(target, "target"); + String targetPrefix = "urn:publicid:gv.at:cdid+"; + String targetString = null; + if (target.length() == 2) { + targetString = targetPrefix + target; + } else if (!target.startsWith(targetPrefix)) { + throw new EgovUtilException("Target must start with " + targetPrefix); + } else { + targetString = target; + } + Holder returnBPK = new Holder(); + + this.szr.getBPK(personInfo, targetString, vkz, null, false, returnBPK, null, null); + + return returnBPK.value; + } + + public String transformBPK(PersonInfoType personInfo, String inputBpk, String inputTarget, String target, String vkz) throws SZRException, EgovUtilException { + MiscUtil.assertNotNull(personInfo, "personInfo"); + MiscUtil.assertNotNull(target, "target"); + String targetPrefix = "urn:publicid:gv.at:cdid+"; + String targetString = null; + if (target.length() == 2) { + targetString = targetPrefix + target; + } else if (!target.startsWith(targetPrefix)) { + throw new EgovUtilException("Target must start with " + targetPrefix); + } else { + targetString = target; + } + Holder returnBPK = new Holder(); + FremdBPKRequestType rt = new FremdBPKRequestType(); + rt.setBereichsKennung(target); + rt.setVKZ(vkz); + List list = new ArrayList(); + list.add(rt); + List response = this.szr.transformBPK(personInfo, inputBpk, inputTarget, "kt", list); + if (response == null) { + return null; + } + return response.get(0).getFremdBPK(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void initialize() throws EgovUtilException { + URL url = SZRClient.class.getResource("/wsdl/szr/SZR_v2.0.wsdl"); + SZRService szrService = null; + if (config.isSZRTestEnvironment()) { + log.trace("Initializing SZR test configuration."); + //szrService = new SZRService(url, new QName("urn:SZRServices", "SZRTestumgebung")); + szrService = new SZRService(url, new QName("urn:SZRServices", "SZRService")); + szr = szrService.getSZRTestumgebung(); + } else { + log.trace("Initializing SZR productive configuration."); + szrService = new SZRService(url, new QName("urn:SZRServices", "SZRService")); + szr = szrService.getSZRProduktionsumgebung(); + } + String szrURL = null; + if (config.isSZRTestEnvironment()) { + szrURL = config.getSZRTestEnvironmentURL(); + } else { + szrURL = config.getSZRProductionEnvironmentURL(); + } + + log.trace("SZR connection URL: " + szrURL); + BindingProvider bindingProvider = (BindingProvider) szr; + Map requestContext = bindingProvider.getRequestContext(); + requestContext.put( + BindingProvider.ENDPOINT_ADDRESS_PROPERTY, szrURL); + + + + log.trace("Adding JAX-WS request/response trace handler."); + List handlerList = bindingProvider.getBinding().getHandlerChain(); + if (handlerList == null) { + handlerList = new ArrayList(); + } + LoggingHandler loggingHandler = new LoggingHandler(); + handlerList.add(loggingHandler); + log.trace("Adding WS-Security Header handler."); + PvpTokenType pvpToken = config.getPVPToken(); + SZRSOAPHandler szrSOAPHandler = new SZRSOAPHandler(); + szrSOAPHandler.configure(pvpToken); + handlerList.add(szrSOAPHandler); + bindingProvider.getBinding().setHandlerChain(handlerList); + + // check for ssl + if (szrURL.toLowerCase().startsWith("https")) { + log.trace("Using ssl for SZR client request."); + SSLContext sslContext = this.config.getSZRsslConfiguration().getSSLContext(false); + if (sslContext == null) { + throw new EgovUtilException("SSL context from configuration is empty. Please configure an SSL context in the configuration first."); + } + requestContext.put(JAXWSProperties.SSL_SOCKET_FACTORY, sslContext.getSocketFactory()); + + // check for lax hostname + if (this.config.getSZRsslConfiguration().useLaxHostNameVerifier()) { + log.trace("LaxHostnameVerifier enabled. This setting is not recommended to use."); + requestContext.put(JAXWSProperties.HOSTNAME_VERIFIER, new LaxHostNameVerifier()); + } + } + } + +} diff --git a/src/main/java/at/gv/util/client/szr/SZRClientException.java b/src/main/java/at/gv/util/client/szr/SZRClientException.java new file mode 100644 index 0000000..3b864da --- /dev/null +++ b/src/main/java/at/gv/util/client/szr/SZRClientException.java @@ -0,0 +1,23 @@ +package at.gv.util.client.szr; + +public class SZRClientException extends Exception { + + private static final long serialVersionUID = 5173056345209288701L; + + public SZRClientException() { + super(); + } + + public SZRClientException(String message, Throwable cause) { + super(message, cause); + } + + public SZRClientException(String message) { + super(message); + } + + public SZRClientException(Throwable cause) { + super(cause); + } + +} diff --git a/src/main/java/at/gv/util/client/szr/SZRSOAPHandler.java b/src/main/java/at/gv/util/client/szr/SZRSOAPHandler.java new file mode 100644 index 0000000..05e5004 --- /dev/null +++ b/src/main/java/at/gv/util/client/szr/SZRSOAPHandler.java @@ -0,0 +1,117 @@ +package at.gv.util.client.szr; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Set; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.soap.SOAPElement; +import javax.xml.soap.SOAPEnvelope; +import javax.xml.soap.SOAPException; +import javax.xml.soap.SOAPFactory; +import javax.xml.soap.SOAPHeader; +import javax.xml.soap.SOAPMessage; +import javax.xml.ws.handler.MessageContext; +import javax.xml.ws.handler.soap.SOAPHandler; +import javax.xml.ws.handler.soap.SOAPMessageContext; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; + +import at.gv.util.DOMUtils; +import at.gv.util.MiscUtil; +import at.gv.util.xsd.szr.pvp.ObjectFactory; +import at.gv.util.xsd.szr.pvp.PvpTokenType; + +public class SZRSOAPHandler implements SOAPHandler { + + private static final String AUTH_NS = "http://schemas.xmlsoap.org/ws/2002/04/secext"; + private static final String AUTH_PREFIX="wss"; + + private PvpTokenType pvpTokenType = null; + + private Logger log = LoggerFactory.getLogger(SZRSOAPHandler.class); + + + public void close(MessageContext arg0) { + } + + + public boolean handleFault(SOAPMessageContext arg0) { + return true; + } + + public void configure(PvpTokenType pvpToken) { + MiscUtil.assertNotNull(pvpToken, "pvpToken"); + this.pvpTokenType = pvpToken; + } + + + public boolean handleMessage(SOAPMessageContext smc) { + log.trace("Initializing SZR SOAP message handler."); + + boolean isOutMessage = ((Boolean) smc.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue(); + log.trace("Outbound message: " + isOutMessage); + if (pvpTokenType == null) { + throw new NullPointerException("Please configure first the PVP token."); + } + + if (!isOutMessage) { + return true; + } + try { + SOAPMessage message = smc.getMessage(); + SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); + SOAPFactory soapFactory = SOAPFactory.newInstance(); + + // Creating WS-Security header element + SOAPElement wsSecHeaderElm = soapFactory.createElement( + "Security", + AUTH_PREFIX, + AUTH_NS); + + // serialize pvp token + JAXBContext ctx = JAXBContext.newInstance(PvpTokenType.class); + ObjectFactory of = new ObjectFactory(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ctx.createMarshaller().marshal(of.createPvpToken(pvpTokenType), bos); + Element pvpTokenElement = DOMUtils.parseXmlNonValidating(new ByteArrayInputStream(bos.toByteArray())); + SOAPElement pvpToken = soapFactory.createElement(pvpTokenElement); + + // adding elements + wsSecHeaderElm.addChildElement(pvpToken); + SOAPHeader header = envelope.addHeader(); + header.addChildElement(wsSecHeaderElm); + + return true; + } catch(SOAPException e) { + log.warn("Cannot add WS-Security header.", e); + return false; + } catch (JAXBException e) { + log.warn("Cannot add WS-Security header.", e); + return false; + } catch (ParserConfigurationException e) { + log.warn("Cannot add WS-Security header.", e); + return false; + } catch (SAXException e) { + log.warn("Cannot add WS-Security header.", e); + return false; + } catch (IOException e) { + log.warn("Cannot add WS-Security header.", e); + return false; + } + + + } + + public Set getHeaders() { + return null; + } + +} -- cgit v1.2.3