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 "SessionCache.h"
29 #include "attribute/Attribute.h"
30 #include "util/PropertySet.h"
31 #include "util/TemplateParameters.h"
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/util/URLEncoder.h>
37 using namespace shibsp;
38 using namespace xmltooling;
41 TemplateParameters::TemplateParameters(const exception* e, const PropertySet* props, const Session* session)
42 : m_exception(e), m_toolingException(dynamic_cast<const XMLToolingException*>(e)), m_session(session)
44 setPropertySet(props);
47 TemplateParameters::~TemplateParameters()
51 void TemplateParameters::setPropertySet(const PropertySet* props)
55 // Create a timestamp.
56 time_t now = time(nullptr);
57 #if defined(HAVE_CTIME_R_2)
59 m_map["now"] = ctime_r(&now,timebuf);
60 #elif defined(HAVE_CTIME_R_3)
62 m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
64 m_map["now"] = ctime(&now);
66 string& s = m_map["now"];
67 s.erase(s.begin() + s.size() - 1);
70 const XMLToolingException* TemplateParameters::getRichException() const
72 return m_toolingException;
75 const char* TemplateParameters::getParameter(const char* name) const
78 if (!strcmp(name, "errorType"))
79 return m_toolingException ? m_toolingException->getClassName() : "std::exception";
80 else if (!strcmp(name, "errorText"))
81 return m_exception->what();
84 const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
89 if (!strcmp(name, "entityID"))
90 return m_session->getEntityID();
92 const multimap<string,const Attribute*>& attrs = m_session->getIndexedAttributes();
93 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> walker;
94 for (walker = attrs.equal_range(name); walker.first != walker.second; ++walker.first) {
95 if (walker.first->second->valueCount() > 0)
96 return walker.first->second->getSerializedValues().front().c_str();
101 pair<bool,const char*> p = m_props->getString(name);
109 string TemplateParameters::toQueryString() const
111 // Capture local stuff.
114 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
115 for (map<string,string>::const_iterator i = m_map.begin(); i != m_map.end(); ++i)
116 q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
118 // Add in the exception content.
120 q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
121 if (m_toolingException)
122 q = q + '&' + m_toolingException->toQueryString();