Mercurial > dive4elements > river
diff flys-client/src/main/java/de/intevation/flys/client/server/auth/was/Response.java @ 2956:d7f76f197d89
Refactor GGInA authentication
Move authentication related classes to de.intevation.fly.client.server.auth
package. Abstract the authentication classes to allow other authentications
beside WAS/GGInA.
flys-client/trunk@4936 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Wed, 11 Jul 2012 13:31:56 +0000 |
parents | |
children | 5ba0a6efdf3b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/auth/was/Response.java Wed Jul 11 13:31:56 2012 +0000 @@ -0,0 +1,105 @@ +package de.intevation.flys.client.server.auth.was; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.codec.binary.Base64InputStream; + +import org.apache.http.HttpEntity; + +import org.apache.log4j.Logger; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; + +import de.intevation.flys.client.server.auth.Authentication; + +public class Response implements Authentication { + + private static Logger logger = Logger.getLogger(Response.class); + + private Element root; + private Assertion assertion; + private String username; + private String password; + + + public Response(HttpEntity entity, String username, String password) throws ServiceException { + + if (entity == null) { + throw new ServiceException("Invalid response"); + } + + String contenttype = entity.getContentType().getValue(); + + try { + InputStream in = entity.getContent(); + + if (!contenttype.equals("application/vnd.ogc.se_xml")) { + // XXX: Assume base64 encoded content. + in = new Base64InputStream(in); + } + + SAXBuilder builder = new SAXBuilder(); + Document doc = builder.build(in); + Element root = doc.getRootElement(); + String rname = root.getName(); + + if (rname != null && rname.equals("ServiceExceptionReport")) { + throw new ServiceException(root.getChildText("ServiceException")); + } + + this.root = root; + this.username = username; + this.password = password; + + } + catch(JDOMException e) { + logger.error(e); + } + catch(IOException e) { + logger.error(e); + } + } + + public Element getRoot() { + return this.root; + } + + @Override + public boolean isSuccess() { + String status = getStatus(); + return status != null && status.equals("samlp:Success"); + } + + public String getStatus() { + Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO); + if (status == null) { + return null; + } + Element statuscode = status.getChild("StatusCode", + Namespaces.SAML_NS_PROTO); + if (statuscode == null) { + return null; + } + return statuscode.getAttributeValue("Value"); + } + + public Assertion getAssertion() { + if (this.assertion == null && this.root != null) { + Element assertion = this.root.getChild("Assertion", + Namespaces.SAML_NS_ASSERT); + if (assertion != null) { + this.assertion = new Assertion(assertion); + } + } + return this.assertion; + } + + public User getUser() { + return new User(this.username, this.password); + } +} +// vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: