1818 */
1919package org .elasticsearch .action .admin .indices .close ;
2020
21+ import org .elasticsearch .ElasticsearchException ;
2122import org .elasticsearch .Version ;
23+ import org .elasticsearch .action .support .DefaultShardOperationFailedException ;
2224import org .elasticsearch .action .support .master .ShardsAcknowledgedResponse ;
25+ import org .elasticsearch .common .Nullable ;
26+ import org .elasticsearch .common .Strings ;
2327import org .elasticsearch .common .io .stream .StreamInput ;
2428import org .elasticsearch .common .io .stream .StreamOutput ;
29+ import org .elasticsearch .common .io .stream .Writeable ;
30+ import org .elasticsearch .common .xcontent .ToXContentFragment ;
31+ import org .elasticsearch .common .xcontent .XContentBuilder ;
32+ import org .elasticsearch .index .Index ;
2533
2634import java .io .IOException ;
35+ import java .util .List ;
36+ import java .util .Objects ;
37+
38+ import static java .util .Collections .emptyList ;
39+ import static java .util .Collections .unmodifiableList ;
2740
2841public class CloseIndexResponse extends ShardsAcknowledgedResponse {
2942
43+ private List <IndexResult > indices ;
44+
3045 CloseIndexResponse () {
3146 }
3247
33- public CloseIndexResponse (final boolean acknowledged , final boolean shardsAcknowledged ) {
48+ public CloseIndexResponse (final boolean acknowledged , final boolean shardsAcknowledged , final List < IndexResult > indices ) {
3449 super (acknowledged , shardsAcknowledged );
50+ this .indices = unmodifiableList (Objects .requireNonNull (indices ));
51+ }
52+
53+ public List <IndexResult > getIndices () {
54+ return indices ;
3555 }
3656
3757 @ Override
@@ -40,6 +60,11 @@ public void readFrom(StreamInput in) throws IOException {
4060 if (in .getVersion ().onOrAfter (Version .V_7_2_0 )) {
4161 readShardsAcknowledged (in );
4262 }
63+ if (in .getVersion ().onOrAfter (Version .V_7_3_0 )) {
64+ indices = unmodifiableList (in .readList (IndexResult ::new ));
65+ } else {
66+ indices = unmodifiableList (emptyList ());
67+ }
4368 }
4469
4570 @ Override
@@ -48,5 +73,225 @@ public void writeTo(StreamOutput out) throws IOException {
4873 if (out .getVersion ().onOrAfter (Version .V_7_2_0 )) {
4974 writeShardsAcknowledged (out );
5075 }
76+ if (out .getVersion ().onOrAfter (Version .V_7_3_0 )) {
77+ out .writeList (indices );
78+ }
79+ }
80+
81+ protected void addCustomFields (final XContentBuilder builder , final Params params ) throws IOException {
82+ super .addCustomFields (builder , params );
83+ builder .startObject ("indices" );
84+ for (IndexResult index : indices ) {
85+ index .toXContent (builder , params );
86+ }
87+ builder .endObject ();
88+ }
89+
90+ @ Override
91+ public String toString () {
92+ return Strings .toString (this );
93+ }
94+
95+ public static class IndexResult implements Writeable , ToXContentFragment {
96+
97+ private final Index index ;
98+ private final @ Nullable Exception exception ;
99+ private final @ Nullable ShardResult [] shards ;
100+
101+ public IndexResult (final Index index ) {
102+ this (index , null , null );
103+ }
104+
105+ public IndexResult (final Index index , final Exception failure ) {
106+ this (index , Objects .requireNonNull (failure ), null );
107+ }
108+
109+ public IndexResult (final Index index , final ShardResult [] shards ) {
110+ this (index , null , Objects .requireNonNull (shards ));
111+ }
112+
113+ private IndexResult (final Index index , @ Nullable final Exception exception , @ Nullable final ShardResult [] shards ) {
114+ this .index = Objects .requireNonNull (index );
115+ this .exception = exception ;
116+ this .shards = shards ;
117+ }
118+
119+ IndexResult (final StreamInput in ) throws IOException {
120+ this .index = new Index (in );
121+ this .exception = in .readException ();
122+ this .shards = in .readOptionalArray (ShardResult ::new , ShardResult []::new );
123+ }
124+
125+ @ Override
126+ public void writeTo (final StreamOutput out ) throws IOException {
127+ index .writeTo (out );
128+ out .writeException (exception );
129+ out .writeOptionalArray (shards );
130+ }
131+
132+ public Index getIndex () {
133+ return index ;
134+ }
135+
136+ public Exception getException () {
137+ return exception ;
138+ }
139+
140+ public ShardResult [] getShards () {
141+ return shards ;
142+ }
143+
144+ public boolean hasFailures () {
145+ if (exception != null ) {
146+ return true ;
147+ }
148+ if (shards != null ) {
149+ for (ShardResult shard : shards ) {
150+ if (shard .hasFailures ()) {
151+ return true ;
152+ }
153+ }
154+ }
155+ return false ;
156+ }
157+
158+ @ Override
159+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
160+ builder .startObject (index .getName ());
161+ {
162+ if (hasFailures ()) {
163+ builder .field ("closed" , false );
164+ if (exception != null ) {
165+ builder .startObject ("exception" );
166+ ElasticsearchException .generateFailureXContent (builder , params , exception , true );
167+ builder .endObject ();
168+ } else {
169+ builder .startObject ("failedShards" );
170+ for (ShardResult shard : shards ) {
171+ if (shard .hasFailures ()) {
172+ shard .toXContent (builder , params );
173+ }
174+ }
175+ builder .endObject ();
176+ }
177+ } else {
178+ builder .field ("closed" , true );
179+ }
180+ }
181+ return builder .endObject ();
182+ }
183+
184+ @ Override
185+ public String toString () {
186+ return Strings .toString (this );
187+ }
188+ }
189+
190+ public static class ShardResult implements Writeable , ToXContentFragment {
191+
192+ private final int id ;
193+ private final ShardResult .Failure [] failures ;
194+
195+ public ShardResult (final int id , final Failure [] failures ) {
196+ this .id = id ;
197+ this .failures = failures ;
198+ }
199+
200+ ShardResult (final StreamInput in ) throws IOException {
201+ this .id = in .readVInt ();
202+ this .failures = in .readOptionalArray (Failure ::readFailure , ShardResult .Failure []::new );
203+ }
204+
205+ @ Override
206+ public void writeTo (final StreamOutput out ) throws IOException {
207+ out .writeVInt (id );
208+ out .writeOptionalArray (failures );
209+ }
210+
211+ public boolean hasFailures () {
212+ return failures != null && failures .length > 0 ;
213+ }
214+
215+ public int getId () {
216+ return id ;
217+ }
218+
219+ public Failure [] getFailures () {
220+ return failures ;
221+ }
222+
223+ @ Override
224+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
225+ builder .startObject (String .valueOf (id ));
226+ {
227+ builder .startArray ("failures" );
228+ if (failures != null ) {
229+ for (Failure failure : failures ) {
230+ builder .startObject ();
231+ failure .toXContent (builder , params );
232+ builder .endObject ();
233+ }
234+ }
235+ builder .endArray ();
236+ }
237+ return builder .endObject ();
238+ }
239+
240+ @ Override
241+ public String toString () {
242+ return Strings .toString (this );
243+ }
244+
245+ public static class Failure extends DefaultShardOperationFailedException implements Writeable {
246+
247+ private @ Nullable String nodeId ;
248+
249+ private Failure () {
250+ }
251+
252+ public Failure (final String index , final int shardId , final Throwable reason ) {
253+ this (index , shardId , reason , null );
254+ }
255+
256+ public Failure (final String index , final int shardId , final Throwable reason , final String nodeId ) {
257+ super (index , shardId , reason );
258+ this .nodeId = nodeId ;
259+ }
260+
261+ public String getNodeId () {
262+ return nodeId ;
263+ }
264+
265+ @ Override
266+ public void readFrom (final StreamInput in ) throws IOException {
267+ super .readFrom (in );
268+ nodeId = in .readOptionalString ();
269+ }
270+
271+ @ Override
272+ public void writeTo (final StreamOutput out ) throws IOException {
273+ super .writeTo (out );
274+ out .writeOptionalString (nodeId );
275+ }
276+
277+ @ Override
278+ public XContentBuilder toXContent (final XContentBuilder builder , final Params params ) throws IOException {
279+ if (nodeId != null ) {
280+ builder .field ("node" , nodeId );
281+ }
282+ return super .toXContent (builder , params );
283+ }
284+
285+ @ Override
286+ public String toString () {
287+ return Strings .toString (this );
288+ }
289+
290+ static Failure readFailure (final StreamInput in ) throws IOException {
291+ final Failure failure = new Failure ();
292+ failure .readFrom (in );
293+ return failure ;
294+ }
295+ }
51296 }
52297}
0 commit comments