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 <xercesc/util/XMLUniDefs.hpp>
39 using namespace xmltooling;
40 using namespace xercesc;
42 using xmlconstants::XMLTOOLING_NS;
44 params::params(int count,...)
49 v.push_back(va_arg(args,char*));
53 namedparams::namedparams(int count,...)
59 v.push_back(va_arg(args,char*));
63 XMLToolingException::ExceptionFactoryMap XMLToolingException::m_factoryMap;
65 XMLToolingException* XMLToolingException::getInstance(const char* exceptionClass)
68 ExceptionFactoryMap::const_iterator i=m_factoryMap.find(exceptionClass);
69 if (i!=m_factoryMap.end())
72 return new XMLToolingException();
75 XMLToolingException::XMLToolingException(const char* msg, const params& p)
82 XMLToolingException::XMLToolingException(const char* msg, const namedparams& p)
89 XMLToolingException::XMLToolingException(const std::string& msg, const params& p) : m_msg(msg)
94 XMLToolingException::XMLToolingException(const std::string& msg, const namedparams& p) : m_msg(msg)
99 void XMLToolingException::setMessage(const char* msg)
105 m_processedmsg.erase();
108 inline const char* get_digit_character()
110 static const char s_characters[19] =
132 static const char *s_mid = s_characters + 9;
137 inline const char* unsigned_integer_to_string(char* buf, size_t cchBuf, size_t i)
139 char* psz=buf + cchBuf - 1; // Set psz to last char
140 *psz = 0; // Set terminating null
143 size_t lsd = i % 10; // Get least significant
146 i /= 10; // Prepare for next most
151 *psz = get_digit_character()[lsd]; // Place the digit
153 } while(i!=0 && psz>buf);
158 void XMLToolingException::addProperties(const params& p)
160 m_processedmsg.erase();
161 map<string,string>::size_type i=m_params.size()+1;
163 const vector<const char*>& v=p.get();
164 for (vector<const char*>::const_iterator ci=v.begin(); ci!=v.end(); ci++) {
165 m_params[unsigned_integer_to_string(buf,sizeof(buf),i++)] = *ci;
169 void XMLToolingException::addProperties(const namedparams& p)
171 m_processedmsg.erase();
172 const vector<const char*>& v=p.get();
173 for (vector<const char*>::const_iterator ci=v.begin(); ci!=v.end(); ci++) {
175 m_params[*ci] = *(ci+1);
176 ci++; // advance past name to value, then loop will advance it again
180 const char* XMLToolingException::getProperty(unsigned int index) const
183 map<string,string>::const_iterator i=m_params.find(unsigned_integer_to_string(buf,sizeof(buf),index));
184 return (i==m_params.end()) ? nullptr : i->second.c_str();
187 const char* XMLToolingException::getProperty(const char* name) const
189 map<string,string>::const_iterator i=m_params.find(name);
190 return (i==m_params.end()) ? nullptr : i->second.c_str();
193 const char* XMLToolingException::getMessage() const
195 if (!m_processedmsg.empty())
196 return m_processedmsg.c_str();
197 else if (m_params.empty())
198 return m_msg.c_str();
200 static const char* legal="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_";
202 // Replace any parameters in the message.
203 string::size_type i=0,start=0;
204 while (start!=string::npos && start<m_msg.length() && (i=m_msg.find("$",start))!=string::npos) {
206 m_processedmsg += m_msg.substr(start,i-start); // append everything in between
207 start=i+1; // move start to the beginning of the token name
208 i=m_msg.find_first_not_of(legal,start); // find token delimiter
209 if (i==start) { // append a non legal character
210 m_processedmsg+=m_msg[start++];
214 // search for token in map
215 map<string,string>::const_iterator param=m_params.find(m_msg.substr(start,(i==string::npos) ? i : i-start));
216 if (param!=m_params.end()) {
217 m_processedmsg+=param->second;
221 if (start!=string::npos && start<m_msg.length())
222 m_processedmsg += m_msg.substr(start,i); // append rest of string
223 return m_processedmsg.c_str();
226 void xml_encode(string& s, const char* pre, const char* start, const char* post)
230 while (start && *start) {
231 pos = strcspn(start, "\"<>&");
233 s.append(start, pos);
238 case '\'': s += "'"; break;
239 case '<': s += "<"; break;
240 case '>': s += ">"; break;
241 case '&': s += "&"; break;
242 default: s += *start;
250 string XMLToolingException::toString() const
252 string xml=string("<exception xmlns='http://www.opensaml.org/xmltooling' type='") + getClassName() + "'>";
253 const char* msg=getMessage();
255 xml_encode(xml, "<message>", msg, "</message>");
256 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
257 for (map<string,string>::const_iterator i=m_params.begin(); i!=m_params.end(); i++) {
258 xml_encode(xml, "<param name='", i->first.c_str(), "'");
259 xml_encode(xml, ">", encoder->encode(i->second.c_str()).c_str(), "</param>");
265 string XMLToolingException::toQueryString() const
268 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
269 for (map<string,string>::const_iterator i=m_params.begin(); i!=m_params.end(); i++) {
272 q = q + i->first + '=' + enc->encode(i->second.c_str());
277 XMLToolingException* XMLToolingException::fromStream(std::istream& in)
279 static const XMLCh exception[] = UNICODE_LITERAL_9(e,x,c,e,p,t,i,o,n);
280 static const XMLCh message[] = UNICODE_LITERAL_7(m,e,s,s,a,g,e);
281 static const XMLCh name[] = UNICODE_LITERAL_4(n,a,m,e);
282 static const XMLCh param[] = UNICODE_LITERAL_5(p,a,r,a,m);
283 static const XMLCh type[] = UNICODE_LITERAL_4(t,y,p,e);
285 DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
287 // Check root element.
288 const DOMElement* root=doc->getDocumentElement();
289 if (!XMLHelper::isNodeNamed(root,XMLTOOLING_NS,exception)) {
291 throw XMLToolingException("Invalid root element on serialized exception.");
294 auto_ptr_char classname(root->getAttributeNS(nullptr,type));
295 auto_ptr<XMLToolingException> excep(XMLToolingException::getInstance(classname.get()));
297 DOMElement* child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,message);
298 if (child && child->hasChildNodes()) {
299 auto_ptr_char m(child->getFirstChild()->getNodeValue());
300 excep->setMessage(m.get());
303 const URLEncoder* encoder = XMLToolingConfig::getConfig().getURLEncoder();
304 child=XMLHelper::getFirstChildElement(root,XMLTOOLING_NS,param);
305 while (child && child->hasChildNodes()) {
306 auto_ptr_char n(child->getAttributeNS(nullptr,name));
307 char* encoded = XMLString::transcode(child->getFirstChild()->getNodeValue());
308 if (n.get() && encoded) {
309 encoder->decode(encoded);
310 excep->addProperty(n.get(), encoded);
312 XMLString::release(&encoded);
313 child=XMLHelper::getNextSiblingElement(child,XMLTOOLING_NS,param);
317 return excep.release();
320 XMLToolingException* XMLToolingException::fromString(const char* s)
323 return fromStream(in);