Clean Solaris build.
[shibboleth/cpp-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 #ifdef HAVE_CTIME_R
41     char timebuf[32];
42     m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
43 #else
44     m_map["now"] = ctime(&now);
45 #endif
46 }
47
48 const char* TemplateParameters::getParameter(const char* name) const
49 {
50     if (m_exception) {
51         if (!strcmp(name, "errorType"))
52             return m_toolingException ? m_toolingException->getClassName() : "std::exception";
53         else if (!strcmp(name, "errorText"))
54             return m_exception->what();
55     }
56
57     const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
58     if (pch || !m_props)
59         return pch;
60     pair<bool,const char*> p = m_props->getString(name);
61     return p.first ? p.second : NULL;
62 }
63
64 string TemplateParameters::toQueryString() const
65 {
66     // Capture local stuff.
67     string q;
68
69     const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
70     for (map<string,string>::const_iterator i=m_map.begin(); i!=m_map.end(); i++)
71         q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
72
73     // Add in the exception content.
74     if (m_exception) {
75         q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
76         if (m_toolingException)
77             q = q + '&' + m_toolingException->toQueryString();
78     }
79
80     q.erase(0,1);
81     return q;
82 }