diff flys-client/src/main/java/de/intevation/flys/client/server/was/Response.java @ 2943:7683d4e43afa

Implement class representation of a Web Authentication Service (WAS) request and response. If the authentication is successful the WAS responses with a base64 encoded Security Assertion Markup Language. The current implementation of the saml response simplifies the protocol and misses validation. flys-client/trunk@4909 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Tue, 10 Jul 2012 10:49:18 +0000
parents
children 927a3bd932d5
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/was/Response.java	Tue Jul 10 10:49:18 2012 +0000
@@ -0,0 +1,93 @@
+package de.intevation.flys.client.server.was;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.IOUtils;
+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.Namespace;
+import org.jdom.input.SAXBuilder;
+
+public class Response {
+
+    private static Logger logger = Logger.getLogger(Response.class);
+
+    private Element root;
+    private Assertion assertion;
+
+    public Response(HttpEntity entity) 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")) {
+                // assume base64
+                byte[] content = IOUtils.toByteArray(entity.getContent());
+                in = new ByteArrayInputStream(Base64.decodeBase64(content));
+            }
+
+            SAXBuilder builder = new SAXBuilder();
+            Document doc = builder.build(in);
+            Element root = doc.getRootElement();
+
+            if (root.getName() == "ServiceExceptionReport") {
+                throw new ServiceException(root.getChildText("ServiceException"));
+            }
+
+            this.root = root;
+        }
+        catch(JDOMException e) {
+            logger.error(e);
+        }
+        catch(IOException e) {
+            logger.error(e);
+        }
+    }
+
+    public Element getRoot() {
+        return this.root;
+    }
+
+    public Boolean isSuccess() {
+        return this.getStatus() == "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;
+    }
+}
+// vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:

http://dive4elements.wald.intevation.org