e8de4d6346c3ab8b494a5a659b10d8ac1217cdb8
[shibboleth/sp.git] / shibsp / handler / impl / ChainingLogoutInitiator.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  * ChainingLogoutInitiator.cpp
19  * 
20  * Chains together multiple LogoutInitiator handlers in sequence.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "handler/AbstractHandler.h"
26 #include "handler/LogoutHandler.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 ChainingLogoutInitiator : public AbstractHandler, public LogoutHandler
44     {
45     public:
46         ChainingLogoutInitiator(const DOMElement* e, const char* appId);
47         virtual ~ChainingLogoutInitiator() {
48             for_each(m_handlers.begin(), m_handlers.end(), xmltooling::cleanup<Handler>());
49         }
50         
51         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
52
53 #ifndef SHIBSP_LITE
54         const char* getType() const {
55             return "LogoutInitiator";
56         }
57
58         void generateMetadata(opensaml::saml2md::SPSSODescriptor& role, const char* handlerURL) const {
59             for (vector<Handler*>::const_iterator i = m_handlers.begin(); i!=m_handlers.end(); ++i)
60                 (*i)->generateMetadata(role, handlerURL);
61         }
62 #endif
63
64     private:
65         vector<Handler*> m_handlers;
66     };
67
68 #if defined (_MSC_VER)
69     #pragma warning( pop )
70 #endif
71
72     static const XMLCh _LogoutInitiator[] =     UNICODE_LITERAL_15(L,o,g,o,u,t,I,n,i,t,i,a,t,o,r);
73     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
74
75     class SHIBSP_DLLLOCAL LogoutInitiatorNodeFilter : public DOMNodeFilter
76     {
77     public:
78         short acceptNode(const DOMNode* node) const {
79             if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,_LogoutInitiator))
80                 return FILTER_REJECT;
81             return FILTER_ACCEPT;
82         }
83     };
84
85     static SHIBSP_DLLLOCAL LogoutInitiatorNodeFilter g_LINFilter;
86
87     Handler* SHIBSP_DLLLOCAL ChainingLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
88     {
89         return new ChainingLogoutInitiator(p.first, p.second);
90     }
91 };
92
93 ChainingLogoutInitiator::ChainingLogoutInitiator(const DOMElement* e, const char* appId)
94     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator.Chaining"), &g_LINFilter)
95 {
96     SPConfig& conf = SPConfig::getConfig();
97
98     // Load up the chain of handlers.
99     e = e ? XMLHelper::getFirstChildElement(e, _LogoutInitiator) : NULL;
100     while (e) {
101         auto_ptr_char type(e->getAttributeNS(NULL,_type));
102         if (type.get() && *(type.get())) {
103             try {
104                 m_handlers.push_back(conf.LogoutInitiatorManager.newPlugin(type.get(),make_pair(e, appId)));
105                 m_handlers.back()->setParent(this);
106             }
107             catch (exception& ex) {
108                 m_log.error("caught exception processing embedded LogoutInitiator element: %s", ex.what());
109             }
110         }
111         e = XMLHelper::getNextSiblingElement(e, _LogoutInitiator);
112     }
113 }
114
115 pair<bool,long> ChainingLogoutInitiator::run(SPRequest& request, bool isHandler) const
116 {
117     // Defer to base class first.
118     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
119     if (ret.first)
120         return ret;
121
122     for (vector<Handler*>::const_iterator i = m_handlers.begin(); i!=m_handlers.end(); ++i) {
123         ret = (*i)->run(request, isHandler);
124         if (ret.first)
125             return ret;
126     }
127     throw ConfigurationException("None of the configured LogoutInitiators handled the request.");
128 }