Skip to content

Commit 686544f

Browse files
committed
* Cleanup serdes by using existing utils in the ES codebase
Signed-off-by: lloydmeta <[email protected]>
1 parent 04e8c45 commit 686544f

File tree

2 files changed

+5
-44
lines changed

2 files changed

+5
-44
lines changed

x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlInitiateSingleSignOnAttributes.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.io.stream.Writeable;
15+
import org.elasticsearch.common.xcontent.XContentParserUtils;
1516
import org.elasticsearch.xcontent.ToXContentObject;
1617
import org.elasticsearch.xcontent.XContentBuilder;
1718
import org.elasticsearch.xcontent.XContentParser;
1819

1920
import java.io.IOException;
20-
import java.util.ArrayList;
2121
import java.util.Collections;
2222
import java.util.HashMap;
2323
import java.util.List;
@@ -50,57 +50,22 @@ public Map<String, List<String>> getAttributes() {
5050
* @return A new SamlInitiateSingleSignOnAttributes instance
5151
*/
5252
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));
6454
return new SamlInitiateSingleSignOnAttributes(attributes);
6555
}
6656

6757
@Override
6858
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);
7760
}
7861

7962
@Override
8063
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);
9065
}
9166

9267
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);
10469
}
10570

10671
/**

x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlInitiateSingleSignOnAttributesTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public void testEmptyAttributes() throws Exception {
5050

5151
// Test toXContent
5252
XContentBuilder builder = XContentFactory.jsonBuilder();
53-
builder.startObject();
5453
attributes.toXContent(builder, ToXContent.EMPTY_PARAMS);
55-
builder.endObject();
5654
String json = BytesReference.bytes(builder).utf8ToString();
5755

5856
final SamlInitiateSingleSignOnAttributes parsedAttributes = parseFromJson(json);
@@ -88,9 +86,7 @@ public void testWithAttributes() throws Exception {
8886

8987
// Test toXContent
9088
XContentBuilder builder = XContentFactory.jsonBuilder();
91-
builder.startObject();
9289
attributes.toXContent(builder, ToXContent.EMPTY_PARAMS);
93-
builder.endObject();
9490
String json = BytesReference.bytes(builder).utf8ToString();
9591

9692
// Test parsing from JSON

0 commit comments

Comments
 (0)