diff options
author | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2013-12-20 12:35:28 +0100 |
---|---|---|
committer | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2013-12-20 12:35:28 +0100 |
commit | defceef8afef538555c13d33e344a89a828a3d97 (patch) | |
tree | 24b44f970f161d5b139dde501ca0f5d883f9fdea /src/main/java/at/gv/util/client/moaid | |
download | egovutils-defceef8afef538555c13d33e344a89a828a3d97.tar.gz egovutils-defceef8afef538555c13d33e344a89a828a3d97.tar.bz2 egovutils-defceef8afef538555c13d33e344a89a828a3d97.zip |
inital
Diffstat (limited to 'src/main/java/at/gv/util/client/moaid')
-rw-r--r-- | src/main/java/at/gv/util/client/moaid/MOAIDClient.java | 126 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/moaid/MOAIDClientException.java | 23 |
2 files changed, 149 insertions, 0 deletions
diff --git a/src/main/java/at/gv/util/client/moaid/MOAIDClient.java b/src/main/java/at/gv/util/client/moaid/MOAIDClient.java new file mode 100644 index 0000000..09f8a57 --- /dev/null +++ b/src/main/java/at/gv/util/client/moaid/MOAIDClient.java @@ -0,0 +1,126 @@ +package at.gv.util.client.moaid; + +import java.math.BigInteger; +import java.net.URL; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import javax.annotation.Resource; +import javax.net.ssl.SSLContext; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.namespace.QName; +import javax.xml.ws.BindingProvider; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.Handler; + +import org.apache.commons.lang.RandomStringUtils; +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.GetAuthenticationDataService; +import at.gv.util.wsdl.IdentificationPortType; +import at.gv.util.wsdl.MOAFault; +import at.gv.util.xsd.saml.protocol.RequestType; +import at.gv.util.xsd.saml.protocol.ResponseType; + +import com.sun.xml.ws.developer.JAXWSProperties; + +public class MOAIDClient { + + @Resource + WebServiceContext wsContext; + + private EgovUtilConfiguration config = null; + Logger log = LoggerFactory.getLogger(MOAIDClient.class); + + public MOAIDClient() {} + + public MOAIDClient(EgovUtilConfiguration config) { + this.config = config; + + + } + + @SuppressWarnings("unchecked") + public ResponseType sendGetAuthenticationDataRequest(String getAuthDataServiceURL, String artifact) throws MOAIDClientException { + + // verify that parameters are not null + if (getAuthDataServiceURL == null) { + throw new NullPointerException("Parameter 'getAuthDataServiceURL' must not be null."); + } + if (artifact == null) { + throw new NullPointerException("Parameter 'artifact' must not be null."); + } + + URL url = MOAIDClient.class.getResource("/wsdl/MOA-ID-1.x.wsdl"); + GetAuthenticationDataService service = new GetAuthenticationDataService(url, new QName("http://reference.e-government.gv.at/namespace/moa/20020822#", "GetAuthenticationDataService")); + IdentificationPortType port = service.getIdentificationPort(); + + log.trace("Authentication data service request URL: " + getAuthDataServiceURL); + try { + + // set endpoint address + BindingProvider bindingProvider = (BindingProvider) port; + Map<String, Object> requestContext = bindingProvider.getRequestContext(); + requestContext.put( + BindingProvider.ENDPOINT_ADDRESS_PROPERTY, getAuthDataServiceURL); + + // add trace log + log.trace("Adding JAX-WS request/response trace handler."); + List<Handler> handlerList = bindingProvider.getBinding().getHandlerChain(); + if (handlerList == null) { + handlerList = new ArrayList(); + } + LoggingHandler loggingHandler = new LoggingHandler(); + handlerList.add(loggingHandler); + bindingProvider.getBinding().setHandlerChain(handlerList); + + // initialize ssl + + + if (getAuthDataServiceURL.toLowerCase().startsWith("https")) { + if (this.config == null) { + throw new MOAIDClientException("SSL requires a valid configuration for MOA-ID client."); + } + log.trace("Using ssl for MOA-ID get authentication data request."); + SSLContext sslContext = config.getMOAIDsslConfiguration().getSSLContext(false); + if (sslContext == null) { + throw new MOAIDClientException("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.getMOAIDsslConfiguration().useLaxHostNameVerifier()) { + log.trace("LaxHostnameVerifier enabled. This setting is not recommended to use."); + requestContext.put(JAXWSProperties.HOSTNAME_VERIFIER, new LaxHostNameVerifier()); + } + } + + // make simple moa-id request + RequestType request = new RequestType(); + request.setMajorVersion(BigInteger.ONE); + request.setMinorVersion(BigInteger.ZERO); + request.setIssueInstant(MiscUtil.getXMLGregorianCalendar(new Date())); + request.setRequestID(RandomStringUtils.randomAlphanumeric(10)); + + request.getAssertionArtifact().add(artifact); + + return port.getAuthenticationData(request); + + } catch (EgovUtilException e) { + throw new MOAIDClientException(e); + } catch (MOAFault e) { + throw new MOAIDClientException(e); + } catch (DatatypeConfigurationException e) { + throw new MOAIDClientException(e); + } + } + +} diff --git a/src/main/java/at/gv/util/client/moaid/MOAIDClientException.java b/src/main/java/at/gv/util/client/moaid/MOAIDClientException.java new file mode 100644 index 0000000..e9289e5 --- /dev/null +++ b/src/main/java/at/gv/util/client/moaid/MOAIDClientException.java @@ -0,0 +1,23 @@ +package at.gv.util.client.moaid; + +public class MOAIDClientException extends Exception { + + private static final long serialVersionUID = 5173056345209288701L; + + public MOAIDClientException() { + super(); + } + + public MOAIDClientException(String message, Throwable cause) { + super(message, cause); + } + + public MOAIDClientException(String message) { + super(message); + } + + public MOAIDClientException(Throwable cause) { + super(cause); + } + +} |