VS10 solution files, convert from NULL macro to nullptr.
[shibboleth/sp.git] / shibsp / handler / impl / ChainingSessionInitiator.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  * ChainingSessionInitiator.cpp
19  * 
20  * Chains together multiple SessionInitiator handlers in sequence.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "handler/AbstractHandler.h"
26 #include "handler/SessionInitiator.h"
27 #include "util/SPConstants.h"
28
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xmltooling/util/XMLHelper.h>
31
32 using namespace shibsp;
33 using namespace xmltooling;
34 using namespace std;
35
36 namespace shibsp {
37
38 #if defined (_MSC_VER)
39     #pragma warning( push )
40     #pragma warning( disable : 4250 )
41 #endif
42
43     class SHIBSP_DLLLOCAL ChainingSessionInitiator : public SessionInitiator, public AbstractHandler
44     {
45     public:
46         ChainingSessionInitiator(const DOMElement* e, const char* appId);
47         virtual ~ChainingSessionInitiator() {
48             for_each(m_handlers.begin(), m_handlers.end(), xmltooling::cleanup<SessionInitiator>());
49         }
50         
51         pair<bool,long> run(SPRequest& request, string& entityID, bool isHandler=true) const;
52
53 #ifndef SHIBSP_LITE
54         void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const {
55             for (vector<SessionInitiator*>::const_iterator i = m_handlers.begin(); i!=m_handlers.end(); ++i)
56                 (*i)->generateMetadata(role, handlerURL);
57         }
58 #endif
59
60     private:
61         vector<SessionInitiator*> m_handlers;
62     };
63
64 #if defined (_MSC_VER)
65     #pragma warning( pop )
66 #endif
67
68     static const XMLCh _SessionInitiator[] =    UNICODE_LITERAL_16(S,e,s,s,i,o,n,I,n,i,t,i,a,t,o,r);
69     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
70
71     class SHIBSP_DLLLOCAL SessionInitiatorNodeFilter : public DOMNodeFilter
72     {
73     public:
74 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
75         short
76 #else
77         FilterAction
78 #endif
79         acceptNode(const DOMNode* node) const {
80             if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,_SessionInitiator))
81                 return FILTER_REJECT;
82             return FILTER_ACCEPT;
83         }
84     };
85
86     static SHIBSP_DLLLOCAL SessionInitiatorNodeFilter g_SINFilter;
87
88     SessionInitiator* SHIBSP_DLLLOCAL ChainingSessionInitiatorFactory(const pair<const DOMElement*,const char*>& p)
89     {
90         return new ChainingSessionInitiator(p.first, p.second);
91     }
92 };
93
94 ChainingSessionInitiator::ChainingSessionInitiator(const DOMElement* e, const char* appId)
95     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".SessionInitiator.Chaining"), &g_SINFilter)
96 {
97     SPConfig& conf = SPConfig::getConfig();
98
99     // Load up the chain of handlers.
100     e = e ? XMLHelper::getFirstChildElement(e, _SessionInitiator) : nullptr;
101     while (e) {
102         auto_ptr_char type(e->getAttributeNS(nullptr,_type));
103         if (type.get() && *(type.get())) {
104             try {
105                 m_handlers.push_back(conf.SessionInitiatorManager.newPlugin(type.get(),make_pair(e, appId)));
106                 m_handlers.back()->setParent(this);
107             }
108             catch (exception& ex) {
109                 m_log.error("caught exception processing embedded SessionInitiator element: %s", ex.what());
110             }
111         }
112         e = XMLHelper::getNextSiblingElement(e, _SessionInitiator);
113     }
114
115     m_supportedOptions.insert("isPassive");
116 }
117
118 pair<bool,long> ChainingSessionInitiator::run(SPRequest& request, string& entityID, bool isHandler) const
119 {
120     if (!checkCompatibility(request, isHandler))
121         return make_pair(false,0L);
122
123     pair<bool,long> ret;
124     for (vector<SessionInitiator*>::const_iterator i = m_handlers.begin(); i!=m_handlers.end(); ++i) {
125         ret = (*i)->run(request, entityID, isHandler);
126         if (ret.first)
127             return ret;
128     }
129     throw ConfigurationException("None of the configured SessionInitiators handled the request.");
130 }