22
33import java .io .Closeable ;
44import java .io .IOException ;
5+ import java .util .ArrayList ;
56import java .util .Collections ;
7+ import java .util .List ;
8+ import java .util .Map ;
9+ import java .util .Objects ;
610
711import org .apache .commons .io .IOUtils ;
812import org .apache .http .HttpEntity ;
1317
1418import com .fasterxml .jackson .databind .ObjectMapper ;
1519
16- public class ESRest implements Closeable {
20+ public class ESRest implements Closeable {
1721
1822 protected static final ObjectMapper mapper = new ObjectMapper ();
1923
20- protected final RestClient client ;
24+ protected List < RestClient > clients ;
2125
22- public ESRest (RestClient client ) {
23- this .client = client ;
26+ public ESRest (List <RestClient > clients ) {
27+ Objects .requireNonNull (clients ,"Clients can't be null" );
28+ if (clients .isEmpty ()) {
29+ throw new IllegalArgumentException ("Client list can't be empty" );
30+ }
31+ this .clients = Collections .synchronizedList (clients );
2432 }
25-
26- public <T > T get (String resource , Class <T > responseClass , boolean assertSuccess ) throws IOException {
27- final Response response = client . performRequest ("GET" , resource , Collections .singletonMap ("pretty" , "true" ));
28- if (assertSuccess ){
33+
34+ public <T > T get (String resource , Class <T > responseClass , boolean assertSuccess ) throws IOException {
35+ final Response response = performRequest ("GET" , resource , Collections .singletonMap ("pretty" , "true" ));
36+ if (assertSuccess ) {
2937 assertSuccess (response );
3038 }
3139 return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
3240 }
3341
34- public <T > T post (String resource , String body , Class <T > responseClass , boolean assertSuccess )
35- throws IOException {
36- final Response response = client .performRequest ("POST" , resource , Collections .singletonMap ("pretty" , "true" ),
42+ public <T > T post (String resource , String body , Class <T > responseClass , boolean assertSuccess ) throws IOException {
43+ final Response response = performRequest ("POST" , resource , Collections .singletonMap ("pretty" , "true" ),
3744 new NStringEntity (body , ContentType .APPLICATION_JSON ));
3845 if (assertSuccess ) {
3946 assertSuccess (response );
4047 }
4148 return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
4249 }
43-
50+
4451 /**
4552 *
4653 * @param resource
@@ -49,40 +56,87 @@ public <T> T post(String resource, String body, Class<T> responseClass, boolean
4956 * @throws IOException
5057 */
5158 public int head (String resource , boolean assertSuccess ) throws IOException {
52- Response response = client . performRequest ("HEAD" , resource , Collections .singletonMap ("pretty" , "true" ));
59+ Response response = performRequest ("HEAD" , resource , Collections .singletonMap ("pretty" , "true" ));
5360 return response .getStatusLine ().getStatusCode ();
5461 }
5562
56- public <T > T put (String resource , String body , Class <T > responseClass , boolean assertSuccess ) throws IOException {
63+ public <T > T put (String resource , String body , Class <T > responseClass , boolean assertSuccess ) throws IOException {
5764 final HttpEntity entity = new NStringEntity (body , ContentType .APPLICATION_JSON );
58- final Response response = client .performRequest ("PUT" , resource , Collections .<String , String >emptyMap (),
59- entity );
65+ final Response response = performRequest ("PUT" , resource , Collections .<String , String >emptyMap (), entity );
6066 if (assertSuccess ) {
6167 assertSuccess (response );
6268 }
6369 return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
6470 }
6571
66- public <T > T delete (String resource ,Class <T > responseClass , boolean assertSuccess ) throws IOException {
67- final Response response = client .performRequest ("DELETE" , resource , Collections .<String , String >emptyMap ());
72+ private Response performRequest (String method , String endpoint , Map <String , String > headers ) throws IOException {
73+ return performRequest (method , endpoint , headers , null );
74+ }
75+
76+ private Response performRequest (String method , String endpoint , Map <String , String > headers , HttpEntity entity )
77+ throws IOException {
78+ IOException savedException = null ;
79+ int clientIndex = 0 ;
80+ for (; clientIndex < clients .size (); clientIndex ++) {
81+ try {
82+ final Response response = clients .get (clientIndex ).performRequest (method , endpoint , headers , entity );
83+ if (clientIndex != 0 ) {
84+ synchronized (clients ) {
85+ // If the client is not at the top of the list, we should move
86+ // it since it is the successful one.
87+ clients = moveToTop (clients , clients .get (clientIndex ));
88+ }
89+ }
90+ return response ;
91+ } catch (IOException e ) {
92+ // TODO: Save all the exceptions and throw them like in TestNG
93+ // soft assert:
94+ // https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/asserts/SoftAssert.java
95+ savedException = e ;
96+ }
97+
98+ }
99+ throw savedException ;
100+ }
101+
102+ private static <T > List <T > moveToTop (List <T > items , T input ) {
103+ int index = items .indexOf (input );
104+ List <T > copy ;
105+ if (index >= 0 ) {
106+ copy = new ArrayList <T >(items .size ());
107+ copy .add (items .get (index ));
108+ copy .addAll (items .subList (0 , index ));
109+ copy .addAll (items .subList (index + 1 , items .size ()));
110+ } else {
111+ return items ;
112+ }
113+ return copy ;
114+ }
115+
116+ public <T > T delete (String resource , Class <T > responseClass , boolean assertSuccess ) throws IOException {
117+ final Response response = performRequest ("DELETE" , resource , Collections .<String , String >emptyMap ());
68118 if (assertSuccess ) {
69119 assertSuccess (response );
70120 }
71121 return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
72122 }
73-
123+
74124 private void assertSuccess (Response response ) throws IOException {
75125 if (response .getStatusLine ().getStatusCode () != 200 ) {
76126 throw new IOException ("Return status is " + response .getStatusLine ().getStatusCode ());
77127 }
78128 }
79-
129+
80130 @ Override
81- public void close () throws IOException {
82- if (client != null ){
83- client .close ();
131+ public void close () throws IOException {
132+ if (null == clients || clients .isEmpty ()) {
133+ return ;
134+ }
135+ for (RestClient client : clients ) {
136+ if (client != null ) {
137+ client .close ();
138+ }
84139 }
85140 }
86141
87-
88142}
0 commit comments