annotate flys-client/src/main/java/de/intevation/flys/client/server/was/Response.java @ 2947:645c774b3b98

WAS response: Directly use Base64InputStream instead of reading it first into memory. flys-client/trunk@4922 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 10 Jul 2012 19:55:22 +0000
parents 927a3bd932d5
children
rev   line source
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server.was;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
2
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
3 import java.io.IOException;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
4 import java.io.InputStream;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
5
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
6 import org.apache.commons.codec.binary.Base64InputStream;
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
7
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8 import org.apache.http.HttpEntity;
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
9
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10 import org.apache.log4j.Logger;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12 import org.jdom.Document;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13 import org.jdom.Element;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14 import org.jdom.JDOMException;
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
15
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16 import org.jdom.input.SAXBuilder;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18 public class Response {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 private static Logger logger = Logger.getLogger(Response.class);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 private Element root;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 private Assertion assertion;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25 public Response(HttpEntity entity) throws ServiceException {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27 if (entity == null) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
28 throw new ServiceException("Invalid response");
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
29 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31 String contenttype = entity.getContentType().getValue();
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
33 try {
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
34 InputStream in = entity.getContent();
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
35
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
36 if (!contenttype.equals("application/vnd.ogc.se_xml")) {
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
37 // XXX: Assume base64 encoded content.
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
38 in = new Base64InputStream(in);
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 SAXBuilder builder = new SAXBuilder();
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
42 Document doc = builder.build(in);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43 Element root = doc.getRootElement();
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
44 String rname = root.getName();
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
46 if (rname != null && rname.equals("ServiceExceptionReport")) {
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 throw new ServiceException(root.getChildText("ServiceException"));
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50 this.root = root;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52 catch(JDOMException e) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 logger.error(e);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
55 catch(IOException e) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
56 logger.error(e);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
57 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
58 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
59
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
60 public Element getRoot() {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
61 return this.root;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
62 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
63
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
64 public Boolean isSuccess() {
2947
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
65 String status = getStatus();
645c774b3b98 WAS response: Directly use Base64InputStream instead of reading it first into memory.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2945
diff changeset
66 return status != null && status.equals("samlp:Success");
2943
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
69 public String getStatus() {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
70 Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
71 if (status == null) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
72 return null;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
73 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
74 Element statuscode = status.getChild("StatusCode",
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
75 Namespaces.SAML_NS_PROTO);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
76 if (statuscode == null) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77 return null;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
79 return statuscode.getAttributeValue("Value");
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
80 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82 public Assertion getAssertion() {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
83 if (this.assertion == null && this.root != null) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 Element assertion = this.root.getChild("Assertion",
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
85 Namespaces.SAML_NS_ASSERT);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
86 if (assertion != null) {
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 this.assertion = new Assertion(assertion);
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
89 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
90 return this.assertion;
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
91 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
92 }
7683d4e43afa Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
93 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80:

http://dive4elements.wald.intevation.org