Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
2955:f1030909eeb6 | 2956:d7f76f197d89 |
---|---|
1 package de.intevation.flys.client.server.auth.was; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 | |
6 import org.apache.commons.codec.binary.Base64InputStream; | |
7 | |
8 import org.apache.http.HttpEntity; | |
9 | |
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.input.SAXBuilder; | |
16 | |
17 import de.intevation.flys.client.server.auth.Authentication; | |
18 | |
19 public class Response implements Authentication { | |
20 | |
21 private static Logger logger = Logger.getLogger(Response.class); | |
22 | |
23 private Element root; | |
24 private Assertion assertion; | |
25 private String username; | |
26 private String password; | |
27 | |
28 | |
29 public Response(HttpEntity entity, String username, String password) throws ServiceException { | |
30 | |
31 if (entity == null) { | |
32 throw new ServiceException("Invalid response"); | |
33 } | |
34 | |
35 String contenttype = entity.getContentType().getValue(); | |
36 | |
37 try { | |
38 InputStream in = entity.getContent(); | |
39 | |
40 if (!contenttype.equals("application/vnd.ogc.se_xml")) { | |
41 // XXX: Assume base64 encoded content. | |
42 in = new Base64InputStream(in); | |
43 } | |
44 | |
45 SAXBuilder builder = new SAXBuilder(); | |
46 Document doc = builder.build(in); | |
47 Element root = doc.getRootElement(); | |
48 String rname = root.getName(); | |
49 | |
50 if (rname != null && rname.equals("ServiceExceptionReport")) { | |
51 throw new ServiceException(root.getChildText("ServiceException")); | |
52 } | |
53 | |
54 this.root = root; | |
55 this.username = username; | |
56 this.password = password; | |
57 | |
58 } | |
59 catch(JDOMException e) { | |
60 logger.error(e); | |
61 } | |
62 catch(IOException e) { | |
63 logger.error(e); | |
64 } | |
65 } | |
66 | |
67 public Element getRoot() { | |
68 return this.root; | |
69 } | |
70 | |
71 @Override | |
72 public boolean isSuccess() { | |
73 String status = getStatus(); | |
74 return status != null && status.equals("samlp:Success"); | |
75 } | |
76 | |
77 public String getStatus() { | |
78 Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO); | |
79 if (status == null) { | |
80 return null; | |
81 } | |
82 Element statuscode = status.getChild("StatusCode", | |
83 Namespaces.SAML_NS_PROTO); | |
84 if (statuscode == null) { | |
85 return null; | |
86 } | |
87 return statuscode.getAttributeValue("Value"); | |
88 } | |
89 | |
90 public Assertion getAssertion() { | |
91 if (this.assertion == null && this.root != null) { | |
92 Element assertion = this.root.getChild("Assertion", | |
93 Namespaces.SAML_NS_ASSERT); | |
94 if (assertion != null) { | |
95 this.assertion = new Assertion(assertion); | |
96 } | |
97 } | |
98 return this.assertion; | |
99 } | |
100 | |
101 public User getUser() { | |
102 return new User(this.username, this.password); | |
103 } | |
104 } | |
105 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: |