28
28
import org .elasticsearch .common .xcontent .XContentBuilder ;
29
29
import org .elasticsearch .common .xcontent .XContentParser ;
30
30
import org .elasticsearch .rest .RestStatus ;
31
+ import org .elasticsearch .script .ScriptContextInfo ;
31
32
32
33
import java .io .IOException ;
33
- import java .util .ArrayList ;
34
34
import java .util .Collections ;
35
+ import java .util .Comparator ;
36
+ import java .util .HashMap ;
35
37
import java .util .List ;
36
38
import java .util .Map ;
37
39
import java .util .Objects ;
40
+ import java .util .Set ;
41
+ import java .util .function .Function ;
38
42
import java .util .stream .Collectors ;
39
43
40
- import static org .elasticsearch .common .xcontent .XContentParser .Token .END_OBJECT ;
41
- import static org .elasticsearch .common .xcontent .XContentParser .Token .START_OBJECT ;
42
-
43
44
public class GetScriptContextResponse extends ActionResponse implements StatusToXContentObject {
44
45
45
46
private static final ParseField CONTEXTS = new ParseField ("contexts" );
46
- private final List <String > contextNames ;
47
+ final Map <String , ScriptContextInfo > contexts ;
47
48
48
49
@ SuppressWarnings ("unchecked" )
49
50
public static final ConstructingObjectParser <GetScriptContextResponse ,Void > PARSER =
50
51
new ConstructingObjectParser <>("get_script_context" , true ,
51
52
(a ) -> {
52
- Map <String , Object > contexts = ((List <String >) a [0 ]).stream ().collect ( Collectors . toMap (
53
- name -> name , name -> new Object ( )
54
- )) ;
53
+ Map <String ,ScriptContextInfo > contexts = ((List <ScriptContextInfo >) a [0 ]).stream ().collect (
54
+ Collectors . toMap ( ScriptContextInfo :: getName , c -> c )
55
+ );
55
56
return new GetScriptContextResponse (contexts );
56
57
}
57
58
);
58
59
59
60
static {
60
- PARSER .declareNamedObjects (
61
- ConstructingObjectParser .constructorArg (),
62
- (p , c , n ) ->
63
- {
64
- // advance empty object
65
- assert (p .nextToken () == START_OBJECT );
66
- assert (p .nextToken () == END_OBJECT );
67
- return n ;
68
- },
69
- CONTEXTS
70
- );
61
+ PARSER .declareObjectArray (ConstructingObjectParser .constructorArg (),
62
+ (parser , ctx ) -> ScriptContextInfo .PARSER .apply (parser , ctx ), CONTEXTS );
71
63
}
72
64
73
65
GetScriptContextResponse (StreamInput in ) throws IOException {
74
66
super (in );
75
67
int size = in .readInt ();
76
- ArrayList <String > contextNames = new ArrayList <>(size );
68
+ HashMap <String , ScriptContextInfo > contexts = new HashMap <>(size );
77
69
for (int i = 0 ; i < size ; i ++) {
78
- contextNames .add (in .readString ());
70
+ ScriptContextInfo info = new ScriptContextInfo (in );
71
+ contexts .put (info .name , info );
79
72
}
80
- this .contextNames = Collections .unmodifiableList (contextNames );
73
+ this .contexts = Collections .unmodifiableMap (contexts );
74
+ }
75
+
76
+ // TransportAction constructor
77
+ GetScriptContextResponse (Set <ScriptContextInfo > contexts ) {
78
+ this .contexts = Map .copyOf (contexts .stream ().collect (
79
+ Collectors .toMap (ScriptContextInfo ::getName , Function .identity ())
80
+ ));
81
+ }
82
+
83
+ // Parser constructor
84
+ private GetScriptContextResponse (Map <String ,ScriptContextInfo > contexts ) {
85
+ this .contexts = Map .copyOf (contexts );
81
86
}
82
87
83
- GetScriptContextResponse (Map <String ,Object > contexts ) {
84
- List <String > contextNames = new ArrayList <>(contexts .keySet ());
85
- contextNames .sort (String ::compareTo );
86
- this .contextNames = Collections .unmodifiableList (contextNames );
88
+ private List <ScriptContextInfo > byName () {
89
+ return contexts .values ().stream ().sorted (Comparator .comparing (ScriptContextInfo ::getName )).collect (Collectors .toList ());
87
90
}
88
91
89
92
@ Override
90
93
public void writeTo (StreamOutput out ) throws IOException {
91
- out .writeInt (this . contextNames .size ());
92
- for (String context : this . contextNames ) {
93
- out . writeString ( context );
94
+ out .writeInt (contexts .size ());
95
+ for (ScriptContextInfo context : contexts . values () ) {
96
+ context . writeTo ( out );
94
97
}
95
98
}
96
99
@@ -101,11 +104,11 @@ public RestStatus status() {
101
104
102
105
@ Override
103
106
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
104
- builder .startObject ().startObject (CONTEXTS .getPreferredName ());
105
- for (String contextName : this . contextNames ) {
106
- builder . startObject ( contextName ). endObject ( );
107
+ builder .startObject ().startArray (CONTEXTS .getPreferredName ());
108
+ for (ScriptContextInfo context : byName () ) {
109
+ context . toXContent ( builder , params );
107
110
}
108
- builder .endObject ().endObject (); // CONTEXTS
111
+ builder .endArray ().endObject (); // CONTEXTS
109
112
return builder ;
110
113
}
111
114
@@ -122,11 +125,11 @@ public boolean equals(Object o) {
122
125
return false ;
123
126
}
124
127
GetScriptContextResponse that = (GetScriptContextResponse ) o ;
125
- return contextNames .equals (that .contextNames );
128
+ return contexts .equals (that .contexts );
126
129
}
127
130
128
131
@ Override
129
132
public int hashCode () {
130
- return Objects .hash (contextNames );
133
+ return Objects .hash (contexts );
131
134
}
132
135
}
0 commit comments