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);
68 const XMLToolingException* TemplateParameters::getRichException() const
70 return m_toolingException;
73 const char* TemplateParameters::getParameter(const char* name) const
76 if (!strcmp(name, "errorType"))
77 return m_toolingException ? m_toolingException->getClassName() : "std::exception";
78 else if (!strcmp(name, "errorText"))
79 return m_exception->what();
82 const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
87 if (!strcmp(name, "entityID"))
88 return m_session->getEntityID();
90 const multimap<string,const Attribute*>& attrs = m_session->getIndexedAttributes();
91 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> walker;
92 for (walker = attrs.equal_range(name); walker.first != walker.second; ++walker.first) {
93 if (walker.first->second->valueCount() > 0)
94 return walker.first->second->getSerializedValues().front().c_str();
99 pair<bool,const char*> p = m_props->getString(name);
107 string TemplateParameters::toQueryString() const
109 // Capture local stuff.
112 const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
113 for (map<string,string>::const_iterator i = m_map.begin(); i != m_map.end(); ++i)
114 q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
116 // Add in the exception content.
118 q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
119 if (m_toolingException)
120 q = q + '&' + m_toolingException->toQueryString();