Add drive letter to paths.
[shibboleth/sp.git] / shibsp / util / TemplateParameters.cpp
1 /*
2  *  Copyright 2001-2007 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/TemplateParameters.h"
25
26 #include <ctime>
27 #include <xmltooling/XMLToolingConfig.h>
28 #include <xmltooling/util/URLEncoder.h>
29
30 using namespace shibsp;
31 using namespace xmltooling;
32 using namespace std;
33
34 void TemplateParameters::setPropertySet(const PropertySet* props)
35 {
36     m_props = props;
37
38     // Create a timestamp.
39     time_t now = time(NULL);
40 #if defined(HAVE_CTIME_R_2)
41     char timebuf[32];
42     m_map["now"] = ctime_r(&now,timebuf);
43 #elif defined(HAVE_CTIME_R_3)
44     char timebuf[32];
45     m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
46 #else
47     m_map["now"] = ctime(&now);
48 #endif
49 }
50
51 const char* TemplateParameters::getParameter(const char* name) const
52 {
53     if (m_exception) {
54         if (!strcmp(name, "errorType"))
55             return m_toolingException ? m_toolingException->getClassName() : "std::exception";
56         else if (!strcmp(name, "errorText"))
57             return m_exception->what();
58     }
59
60     const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
61     if (pch || !m_props)
62         return pch;
63     pair<bool,const char*> p = m_props->getString(name);
64     return p.first ? p.second : NULL;
65 }
66
67 string TemplateParameters::toQueryString() const
68 {
69     // Capture local stuff.
70     string q;
71
72     const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
73     for (map<string,string>::const_iterator i=m_map.begin(); i!=m_map.end(); i++)
74         q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
75
76     // Add in the exception content.
77     if (m_exception) {
78         q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
79         if (m_toolingException)
80             q = q + '&' + m_toolingException->toQueryString();
81     }
82
83     q.erase(0,1);
84     return q;
85 }