Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1SOAPEncoder.cpp
index b112dae..3e1b026 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
 #include "internal.h"
 #include "exceptions.h"
 #include "binding/HTTPResponse.h"
-#include "saml1/binding/SAML1SOAPEncoder.h"
+#include "binding/MessageEncoder.h"
 #include "saml1/core/Protocols.h"
 
 #include <sstream>
@@ -41,6 +41,23 @@ using namespace std;
 
 namespace opensaml {
     namespace saml1p {              
+        class SAML_DLLLOCAL SAML1SOAPEncoder : public MessageEncoder
+        {
+        public:
+            SAML1SOAPEncoder(const DOMElement* e) {}
+            virtual ~SAML1SOAPEncoder() {}
+            
+            long encode(
+                GenericResponse& genericResponse,
+                XMLObject* xmlObject,
+                const char* destination,
+                const char* recipientID=NULL,
+                const char* relayState=NULL,
+                const Credential* credential=NULL,
+                const XMLCh* sigAlgorithm=NULL
+                ) const;
+        };
+
         MessageEncoder* SAML_DLLLOCAL SAML1SOAPEncoderFactory(const DOMElement* const & e)
         {
             return new SAML1SOAPEncoder(e);
@@ -48,15 +65,13 @@ namespace opensaml {
     };
 };
 
-SAML1SOAPEncoder::SAML1SOAPEncoder(const DOMElement* e) {}
-
 long SAML1SOAPEncoder::encode(
     GenericResponse& genericResponse,
     XMLObject* xmlObject,
     const char* destination,
     const char* recipientID,
     const char* relayState,
-    const CredentialResolver* credResolver,
+    const Credential* credential,
     const XMLCh* sigAlgorithm
     ) const
 {
@@ -72,8 +87,8 @@ long SAML1SOAPEncoder::encode(
     genericResponse.setContentType("text/xml");
     HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
     if (httpResponse) {
-        httpResponse->setHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
-        httpResponse->setHeader("Pragma", "no-cache");
+        httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
+        httpResponse->setResponseHeader("Pragma", "no-cache");
     }
 
     DOMElement* rootElement = NULL;
@@ -83,8 +98,8 @@ long SAML1SOAPEncoder::encode(
             Envelope* env = EnvelopeBuilder::buildEnvelope();
             Body* body = BodyBuilder::buildBody();
             env->setBody(body);
-            body->getXMLObjects().push_back(response);
-            if (credResolver ) {
+            body->getUnknownXMLObjects().push_back(response);
+            if (credential) {
                 if (response->getSignature()) {
                     log.debug("response already signed, skipping signature operation");
                     rootElement = env->marshall();
@@ -93,12 +108,14 @@ long SAML1SOAPEncoder::encode(
                     log.debug("signing and marshalling the response");
         
                     // Build a Signature.
-                    Signature* sig = buildSignature(credResolver, sigAlgorithm);
-                    response->setSignature(sig);
+                    Signature* sig = SignatureBuilder::buildSignature();
+                    response->setSignature(sig);    
+                    if (sigAlgorithm)
+                        sig->setSignatureAlgorithm(sigAlgorithm);
             
                     // Sign response while marshalling.
                     vector<Signature*> sigs(1,sig);
-                    rootElement = env->marshall((DOMDocument*)NULL,&sigs);
+                    rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential);
                 }
             }
             else {
@@ -106,9 +123,8 @@ long SAML1SOAPEncoder::encode(
                 rootElement = env->marshall();
             }
             
-            string xmlbuf;
-            XMLHelper::serialize(rootElement, xmlbuf);
-            istringstream s(xmlbuf);
+            stringstream s;
+            s << *rootElement;
             log.debug("sending serialized response");
             long ret = genericResponse.sendResponse(s);
         
@@ -134,7 +150,7 @@ long SAML1SOAPEncoder::encode(
             Envelope* env = EnvelopeBuilder::buildEnvelope();
             Body* body = BodyBuilder::buildBody();
             env->setBody(body);
-            body->getXMLObjects().push_back(fault);
+            body->getUnknownXMLObjects().push_back(fault);
             rootElement = env->marshall();
     
             string xmlbuf;
@@ -166,7 +182,7 @@ long SAML1SOAPEncoder::encode(
         bool error =
             (env->getBody() &&
                 env->getBody()->hasChildren() &&
-                    dynamic_cast<Fault*>(env->getBody()->getXMLObjects().front()));
+                    dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
 
         string xmlbuf;
         XMLHelper::serialize(rootElement, xmlbuf);