726fb55ded968c2f90fb3d99f4301d009eae624e
[shibboleth/cpp-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 "SessionCache.h"
29 #include "attribute/Attribute.h"
30 #include "util/PropertySet.h"
31 #include "util/TemplateParameters.h"
32
33 #include <ctime>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/util/URLEncoder.h>
36
37 using namespace shibsp;
38 using namespace xmltooling;
39 using namespace std;
40
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)
43 {
44     setPropertySet(props);
45 }
46
47 TemplateParameters::~TemplateParameters()
48 {
49 }
50
51 void TemplateParameters::setPropertySet(const PropertySet* props)
52 {
53     m_props = props;
54
55     // Create a timestamp.
56     time_t now = time(nullptr);
57 #if defined(HAVE_CTIME_R_2)
58     char timebuf[32];
59     m_map["now"] = ctime_r(&now,timebuf);
60 #elif defined(HAVE_CTIME_R_3)
61     char timebuf[32];
62     m_map["now"] = ctime_r(&now,timebuf,sizeof(timebuf));
63 #else
64     m_map["now"] = ctime(&now);
65 #endif
66 }
67
68 const XMLToolingException* TemplateParameters::getRichException() const
69 {
70     return m_toolingException;
71 }
72
73 const char* TemplateParameters::getParameter(const char* name) const
74 {
75     if (m_exception) {
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();
80     }
81
82     const char* pch = TemplateEngine::TemplateParameters::getParameter(name);
83     if (pch)
84         return pch;
85
86     if (m_session) {
87         const multimap<string,const Attribute*>& attrs = m_session->getIndexedAttributes();
88         pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> walker;
89         for (walker = attrs.equal_range(name); walker.first != walker.second; ++walker.first) {
90             if (walker.first->second->valueCount() > 0)
91                 return walker.first->second->getSerializedValues().front().c_str();
92         }
93     }
94
95     if (m_props) {
96         pair<bool,const char*> p = m_props->getString(name);
97         if (p.first)
98             return p.second;
99     }
100
101     return nullptr;
102 }
103
104 string TemplateParameters::toQueryString() const
105 {
106     // Capture local stuff.
107     string q;
108
109     const URLEncoder* enc = XMLToolingConfig::getConfig().getURLEncoder();
110     for (map<string,string>::const_iterator i = m_map.begin(); i != m_map.end(); ++i)
111         q = q + '&' + i->first + '=' + enc->encode(i->second.c_str());
112
113     // Add in the exception content.
114     if (m_exception) {
115         q = q + "&errorType=" + enc->encode(getParameter("errorType")) + "&errorText=" + enc->encode(getParameter("errorText"));
116         if (m_toolingException)
117             q = q + '&' + m_toolingException->toQueryString();
118     }
119
120     q.erase(0,1);
121     return q;
122 }