commit | author | age
|
513246
|
1 |
package org.keycloak.protocol.cas.utils; |
MP |
2 |
|
|
3 |
import org.keycloak.protocol.cas.representations.CasServiceResponse; |
|
4 |
|
|
5 |
import javax.xml.bind.JAXBElement; |
|
6 |
import javax.xml.bind.annotation.XmlAccessType; |
|
7 |
import javax.xml.bind.annotation.XmlAccessorType; |
|
8 |
import javax.xml.bind.annotation.XmlAnyElement; |
|
9 |
import javax.xml.bind.annotation.XmlSchema; |
|
10 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
11 |
import javax.xml.namespace.QName; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.List; |
|
14 |
import java.util.Map; |
|
15 |
|
|
16 |
/** |
|
17 |
* Transforms the attribute map of the AuthenticationSuccess object (which can contain either simple values or |
|
18 |
* lists) to a flat list of XML nodes, where the key is the node name.<br> |
|
19 |
* Lists output multiple XML nodes with the same name. |
|
20 |
*/ |
|
21 |
public final class AttributesMapAdapter extends XmlAdapter<AttributesMapAdapter.AttributeWrapperType, Map<String, Object>> { |
|
22 |
@Override |
|
23 |
public AttributeWrapperType marshal(Map<String, Object> v) throws Exception { |
|
24 |
return new AttributeWrapperType(v); |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public Map<String, Object> unmarshal(AttributeWrapperType v) throws Exception { |
|
29 |
throw new IllegalStateException("not implemented"); |
|
30 |
} |
|
31 |
|
|
32 |
@XmlAccessorType(XmlAccessType.FIELD) |
|
33 |
static class AttributeWrapperType { |
|
34 |
@XmlAnyElement |
|
35 |
private final List<JAXBElement<String>> elements; |
|
36 |
|
|
37 |
AttributeWrapperType(Map<String, Object> attributes) { |
|
38 |
this.elements = new ArrayList<>(); |
|
39 |
for (Map.Entry<String, Object> entry : attributes.entrySet()) { |
|
40 |
if (entry.getValue() instanceof List) { |
|
41 |
for (Object item : ((List) entry.getValue())) { |
|
42 |
addElement(entry.getKey(), item); |
|
43 |
} |
|
44 |
} else { |
|
45 |
addElement(entry.getKey(), entry.getValue()); |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
private void addElement(String name, Object value) { |
|
51 |
if (value != null) { |
|
52 |
String namespace = CasServiceResponse.class.getPackage().getAnnotation(XmlSchema.class).namespace(); |
|
53 |
elements.add(new JAXBElement<>(new QName(namespace, name), String.class, value.toString())); |
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
} |