2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * SAML1SOAPEncoder.cpp
20 * SAML 1.x SOAP binding message encoder
24 #include "exceptions.h"
25 #include "binding/HTTPResponse.h"
26 #include "saml1/binding/SAML1SOAPEncoder.h"
27 #include "saml1/core/Protocols.h"
30 #include <log4cpp/Category.hh>
31 #include <xmltooling/util/NDC.h>
32 #include <xmltooling/soap/SOAP.h>
34 using namespace opensaml::saml1p;
35 using namespace opensaml;
36 using namespace xmlsignature;
37 using namespace soap11;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 MessageEncoder* SAML_DLLLOCAL SAML1SOAPEncoderFactory(const DOMElement* const & e)
46 return new SAML1SOAPEncoder(e);
51 SAML1SOAPEncoder::SAML1SOAPEncoder(const DOMElement* e) {}
53 long SAML1SOAPEncoder::encode(
54 GenericResponse& genericResponse,
56 const char* destination,
57 const char* recipientID,
58 const char* relayState,
59 const CredentialResolver* credResolver,
60 const XMLCh* sigAlgorithm
64 xmltooling::NDC ndc("encode");
66 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1SOAP");
68 log.debug("validating input");
69 if (xmlObject->getParent())
70 throw BindingException("Cannot encode XML content with parent.");
72 genericResponse.setContentType("text/xml");
74 DOMElement* rootElement = NULL;
75 Response* response = dynamic_cast<Response*>(xmlObject);
78 Envelope* env = EnvelopeBuilder::buildEnvelope();
79 Body* body = BodyBuilder::buildBody();
81 body->getXMLObjects().push_back(response);
83 if (response->getSignature()) {
84 log.debug("response already signed, skipping signature operation");
85 rootElement = env->marshall();
88 log.debug("signing and marshalling the response");
91 Signature* sig = buildSignature(credResolver, sigAlgorithm);
92 response->setSignature(sig);
94 // Sign response while marshalling.
95 vector<Signature*> sigs(1,sig);
96 rootElement = env->marshall((DOMDocument*)NULL,&sigs);
100 log.debug("marshalling the response");
101 rootElement = env->marshall();
105 XMLHelper::serialize(rootElement, xmlbuf);
106 istringstream s(xmlbuf);
107 log.debug("sending serialized response");
108 long ret = genericResponse.sendResponse(s);
110 // Cleanup by destroying XML.
114 catch (XMLToolingException&) {
115 // A bit weird...we have to "revert" things so that the response is isolated
116 // so the caller can free it.
117 if (response->getParent()) {
118 response->getParent()->detach();
125 Fault* fault = dynamic_cast<Fault*>(xmlObject);
128 log.debug("building Envelope and marshalling Fault");
129 Envelope* env = EnvelopeBuilder::buildEnvelope();
130 Body* body = BodyBuilder::buildBody();
132 body->getXMLObjects().push_back(fault);
133 rootElement = env->marshall();
136 XMLHelper::serialize(rootElement, xmlbuf);
137 istringstream s(xmlbuf);
138 log.debug("sending serialized fault");
139 long ret = genericResponse.sendError(s);
141 // Cleanup by destroying XML.
145 catch (XMLToolingException&) {
146 // A bit weird...we have to "revert" things so that the fault is isolated
147 // so the caller can free it.
148 if (fault->getParent()) {
149 fault->getParent()->detach();
156 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
158 log.debug("marshalling envelope");
159 rootElement = env->marshall();
163 env->getBody()->hasChildren() &&
164 dynamic_cast<Fault*>(env->getBody()->getXMLObjects().front()));
167 XMLHelper::serialize(rootElement, xmlbuf);
168 istringstream s(xmlbuf);
169 log.debug("sending serialized envelope");
170 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
172 // Cleanup by destroying XML.
177 throw BindingException("XML content for SAML 1.x SOAP Encoder must be a SAML 1.x <Response> or SOAP Fault/Envelope.");