|
12 | 12 | import org.elasticsearch.common.io.stream.StreamInput; |
13 | 13 | import org.elasticsearch.common.io.stream.StreamOutput; |
14 | 14 | import org.elasticsearch.common.io.stream.Writeable; |
| 15 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
15 | 16 | import org.elasticsearch.xcontent.ToXContentObject; |
16 | 17 | import org.elasticsearch.xcontent.XContentBuilder; |
17 | 18 | import org.elasticsearch.xcontent.XContentParser; |
18 | 19 |
|
19 | 20 | import java.io.IOException; |
20 | | -import java.util.ArrayList; |
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.HashMap; |
23 | 23 | import java.util.List; |
@@ -50,57 +50,22 @@ public Map<String, List<String>> getAttributes() { |
50 | 50 | * @return A new SamlInitiateSingleSignOnAttributes instance |
51 | 51 | */ |
52 | 52 | public static SamlInitiateSingleSignOnAttributes fromXContent(XContentParser parser) throws IOException { |
53 | | - Map<String, List<String>> attributes = new HashMap<>(); |
54 | | - while (parser.nextToken() != XContentParser.Token.END_OBJECT) { |
55 | | - String key = parser.currentName(); |
56 | | - if (parser.nextToken() == XContentParser.Token.START_ARRAY) { |
57 | | - List<String> values = new ArrayList<>(); |
58 | | - while (parser.nextToken() != XContentParser.Token.END_ARRAY) { |
59 | | - values.add(parser.text()); |
60 | | - } |
61 | | - attributes.put(key, values); |
62 | | - } |
63 | | - } |
| 53 | + final Map<String, List<String>> attributes = parser.map(HashMap::new, p -> XContentParserUtils.parseList(p, XContentParser::text)); |
64 | 54 | return new SamlInitiateSingleSignOnAttributes(attributes); |
65 | 55 | } |
66 | 56 |
|
67 | 57 | @Override |
68 | 58 | public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
69 | | - for (Map.Entry<String, List<String>> entry : attributes.entrySet()) { |
70 | | - builder.startArray(entry.getKey()); |
71 | | - for (String value : entry.getValue()) { |
72 | | - builder.value(value); |
73 | | - } |
74 | | - builder.endArray(); |
75 | | - } |
76 | | - return builder; |
| 59 | + return builder.map(attributes); |
77 | 60 | } |
78 | 61 |
|
79 | 62 | @Override |
80 | 63 | public void writeTo(StreamOutput out) throws IOException { |
81 | | - out.writeVInt(attributes.size()); |
82 | | - for (Map.Entry<String, List<String>> entry : attributes.entrySet()) { |
83 | | - out.writeString(entry.getKey()); |
84 | | - List<String> values = entry.getValue(); |
85 | | - out.writeVInt(values.size()); |
86 | | - for (String value : values) { |
87 | | - out.writeString(value); |
88 | | - } |
89 | | - } |
| 64 | + out.writeMap(attributes, StreamOutput::writeStringCollection); |
90 | 65 | } |
91 | 66 |
|
92 | 67 | public SamlInitiateSingleSignOnAttributes(StreamInput in) throws IOException { |
93 | | - int size = in.readVInt(); |
94 | | - attributes = new HashMap<>(size); |
95 | | - for (int i = 0; i < size; i++) { |
96 | | - String key = in.readString(); |
97 | | - int valuesSize = in.readVInt(); |
98 | | - List<String> values = new ArrayList<>(valuesSize); |
99 | | - for (int j = 0; j < valuesSize; j++) { |
100 | | - values.add(in.readString()); |
101 | | - } |
102 | | - attributes.put(key, values); |
103 | | - } |
| 68 | + this.attributes = in.readImmutableMap(StreamInput::readStringCollectionAsImmutableList); |
104 | 69 | } |
105 | 70 |
|
106 | 71 | /** |
|
0 commit comments