2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
28 #include "exceptions.h"
29 #include "XMLToolingConfig.h"
30 #include "util/URLEncoder.h"
31 #include "util/XMLConstants.h"
32 #include "util/XMLHelper.h"
37 #include <boost/lexical_cast.hpp>
38 #include <xercesc/util/XMLUniDefs.hpp>
40 using namespace xmltooling;
41 using namespace xercesc;
42 using namespace boost;
44 using xmlconstants::XMLTOOLING_NS;
46 params::params(int count,...)
51 v.push_back(va_arg(args,char*));
55 namedparams::namedparams(int count,...)
61 v.push_back(va_arg(args,char*));
65 XMLToolingException::ExceptionFactoryMap XMLToolingException::m_factoryMap;
67 XMLToolingException* XMLToolingException::getInstance(const char* exceptionClass)
70 ExceptionFactoryMap::const_iterator i=m_factoryMap.find(exceptionClass);
71 if (i!=m_factoryMap.end())
74 return new XMLToolingException();
77 XMLToolingException::XMLToolingException(const char* msg, const params& p)
84 XMLToolingException::XMLToolingException(const char* msg, const namedparams& p)
91 XMLToolingException::XMLToolingException(const std::string& msg, const params& p) : m_msg(msg)
96 XMLToolingException::XMLToolingException(const std::string& msg, const namedparams& p) : m_msg(msg)
101 void XMLToolingException::setMessage(const char* msg)
107 m_processedmsg.erase();
110 void XMLToolingException::addProperties(const params& p)
112 m_processedmsg.erase();
113 map<string,string>::size_type i = m_params.size() + 1;
114 const vector<const char*>& v=p.get();
115 for (vector<const char*>::const_iterator ci = v.begin(); ci != v.end(); ++ci) {
117 m_params[lexical_cast<string>(i++)] = *ci;
119 catch (bad_lexical_cast&) {
124 void XMLToolingException::addProperties(const namedparams& p)
126 m_processedmsg.erase();
127 const vector<const char*>& v = p.get();
128 for (vector<const char*>::const_iterator ci = v.begin(); ci != v.end(); ++ci) {
130 m_params[*ci] = *(ci+1);
131 ++ci; // advance past name to value, then loop will advance it again
135 const char* XMLToolingException::getProperty(unsigned int index) const
138 map<string,string>::const_iterator i = m_params.find(lexical_cast<string>(index));
139 return (i==m_params.end()) ? nullptr : i->second.c_str();
141 catch (bad_lexical_cast&) {
146 const char* XMLToolingException::getProperty(const char* name) const
148 map<string,string>::const_iterator i = m_params.find(name);
149 return (i==m_params.end()) ? nullptr : i->second.c_str();
152 const char* XMLToolingException::getMessage() const
154 if (!m_processedmsg.empty())
155 return m_processedmsg.c_str();
156 else if (m_params.empty())
157 return m_msg.c_str();
159 static const char* legal="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_";
161 // Replace any parameters in the message.
162 string::size_type i=0,start=0;
163 while (start!=string::npos && start<m_msg.length() && (i=m_msg.find("$",start))!=string::npos) {
165 m_processedmsg += m_msg.substr(start,i-start); // append everything in between
166 start=i+1; // move start to the beginning of the token name
167 i=m_msg.find_first_not_of(legal,start); // find token delimiter
168 if (i==start) { // append a non legal character
169 m_processedmsg+=m_msg[start++];
173 // search for token in map
174 map<string,string>::const_iterator param=m_params.find(m_msg.substr(start,(i==string::npos) ? i : i-start));
175 if (param!=m_params.end()) {
176 m_processedmsg+=param->second;
180 if (start!=string::npos && start<m_msg.length())
181 m_processedmsg += m_msg.substr(start,i); // append rest of string
182 return m_processedmsg.c_str();
185 void xml_encode(string& s, const char* pre, const char* start, const char* post)
189 while (start && *start) {
190 pos = strcspn(start, "\"<>&");
192 s.append(start, pos);
197 case '\'': s += "'"; break;
198 case '<': s += "<"; break;
199 case '>': s += ">"; break;
200 case '&': s += "&"; break;
201 default: s += *start;
209 string XMLToolingException::toString() const
211 string xml=string("<exception xmlns='http://www.opensaml.org/xmltooling' type='") + getClassName() + "'>";
212 const char* msg=getMessage();
214 xml_encode(xml, "<message>", msg, "</message>");
215 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
216 for (map<string,string>::const_iterator i = m_params.begin(); i != m_params.end(); ++i) {
217 xml_encode(xml, "<param name='", i->first.c_str(), "'");
218 xml_encode(xml, ">", encoder->encode(i->second.c_str()).c_str(), "</param>");
224 string XMLToolingException::toQueryString() const
227 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
228 for (map<string,string>::const_iterator i = m_params.begin(); i != m_params.end(); ++i) {
231 q = q + i->first + '=' + enc->encode(i->second.c_str());
236 XMLToolingException* XMLToolingException::fromStream(std::istream& in)
238 static const XMLCh exception[] = UNICODE_LITERAL_9(e,x,c,e,p,t,i,o,n);
239 static const XMLCh message[] = UNICODE_LITERAL_7(m,e,s,s,a,g,e);
240 static const XMLCh name[] = UNICODE_LITERAL_4(n,a,m,e);
241 static const XMLCh param[] = UNICODE_LITERAL_5(p,a,r,a,m);
242 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
244 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
246 // Check root element.
247 const DOMElement* root=doc->getDocumentElement();
248 if (!XMLHelper::isNodeNamed(root,XMLTOOLING_NS,exception)) {
250 throw XMLToolingException("Invalid root element on serialized exception.");
253 auto_ptr_char classname(root->getAttributeNS(nullptr,type));
254 auto_ptr<XMLToolingException> excep(XMLToolingException::getInstance(classname.get()));
256 DOMElement* child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,message);
257 if (child && child->hasChildNodes()) {
258 auto_ptr_char m(child->getFirstChild()->getNodeValue());
259 excep->setMessage(m.get());
262 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
263 child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,param);
264 while (child && child->hasChildNodes()) {
265 auto_ptr_char n(child->getAttributeNS(nullptr,name));
266 char* encoded = XMLString::transcode(child->getFirstChild()->getNodeValue());
267 if (n.get() && encoded) {
268 encoder->decode(encoded);
269 excep->addProperty(n.get(), encoded);
271 XMLString::release(&encoded);
272 child=XMLHelper::getNextSiblingElement(child,XMLTOOLING_NS,param);
276 return excep.release();
279 XMLToolingException* XMLToolingException::fromString(const char* s)
282 return fromStream(in);