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