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.
22 * TemplateParameters.cpp
24 * Supplies xmltooling TemplateEngine with additional parameters from a PropertySet.
28 #include "util/PropertySet.h"
29 #include "util/TemplateParameters.h"
32 #include <xmltooling/XMLToolingConfig.h>
33 #include <xmltooling/util/URLEncoder.h>
35 using namespace shibsp;
36 using namespace xmltooling;
39 TemplateParameters::TemplateParameters(const exception* e, const PropertySet* props)
40 : m_exception(e), m_toolingException(dynamic_cast<const XMLToolingException*>(e))
42 setPropertySet(props);
45 TemplateParameters::~TemplateParameters()
49 void TemplateParameters::setPropertySet(const PropertySet* props)
53 // Create a timestamp.
54 time_t now = time(nullptr);
55 #if defined(HAVE_CTIME_R_2)
57 m_map["now"] = ctime_r(&now,timebuf);
58 #elif defined(HAVE_CTIME_R_3)
60 m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
62 m_map["now"] = ctime(&now);
66 const XMLToolingException* TemplateParameters::getRichException() const
68 return m_toolingException;
71 const char* TemplateParameters::getParameter(const char* name) const
74 if (!strcmp(name, "errorType"))
75 return m_toolingException ? m_toolingException->getClassName() : "std::exception";
76 else if (!strcmp(name, "errorText"))
77 return m_exception->what();
80 const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
83 pair<bool,const char*> p = m_props->getString(name);
84 return p.first ? p.second : nullptr;
87 string TemplateParameters::toQueryString() const
89 // Capture local stuff.
92 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
93 for (map<string,string>::const_iterator i=m_map.begin(); i!=m_map.end(); i++)
94 q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
96 // Add in the exception content.
98 q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
99 if (m_toolingException)
100 q = q + '&' + m_toolingException->toQueryString();