VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / util / TemplateParameters.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * TemplateParameters.cpp
19  * 
20  * Supplies xmltooling TemplateEngine with additional parameters from a PropertySet. 
21  */
22
23 #include "internal.h"
24 #include "util/PropertySet.h"
25 #include "util/TemplateParameters.h"
26
27 #include <ctime>
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/util/URLEncoder.h>
30
31 using namespace shibsp;
32 using namespace xmltooling;
33 using namespace std;
34
35 TemplateParameters::TemplateParameters(const exception* e, const PropertySet* props)
36     : m_exception(e), m_toolingException(dynamic_cast<const XMLToolingException*>(e))
37 {
38     setPropertySet(props);
39 }
40
41 TemplateParameters::~TemplateParameters()
42 {
43 }
44
45 void TemplateParameters::setPropertySet(const PropertySet* props)
46 {
47     m_props = props;
48
49     // Create a timestamp.
50     time_t now = time(nullptr);
51 #if defined(HAVE_CTIME_R_2)
52     char timebuf[32];
53     m_map["now"] = ctime_r(&now,timebuf);
54 #elif defined(HAVE_CTIME_R_3)
55     char timebuf[32];
56     m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
57 #else
58     m_map["now"] = ctime(&now);
59 #endif
60 }
61
62 const XMLToolingException* TemplateParameters::getRichException() const
63 {
64     return m_toolingException;
65 }
66
67 const char* TemplateParameters::getParameter(const char* name) const
68 {
69     if (m_exception) {
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();
74     }
75
76     const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
77     if (pch || !m_props)
78         return pch;
79     pair<bool,const char*> p = m_props->getString(name);
80     return p.first ? p.second : nullptr;
81 }
82
83 string TemplateParameters::toQueryString() const
84 {
85     // Capture local stuff.
86     string q;
87
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());
91
92     // Add in the exception content.
93     if (m_exception) {
94         q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
95         if (m_toolingException)
96             q = q + '&' + m_toolingException->toQueryString();
97     }
98
99     q.erase(0,1);
100     return q;
101 }