Set fourth file version digit to signify rebuild.
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / EndpointManager.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file saml/saml2/metadata/EndpointManager.h
23  * 
24  * Templates for processing endpoint information.
25  */
26
27 #ifndef __saml_epmgr_h__
28 #define __saml_epmgr_h__
29
30 #include <saml/base.h>
31
32 #include <vector>
33 #include <xercesc/util/XMLString.hpp>
34
35 namespace opensaml {
36     namespace saml2md {
37         
38         /**
39          * Template for processing unindexed endpoint information.
40          * 
41          * @param _Tx   the endpoint type being managed
42          */
43         template <class _Tx>
44         class EndpointManager
45         {
46         protected:
47             /** Reference to endpoint array. */
48             const typename std::vector<_Tx*>& m_endpoints;
49             
50         public:
51             /**
52              * Constructor.
53              *
54              * @param endpoints array of endpoints to manage
55              */
56             EndpointManager(const typename std::vector<_Tx*>& endpoints) : m_endpoints(endpoints) {
57             }
58             
59             /**
60              * Returns endpoint that supports a particular binding.
61              * 
62              * @param binding   binding to locate
63              * @return a supporting endpoint, favoring the default, or nullptr
64              */
65             const _Tx* getByBinding(const XMLCh* binding) const {
66                 for (typename std::vector<_Tx*>::const_iterator i = m_endpoints.begin(); i!=m_endpoints.end(); ++i) {
67                     if (xercesc::XMLString::equals(binding,(*i)->getBinding()))
68                         return *i;
69                 }
70                 return nullptr;
71             }
72         };
73
74         /**
75          * Template for processing indexed endpoint information.
76          * 
77          * @param _Tx   the endpoint type being managed
78          */
79         template <class _Tx>
80         class IndexedEndpointManager : public EndpointManager<_Tx>
81         {
82             const _Tx* m_default;
83             
84         public:
85             /**
86              * Constructor.
87              *
88              * @param endpoints array of endpoints to manage
89              */
90             IndexedEndpointManager(const typename std::vector<_Tx*>& endpoints) : EndpointManager<_Tx>(endpoints), m_default(nullptr) {
91             }
92             
93             /**
94              * Returns the default endpoint in the set.
95              * 
96              * @return the default endpoint 
97              */
98             const _Tx* getDefault() const {
99                 if (m_default)
100                     return m_default;
101                 for (typename std::vector<_Tx*>::const_iterator i = EndpointManager<_Tx>::m_endpoints.begin(); i!=EndpointManager<_Tx>::m_endpoints.end(); ++i) {
102                     if ((*i)->isDefault())
103                         return m_default=*i;
104                 }
105                 return (EndpointManager<_Tx>::m_endpoints.empty()) ? m_default=nullptr : m_default=EndpointManager<_Tx>::m_endpoints.front();
106             }
107             
108             /**
109              * Returns indexed endpoint.
110              * 
111              * @param index index to locate
112              * @return matching endpoint, or nullptr
113              */
114             const _Tx* getByIndex(unsigned short index) const {
115                 for (typename std::vector<_Tx*>::const_iterator i = EndpointManager<_Tx>::m_endpoints.begin(); i!=EndpointManager<_Tx>::m_endpoints.end(); ++i) {
116                     std::pair<bool,int> comp = (*i)->getIndex();
117                     if (comp.first && index == comp.second)
118                         return *i;
119                 }
120                 return nullptr;
121             }
122             
123             /**
124              * Returns endpoint that supports a particular binding.
125              * 
126              * @param binding   binding to locate
127              * @return a supporting endpoint, favoring the default, or nullptr
128              */
129             const _Tx* getByBinding(const XMLCh* binding) const {
130                 if (getDefault() && xercesc::XMLString::equals(binding,m_default->getBinding()))
131                     return m_default;
132                 return EndpointManager<_Tx>::getByBinding(binding);
133             }
134         };
135     };
136 };
137
138 #endif /* __saml_epmgr_h__ */