This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServiceProviderFeedParser.java
78 lines (66 loc) · 2.64 KB
/
ServiceProviderFeedParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package eduproxy.saml;
import org.springframework.core.io.Resource;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
public class ServiceProviderFeedParser {
private final Resource resource;
public ServiceProviderFeedParser(Resource resource) {
this.resource = resource;
}
public Map<String, ServiceProvider> parse() throws IOException, XMLStreamException {
//despite it's name, the XMLInputFactoryImpl is not thread safe
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(resource.getInputStream());
Map<String, ServiceProvider> serviceProviders = new HashMap<>();
String entityId = null, signingCertificate = null;
boolean isServiceProvider = false, isSigning = false;
List<String> assertionConsumerServiceURLs = null;
while (reader.hasNext()) {
switch (reader.next()) {
case START_ELEMENT:
switch (reader.getLocalName()) {
case "EntityDescriptor":
entityId = reader.getAttributeValue(null, "entityID");
break;
case "SPSSODescriptor":
isServiceProvider = true;
break;
case "KeyDescriptor":
isSigning = "signing".equals(reader.getAttributeValue(null, "use"));
break;
case "X509Certificate":
if (isServiceProvider && isSigning) {
signingCertificate = reader.getElementText().replaceAll("\\s", "");
}
break;
case "AssertionConsumerService":
if (assertionConsumerServiceURLs == null) {
assertionConsumerServiceURLs = new ArrayList<>();
}
assertionConsumerServiceURLs.add(reader.getAttributeValue(null, "Location"));
break;
}
break;
case END_ELEMENT:
if (reader.getLocalName().equals("EntityDescriptor") && isServiceProvider) {
serviceProviders.put(entityId, new ServiceProvider(entityId, signingCertificate, assertionConsumerServiceURLs));
entityId = null;
signingCertificate = null;
isServiceProvider = false;
isSigning = false;
assertionConsumerServiceURLs = null;
}
break;
}
}
return serviceProviders;
}
}