2 * Copyright 2001-2010 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 * TemplateParameters.cpp
20 * Supplies xmltooling TemplateEngine with additional parameters from a PropertySet.
24 #include "util/PropertySet.h"
25 #include "util/TemplateParameters.h"
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/util/URLEncoder.h>
31 using namespace shibsp;
32 using namespace xmltooling;
35 TemplateParameters::TemplateParameters(const exception* e, const PropertySet* props)
36 : m_exception(e), m_toolingException(dynamic_cast<const XMLToolingException*>(e))
38 setPropertySet(props);
41 TemplateParameters::~TemplateParameters()
45 void TemplateParameters::setPropertySet(const PropertySet* props)
49 // Create a timestamp.
50 time_t now = time(nullptr);
51 #if defined(HAVE_CTIME_R_2)
53 m_map["now"] = ctime_r(&now,timebuf);
54 #elif defined(HAVE_CTIME_R_3)
56 m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
58 m_map["now"] = ctime(&now);
62 const XMLToolingException* TemplateParameters::getRichException() const
64 return m_toolingException;
67 const char* TemplateParameters::getParameter(const char* name) const
70 if (!strcmp(name, "errorType"))
71 return m_toolingException ? m_toolingException->getClassName() : "std::exception";
72 else if (!strcmp(name, "errorText"))
73 return m_exception->what();
76 const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
79 pair<bool,const char*> p = m_props->getString(name);
80 return p.first ? p.second : nullptr;
83 string TemplateParameters::toQueryString() const
85 // Capture local stuff.
88 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
89 for (map<string,string>::const_iterator i=m_map.begin(); i!=m_map.end(); i++)
90 q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
92 // Add in the exception content.
94 q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
95 if (m_toolingException)
96 q = q + '&' + m_toolingException->toQueryString();