48eee56e6a1f94116c2009ed70aa9134e88ad89a
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / EndpointManager.h
1 /*
2  *  Copyright 2001-2009 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  * @file saml/saml2/metadata/EndpointManager.h
19  * 
20  * Templates for processing endpoint information.
21  */
22
23 #ifndef __saml_epmgr_h__
24 #define __saml_epmgr_h__
25
26 #include <saml/base.h>
27
28 #include <vector>
29 #include <xercesc/util/XMLString.hpp>
30
31 namespace opensaml {
32     namespace saml2md {
33         
34         /**
35          * Template for processing unindexed endpoint information.
36          * 
37          * @param _Tx   the endpoint type being managed
38          */
39         template <class _Tx>
40         class EndpointManager
41         {
42         protected:
43             /** Reference to endpoint array. */
44             const typename std::vector<_Tx*>& m_endpoints;
45             
46         public:
47             /**
48              * Constructor.
49              *
50              * @param endpoints array of endpoints to manage
51              */
52             EndpointManager(const typename std::vector<_Tx*>& endpoints) : m_endpoints(endpoints) {
53             }
54             
55             /**
56              * Returns endpoint that supports a particular binding.
57              * 
58              * @param binding   binding to locate
59              * @return a supporting endpoint, favoring the default, or NULL
60              */
61             const _Tx* getByBinding(const XMLCh* binding) const {
62                 for (typename std::vector<_Tx*>::const_iterator i = m_endpoints.begin(); i!=m_endpoints.end(); ++i) {
63                     if (xercesc::XMLString::equals(binding,(*i)->getBinding()))
64                         return *i;
65                 }
66                 return NULL;
67             }
68         };
69
70         /**
71          * Template for processing indexed endpoint information.
72          * 
73          * @param _Tx   the endpoint type being managed
74          */
75         template <class _Tx>
76         class IndexedEndpointManager : public EndpointManager<_Tx>
77         {
78             const _Tx* m_default;
79             
80         public:
81             /**
82              * Constructor.
83              *
84              * @param endpoints array of endpoints to manage
85              */
86             IndexedEndpointManager(const typename std::vector<_Tx*>& endpoints) : EndpointManager<_Tx>(endpoints), m_default(NULL) {
87             }
88             
89             /**
90              * Returns the default endpoint in the set.
91              * 
92              * @return the default endpoint 
93              */
94             const _Tx* getDefault() const {
95                 if (m_default)
96                     return m_default;
97                 for (typename std::vector<_Tx*>::const_iterator i = EndpointManager<_Tx>::m_endpoints.begin(); i!=EndpointManager<_Tx>::m_endpoints.end(); ++i) {
98                     if ((*i)->isDefault())
99                         return m_default=*i;
100                 }
101                 return (EndpointManager<_Tx>::m_endpoints.empty()) ? m_default=NULL : m_default=EndpointManager<_Tx>::m_endpoints.front();
102             }
103             
104             /**
105              * Returns indexed endpoint.
106              * 
107              * @param index index to locate
108              * @return matching endpoint, or NULL
109              */
110             const _Tx* getByIndex(unsigned short index) const {
111                 for (typename std::vector<_Tx*>::const_iterator i = EndpointManager<_Tx>::m_endpoints.begin(); i!=EndpointManager<_Tx>::m_endpoints.end(); ++i) {
112                     std::pair<bool,int> comp = (*i)->getIndex();
113                     if (comp.first && index == comp.second)
114                         return *i;
115                 }
116                 return NULL;
117             }
118             
119             /**
120              * Returns endpoint that supports a particular binding.
121              * 
122              * @param binding   binding to locate
123              * @return a supporting endpoint, favoring the default, or NULL
124              */
125             const _Tx* getByBinding(const XMLCh* binding) const {
126                 if (getDefault() && xercesc::XMLString::equals(binding,m_default->getBinding()))
127                     return m_default;
128                 return EndpointManager<_Tx>::getByBinding(binding);
129             }
130         };
131     };
132 };
133
134 #endif /* __saml_epmgr_h__ */