2
2
3
3
import java .io .Closeable ;
4
4
import java .io .IOException ;
5
+ import java .util .ArrayList ;
5
6
import java .util .Collections ;
7
+ import java .util .List ;
8
+ import java .util .Map ;
9
+ import java .util .Objects ;
6
10
7
11
import org .apache .commons .io .IOUtils ;
8
12
import org .apache .http .HttpEntity ;
13
17
14
18
import com .fasterxml .jackson .databind .ObjectMapper ;
15
19
16
- public class ESRest implements Closeable {
20
+ public class ESRest implements Closeable {
17
21
18
22
protected static final ObjectMapper mapper = new ObjectMapper ();
19
23
20
- protected final RestClient client ;
24
+ protected List < RestClient > clients ;
21
25
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 );
24
32
}
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 ) {
29
37
assertSuccess (response );
30
38
}
31
39
return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
32
40
}
33
41
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" ),
37
44
new NStringEntity (body , ContentType .APPLICATION_JSON ));
38
45
if (assertSuccess ) {
39
46
assertSuccess (response );
40
47
}
41
48
return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
42
49
}
43
-
50
+
44
51
/**
45
52
*
46
53
* @param resource
@@ -49,40 +56,87 @@ public <T> T post(String resource, String body, Class<T> responseClass, boolean
49
56
* @throws IOException
50
57
*/
51
58
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" ));
53
60
return response .getStatusLine ().getStatusCode ();
54
61
}
55
62
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 {
57
64
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 );
60
66
if (assertSuccess ) {
61
67
assertSuccess (response );
62
68
}
63
69
return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
64
70
}
65
71
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 ());
68
118
if (assertSuccess ) {
69
119
assertSuccess (response );
70
120
}
71
121
return mapper .readValue (IOUtils .toString (response .getEntity ().getContent (), "UTF-8" ), responseClass );
72
122
}
73
-
123
+
74
124
private void assertSuccess (Response response ) throws IOException {
75
125
if (response .getStatusLine ().getStatusCode () != 200 ) {
76
126
throw new IOException ("Return status is " + response .getStatusLine ().getStatusCode ());
77
127
}
78
128
}
79
-
129
+
80
130
@ 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
+ }
84
139
}
85
140
}
86
141
87
-
88
142
}
0 commit comments