comparison 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
comparison
equal deleted inserted replaced
2942:5885c5b8d71d 2943:7683d4e43afa
1 package de.intevation.flys.client.server.was;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6
7 import org.apache.commons.codec.binary.Base64;
8 import org.apache.commons.io.IOUtils;
9 import org.apache.http.HttpEntity;
10 import org.apache.log4j.Logger;
11
12 import org.jdom.Document;
13 import org.jdom.Element;
14 import org.jdom.JDOMException;
15 import org.jdom.Namespace;
16 import org.jdom.input.SAXBuilder;
17
18 public class Response {
19
20 private static Logger logger = Logger.getLogger(Response.class);
21
22 private Element root;
23 private Assertion assertion;
24
25 public Response(HttpEntity entity) throws ServiceException {
26
27 if (entity == null) {
28 throw new ServiceException("Invalid response");
29 }
30
31 String contenttype = entity.getContentType().getValue();
32
33
34 try{
35 InputStream in = entity.getContent();
36
37 if (!contenttype.equals("application/vnd.ogc.se_xml")) {
38 // assume base64
39 byte[] content = IOUtils.toByteArray(entity.getContent());
40 in = new ByteArrayInputStream(Base64.decodeBase64(content));
41 }
42
43 SAXBuilder builder = new SAXBuilder();
44 Document doc = builder.build(in);
45 Element root = doc.getRootElement();
46
47 if (root.getName() == "ServiceExceptionReport") {
48 throw new ServiceException(root.getChildText("ServiceException"));
49 }
50
51 this.root = root;
52 }
53 catch(JDOMException e) {
54 logger.error(e);
55 }
56 catch(IOException e) {
57 logger.error(e);
58 }
59 }
60
61 public Element getRoot() {
62 return this.root;
63 }
64
65 public Boolean isSuccess() {
66 return this.getStatus() == "samlp:Success";
67 }
68
69 public String getStatus() {
70 Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO);
71 if (status == null) {
72 return null;
73 }
74 Element statuscode = status.getChild("StatusCode",
75 Namespaces.SAML_NS_PROTO);
76 if (statuscode == null) {
77 return null;
78 }
79 return statuscode.getAttributeValue("Value");
80 }
81
82 public Assertion getAssertion() {
83 if (this.assertion == null && this.root != null) {
84 Element assertion = this.root.getChild("Assertion",
85 Namespaces.SAML_NS_ASSERT);
86 if (assertion != null) {
87 this.assertion = new Assertion(assertion);
88 }
89 }
90 return this.assertion;
91 }
92 }
93 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:

http://dive4elements.wald.intevation.org