comparison gwt-client/src/main/java/org/dive4elements/river/client/server/auth/was/Response.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/server/auth/was/Response.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.server.auth.was;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import org.apache.commons.codec.binary.Base64InputStream;
8
9 import org.apache.http.HttpEntity;
10
11 import org.apache.log4j.Logger;
12
13 import org.jdom.Document;
14 import org.jdom.Element;
15 import org.jdom.JDOMException;
16 import org.jdom.input.SAXBuilder;
17
18 import org.dive4elements.river.client.server.auth.Authentication;
19 import org.dive4elements.river.client.server.auth.AuthenticationException;
20
21 import org.dive4elements.river.client.server.features.Features;
22
23 public class Response implements Authentication {
24
25 private static Logger logger = Logger.getLogger(Response.class);
26
27 private Element root;
28 private Assertion assertion;
29 private String username;
30 private String password;
31 private Features features;
32
33
34 public Response(HttpEntity entity, String username, String password, Features features) throws AuthenticationException, IOException {
35
36 if (entity == null) {
37 throw new ServiceException("Invalid response");
38 }
39
40 String contenttype = entity.getContentType().getValue();
41
42 try {
43 InputStream in = entity.getContent();
44
45 if (!contenttype.equals("application/vnd.ogc.se_xml")) {
46 // XXX: Assume base64 encoded content.
47 in = new Base64InputStream(in);
48 }
49
50 SAXBuilder builder = new SAXBuilder();
51 Document doc = builder.build(in);
52 Element root = doc.getRootElement();
53 String rname = root.getName();
54
55 if (rname != null && rname.equals("ServiceExceptionReport")) {
56 throw new ServiceException(root.getChildText("ServiceException"));
57 }
58
59 this.root = root;
60 this.username = username;
61 this.password = password;
62 this.features = features;
63
64 }
65 catch(JDOMException e) {
66 throw new AuthenticationException(e);
67 }
68 }
69
70 public Element getRoot() {
71 return this.root;
72 }
73
74 @Override
75 public boolean isSuccess() {
76 String status = getStatus();
77 return status != null && status.equals("samlp:Success");
78 }
79
80 public String getStatus() {
81 Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO);
82 if (status == null) {
83 return null;
84 }
85 Element statuscode = status.getChild("StatusCode",
86 Namespaces.SAML_NS_PROTO);
87 if (statuscode == null) {
88 return null;
89 }
90 return statuscode.getAttributeValue("Value");
91 }
92
93 public Assertion getAssertion() {
94 if (this.assertion == null && this.root != null) {
95 Element assertion = this.root.getChild("Assertion",
96 Namespaces.SAML_NS_ASSERT);
97 if (assertion != null) {
98 this.assertion = new Assertion(assertion);
99 }
100 }
101 return this.assertion;
102 }
103
104 @Override
105 public User getUser() throws AuthenticationException {
106 Assertion assertion = this.getAssertion();
107 if (assertion == null) {
108 throw new AuthenticationException("Response doesn't contain an assertion");
109 }
110 List<String> features = this.features.getFeatures(
111 this.assertion.getRoles());
112 logger.debug("User " + this.username + " with features " + features +
113 " successfully authenticated.");
114 return new User(this.username, this.password, assertion.getNameID(),
115 this.assertion.getRoles(), assertion, features);
116 }
117 }
118 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:

http://dive4elements.wald.intevation.org