bjoern@2943: package de.intevation.flys.client.server.was; bjoern@2943: bjoern@2943: import java.io.IOException; bjoern@2943: import java.io.InputStream; bjoern@2943: sascha@2947: import org.apache.commons.codec.binary.Base64InputStream; sascha@2947: bjoern@2943: import org.apache.http.HttpEntity; sascha@2947: bjoern@2943: import org.apache.log4j.Logger; bjoern@2943: bjoern@2943: import org.jdom.Document; bjoern@2943: import org.jdom.Element; bjoern@2943: import org.jdom.JDOMException; sascha@2947: bjoern@2943: import org.jdom.input.SAXBuilder; bjoern@2943: bjoern@2943: public class Response { bjoern@2943: bjoern@2943: private static Logger logger = Logger.getLogger(Response.class); bjoern@2943: bjoern@2943: private Element root; bjoern@2943: private Assertion assertion; bjoern@2943: bjoern@2943: public Response(HttpEntity entity) throws ServiceException { bjoern@2943: bjoern@2943: if (entity == null) { bjoern@2943: throw new ServiceException("Invalid response"); bjoern@2943: } bjoern@2943: bjoern@2943: String contenttype = entity.getContentType().getValue(); bjoern@2943: sascha@2947: try { bjoern@2943: InputStream in = entity.getContent(); bjoern@2943: bjoern@2943: if (!contenttype.equals("application/vnd.ogc.se_xml")) { sascha@2947: // XXX: Assume base64 encoded content. sascha@2947: in = new Base64InputStream(in); bjoern@2943: } bjoern@2943: bjoern@2943: SAXBuilder builder = new SAXBuilder(); bjoern@2943: Document doc = builder.build(in); bjoern@2943: Element root = doc.getRootElement(); sascha@2947: String rname = root.getName(); bjoern@2943: sascha@2947: if (rname != null && rname.equals("ServiceExceptionReport")) { bjoern@2943: throw new ServiceException(root.getChildText("ServiceException")); bjoern@2943: } bjoern@2943: bjoern@2943: this.root = root; bjoern@2943: } bjoern@2943: catch(JDOMException e) { bjoern@2943: logger.error(e); bjoern@2943: } bjoern@2943: catch(IOException e) { bjoern@2943: logger.error(e); bjoern@2943: } bjoern@2943: } bjoern@2943: bjoern@2943: public Element getRoot() { bjoern@2943: return this.root; bjoern@2943: } bjoern@2943: bjoern@2943: public Boolean isSuccess() { sascha@2947: String status = getStatus(); sascha@2947: return status != null && status.equals("samlp:Success"); bjoern@2943: } bjoern@2943: bjoern@2943: public String getStatus() { bjoern@2943: Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO); bjoern@2943: if (status == null) { bjoern@2943: return null; bjoern@2943: } bjoern@2943: Element statuscode = status.getChild("StatusCode", bjoern@2943: Namespaces.SAML_NS_PROTO); bjoern@2943: if (statuscode == null) { bjoern@2943: return null; bjoern@2943: } bjoern@2943: return statuscode.getAttributeValue("Value"); bjoern@2943: } bjoern@2943: bjoern@2943: public Assertion getAssertion() { bjoern@2943: if (this.assertion == null && this.root != null) { bjoern@2943: Element assertion = this.root.getChild("Assertion", bjoern@2943: Namespaces.SAML_NS_ASSERT); bjoern@2943: if (assertion != null) { bjoern@2943: this.assertion = new Assertion(assertion); bjoern@2943: } bjoern@2943: } bjoern@2943: return this.assertion; bjoern@2943: } bjoern@2943: } bjoern@2943: // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: