Change license header.
[shibboleth/sp.git] / shibsp / util / TemplateParameters.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * TemplateParameters.cpp
23  * 
24  * Supplies xmltooling TemplateEngine with additional parameters from a PropertySet. 
25  */
26
27 #include "internal.h"
28 #include "util/PropertySet.h"
29 #include "util/TemplateParameters.h"
30
31 #include <ctime>
32 #include <xmltooling/XMLToolingConfig.h>
33 #include <xmltooling/util/URLEncoder.h>
34
35 using namespace shibsp;
36 using namespace xmltooling;
37 using namespace std;
38
39 TemplateParameters::TemplateParameters(const exception* e, const PropertySet* props)
40     : m_exception(e), m_toolingException(dynamic_cast<const XMLToolingException*>(e))
41 {
42     setPropertySet(props);
43 }
44
45 TemplateParameters::~TemplateParameters()
46 {
47 }
48
49 void TemplateParameters::setPropertySet(const PropertySet* props)
50 {
51     m_props = props;
52
53     // Create a timestamp.
54     time_t now = time(nullptr);
55 #if defined(HAVE_CTIME_R_2)
56     char timebuf[32];
57     m_map["now"] = ctime_r(&now,timebuf);
58 #elif defined(HAVE_CTIME_R_3)
59     char timebuf[32];
60     m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
61 #else
62     m_map["now"] = ctime(&now);
63 #endif
64 }
65
66 const XMLToolingException* TemplateParameters::getRichException() const
67 {
68     return m_toolingException;
69 }
70
71 const char* TemplateParameters::getParameter(const char* name) const
72 {
73     if (m_exception) {
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();
78     }
79
80     const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
81     if (pch || !m_props)
82         return pch;
83     pair<bool,const char*> p = m_props->getString(name);
84     return p.first ? p.second : nullptr;
85 }
86
87 string TemplateParameters::toQueryString() const
88 {
89     // Capture local stuff.
90     string q;
91
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());
95
96     // Add in the exception content.
97     if (m_exception) {
98         q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
99         if (m_toolingException)
100             q = q + '&' + m_toolingException->toQueryString();
101     }
102
103     q.erase(0,1);
104     return q;
105 }