1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.common.binding;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.opensaml.saml2.metadata.Endpoint;
24 import org.opensaml.saml2.metadata.IndexedEndpoint;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class BasicEndpointSelector extends AbstractEndpointSelector {
44
45
46 private Logger log = LoggerFactory.getLogger(BasicEndpointSelector.class);
47
48
49 @SuppressWarnings("unchecked")
50 public Endpoint selectEndpoint() {
51 if(getEntityRoleMetadata() == null){
52 return null;
53 }
54
55 List<? extends Endpoint> endpoints = getEntityRoleMetadata().getEndpoints(getEndpointType());
56 if (endpoints == null || endpoints.size() == 0) {
57 return null;
58 }
59
60 Endpoint selectedEndpoint;
61 endpoints = filterEndpointsByProtocolBinding(endpoints);
62 if (endpoints == null || endpoints.size() == 0) {
63 return null;
64 }
65 if (endpoints.get(0) instanceof IndexedEndpoint) {
66 selectedEndpoint = selectIndexedEndpoint((List<IndexedEndpoint>) endpoints);
67 } else {
68 selectedEndpoint = selectNonIndexedEndpoint((List<Endpoint>) endpoints);
69 }
70
71 log.debug("Selected endpoint {} for request", selectedEndpoint.getLocation());
72 return selectedEndpoint;
73 }
74
75
76
77
78
79
80
81
82 protected List<? extends Endpoint> filterEndpointsByProtocolBinding(List<? extends Endpoint> endpoints) {
83 List<Endpoint> filteredEndpoints = new ArrayList<Endpoint>(endpoints);
84 Iterator<Endpoint> endpointItr = filteredEndpoints.iterator();
85 Endpoint endpoint;
86 while (endpointItr.hasNext()) {
87 endpoint = endpointItr.next();
88 if (!getSupportedIssuerBindings().contains(endpoint.getBinding())) {
89 endpointItr.remove();
90 continue;
91 }
92 }
93
94 return filteredEndpoints;
95 }
96
97
98
99
100
101
102
103
104 protected Endpoint selectIndexedEndpoint(List<IndexedEndpoint> endpoints) {
105 List<IndexedEndpoint> endpointsCopy = new ArrayList<IndexedEndpoint>(endpoints);
106 Iterator<IndexedEndpoint> endpointItr = endpointsCopy.iterator();
107 IndexedEndpoint firstNoDefaultEndpoint = null;
108 IndexedEndpoint currentEndpoint;
109 while (endpointItr.hasNext()) {
110 currentEndpoint = endpointItr.next();
111
112
113 if (currentEndpoint.isDefault() != null) {
114 if (currentEndpoint.isDefault()) {
115 return currentEndpoint;
116 }
117
118 if (firstNoDefaultEndpoint == null) {
119 firstNoDefaultEndpoint = currentEndpoint;
120 }
121 }
122 }
123
124 if (firstNoDefaultEndpoint != null) {
125
126 return firstNoDefaultEndpoint;
127 } else {
128 if (endpointsCopy.size() > 0) {
129
130 return endpointsCopy.get(0);
131 } else {
132
133 return null;
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145 protected Endpoint selectNonIndexedEndpoint(List<Endpoint> endpoints) {
146 Iterator<Endpoint> endpointItr = endpoints.iterator();
147 Endpoint endpoint;
148 while (endpointItr.hasNext()) {
149 endpoint = endpointItr.next();
150
151
152 return endpoint;
153 }
154
155
156 return null;
157 }
158 }