0285784a3871aee36a64803e1e2118ce53cff7b3
[shibboleth/cpp-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 log4cpp;
35 using namespace std;
36
37 namespace shibsp {
38
39 #if defined (_MSC_VER)
40     #pragma warning( push )
41     #pragma warning( disable : 4250 )
42 #endif
43
44     class SHIBSP_DLLLOCAL ChainingLogoutInitiator : public AbstractHandler, public LogoutHandler
45     {
46     public:
47         ChainingLogoutInitiator(const DOMElement* e, const char* appId);
48         virtual ~ChainingLogoutInitiator() {
49             for_each(m_handlers.begin(), m_handlers.end(), xmltooling::cleanup<Handler>());
50         }
51         
52         pair<bool,long> run(SPRequest& request, bool isHandler=true) const;
53
54     private:
55         vector<Handler*> m_handlers;
56     };
57
58 #if defined (_MSC_VER)
59     #pragma warning( pop )
60 #endif
61
62     static const XMLCh _LogoutInitiator[] =     UNICODE_LITERAL_15(L,o,g,o,u,t,I,n,i,t,i,a,t,o,r);
63     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
64
65     class SHIBSP_DLLLOCAL LogoutInitiatorNodeFilter : public DOMNodeFilter
66     {
67     public:
68         short acceptNode(const DOMNode* node) const {
69             if (XMLHelper::isNodeNamed(node,shibspconstants::SHIB2SPCONFIG_NS,_LogoutInitiator))
70                 return FILTER_REJECT;
71             return FILTER_ACCEPT;
72         }
73     };
74
75     static SHIBSP_DLLLOCAL LogoutInitiatorNodeFilter g_LINFilter;
76
77     Handler* SHIBSP_DLLLOCAL ChainingLogoutInitiatorFactory(const pair<const DOMElement*,const char*>& p)
78     {
79         return new ChainingLogoutInitiator(p.first, p.second);
80     }
81 };
82
83 ChainingLogoutInitiator::ChainingLogoutInitiator(const DOMElement* e, const char* appId)
84     : AbstractHandler(e, Category::getInstance(SHIBSP_LOGCAT".LogoutInitiator"), &g_LINFilter)
85 {
86     SPConfig& conf = SPConfig::getConfig();
87
88     // Load up the chain of handlers.
89     e = e ? XMLHelper::getFirstChildElement(e, _LogoutInitiator) : NULL;
90     while (e) {
91         auto_ptr_char type(e->getAttributeNS(NULL,_type));
92         if (type.get() && *(type.get())) {
93             try {
94                 m_handlers.push_back(conf.LogoutInitiatorManager.newPlugin(type.get(),make_pair(e, appId)));
95                 m_handlers.back()->setParent(this);
96             }
97             catch (exception& ex) {
98                 m_log.error("caught exception processing embedded SessionInitiator element: %s", ex.what());
99             }
100         }
101         e = XMLHelper::getNextSiblingElement(e, _LogoutInitiator);
102     }
103
104     pair<bool,const char*> loc = getString("Location");
105     if (loc.first) {
106         string address = string(appId) + loc.second + "::run::ChainingLI";
107         setAddress(address.c_str());
108     }
109 }
110
111 pair<bool,long> ChainingLogoutInitiator::run(SPRequest& request, bool isHandler) const
112 {
113     // Defer to base class first.
114     pair<bool,long> ret = LogoutHandler::run(request, isHandler);
115     if (ret.first)
116         return ret;
117
118     for (vector<Handler*>::const_iterator i = m_handlers.begin(); i!=m_handlers.end(); ++i) {
119         ret = (*i)->run(request, isHandler);
120         if (ret.first)
121             return ret;
122     }
123     throw ConfigurationException("None of the configured LogoutInitiators handled the request.");
124 }