Skip to content

Commit afb800f

Browse files
committed
Start adding a RequestOptions
1 parent c9f2320 commit afb800f

File tree

3 files changed

+167
-68
lines changed

3 files changed

+167
-68
lines changed

client/rest/src/main/java/org/elasticsearch/client/Request.java

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@
3636
* HTTP Request to Elasticsearch.
3737
*/
3838
public final class Request {
39-
private static final Header[] NO_HEADERS = new Header[0];
4039
private final String method;
4140
private final String endpoint;
4241
private final Map<String, String> parameters = new HashMap<>();
4342

4443
private HttpEntity entity;
45-
private Header[] headers = NO_HEADERS;
46-
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory =
47-
HttpAsyncResponseConsumerFactory.DEFAULT;
4844

4945
/**
5046
* Create the {@linkplain Request}.
@@ -124,45 +120,6 @@ public HttpEntity getEntity() {
124120
return entity;
125121
}
126122

127-
/**
128-
* Set the headers to attach to the request.
129-
*/
130-
public void setHeaders(Header... headers) {
131-
Objects.requireNonNull(headers, "headers cannot be null");
132-
for (Header header : headers) {
133-
Objects.requireNonNull(header, "header cannot be null");
134-
}
135-
this.headers = headers;
136-
}
137-
138-
/**
139-
* Headers to attach to the request.
140-
*/
141-
public Header[] getHeaders() {
142-
return headers;
143-
}
144-
145-
/**
146-
* set the {@link HttpAsyncResponseConsumerFactory} used to create one
147-
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
148-
* response body gets streamed from a non-blocking HTTP connection on the
149-
* client side.
150-
*/
151-
public void setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) {
152-
this.httpAsyncResponseConsumerFactory =
153-
Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null");
154-
}
155-
156-
/**
157-
* The {@link HttpAsyncResponseConsumerFactory} used to create one
158-
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
159-
* response body gets streamed from a non-blocking HTTP connection on the
160-
* client side.
161-
*/
162-
public HttpAsyncResponseConsumerFactory getHttpAsyncResponseConsumerFactory() {
163-
return httpAsyncResponseConsumerFactory;
164-
}
165-
166123
@Override
167124
public String toString() {
168125
StringBuilder b = new StringBuilder();
@@ -175,18 +132,6 @@ public String toString() {
175132
if (entity != null) {
176133
b.append(", entity=").append(entity);
177134
}
178-
if (headers.length > 0) {
179-
b.append(", headers=");
180-
for (int h = 0; h < headers.length; h++) {
181-
if (h != 0) {
182-
b.append(',');
183-
}
184-
b.append(headers[h].toString());
185-
}
186-
}
187-
if (httpAsyncResponseConsumerFactory != HttpAsyncResponseConsumerFactory.DEFAULT) {
188-
b.append(", consumerFactory=").append(httpAsyncResponseConsumerFactory);
189-
}
190135
return b.append('}').toString();
191136
}
192137

@@ -203,13 +148,11 @@ public boolean equals(Object obj) {
203148
return method.equals(other.method)
204149
&& endpoint.equals(other.endpoint)
205150
&& parameters.equals(other.parameters)
206-
&& Objects.equals(entity, other.entity)
207-
&& Arrays.equals(headers, other.headers)
208-
&& httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory);
151+
&& Objects.equals(entity, other.entity);
209152
}
210153

211154
@Override
212155
public int hashCode() {
213-
return Objects.hash(method, endpoint, parameters, entity, Arrays.hashCode(headers), httpAsyncResponseConsumerFactory);
156+
return Objects.hash(method, endpoint, parameters, entity);
214157
}
215158
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client;
21+
22+
import org.apache.http.entity.ContentType;
23+
import org.apache.http.Header;
24+
import org.apache.http.HttpEntity;
25+
import org.apache.http.nio.entity.NStringEntity;
26+
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
27+
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import java.util.Objects;
32+
33+
import static java.util.Collections.unmodifiableMap;
34+
35+
/**
36+
* Portion the configuraiton of an HTTP request to Elasticsearch that
37+
* can be manipulated without changing Elasticsearch's behavior.
38+
*/
39+
public final class RequestOptions {
40+
41+
public static Builder builder() {
42+
Builder builder = new Builder();
43+
builder.setHeaders(NO_HEADERS);
44+
builder.setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory.DEFAULT);
45+
return builder;
46+
}
47+
48+
private static final Header[] NO_HEADERS = new Header[0];
49+
50+
private final Header[] headers;
51+
private final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
52+
53+
private RequestOptions(Builder builder) {
54+
this.headers = builder.headers;
55+
this.httpAsyncResponseConsumerFactory = builder.httpAsyncResponseConsumerFactory;
56+
}
57+
58+
public Builder toBuilder() {
59+
Builder builder = new Builder();
60+
builder.setHeaders(headers);
61+
builder.setHttpAsyncResponseConsumerFactory(httpAsyncResponseConsumerFactory);
62+
return builder;
63+
}
64+
65+
/**
66+
* Headers to attach to the request.
67+
*/
68+
public Header[] getHeaders() {
69+
return headers;
70+
}
71+
72+
/**
73+
* The {@link HttpAsyncResponseConsumerFactory} used to create one
74+
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
75+
* response body gets streamed from a non-blocking HTTP connection on the
76+
* client side.
77+
*/
78+
public HttpAsyncResponseConsumerFactory getHttpAsyncResponseConsumerFactory() {
79+
return httpAsyncResponseConsumerFactory;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
StringBuilder b = new StringBuilder();
85+
b.append("RequestOptions{");
86+
if (headers.length > 0) {
87+
b.append(", headers=");
88+
for (int h = 0; h < headers.length; h++) {
89+
if (h != 0) {
90+
b.append(',');
91+
}
92+
b.append(headers[h].toString());
93+
}
94+
}
95+
if (httpAsyncResponseConsumerFactory != HttpAsyncResponseConsumerFactory.DEFAULT) {
96+
b.append(", consumerFactory=").append(httpAsyncResponseConsumerFactory);
97+
}
98+
return b.append('}').toString();
99+
}
100+
101+
@Override
102+
public boolean equals(Object obj) {
103+
if (obj == null || (obj.getClass() != getClass())) {
104+
return false;
105+
}
106+
if (obj == this) {
107+
return true;
108+
}
109+
110+
RequestOptions other = (RequestOptions) obj;
111+
return Arrays.equals(headers, other.headers)
112+
&& httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return Objects.hash(Arrays.hashCode(headers), httpAsyncResponseConsumerFactory);
118+
}
119+
120+
public static class Builder {
121+
private Header[] headers;
122+
private HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory;
123+
124+
/**
125+
* Build the {@linkplain RequestOptions}.
126+
*/
127+
public RequestOptions builder() {
128+
return new RequestOptions(this);
129+
}
130+
131+
/**
132+
* Set the headers to attach to the request.
133+
*/
134+
public void setHeaders(Header... headers) {
135+
Objects.requireNonNull(headers, "headers cannot be null");
136+
for (Header header : headers) {
137+
Objects.requireNonNull(header, "header cannot be null");
138+
}
139+
this.headers = headers;
140+
}
141+
142+
/**
143+
* set the {@link HttpAsyncResponseConsumerFactory} used to create one
144+
* {@link HttpAsyncResponseConsumer} callback per retry. Controls how the
145+
* response body gets streamed from a non-blocking HTTP connection on the
146+
* client side.
147+
*/
148+
public void setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) {
149+
this.httpAsyncResponseConsumerFactory =
150+
Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null");
151+
}
152+
}
153+
}

client/rest/src/main/java/org/elasticsearch/client/RestClient.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,15 @@ public synchronized void setHosts(HttpHost... hosts) {
163163
* {@link Exception#getCause()}.
164164
*
165165
* @param request the request to perform
166+
* @param options options on the request to perform
166167
* @return the response returned by Elasticsearch
167168
* @throws IOException in case of a problem or the connection was aborted
168169
* @throws ClientProtocolException in case of an http protocol error
169170
* @throws ResponseException in case Elasticsearch responded with a status code that indicated an error
170171
*/
171-
public Response performRequest(Request request) throws IOException {
172+
public Response performRequest(Request request, RequestOptions options) throws IOException {
172173
SyncResponseListener listener = new SyncResponseListener(maxRetryTimeoutMillis);
173-
performRequestAsyncNoCatch(request, listener);
174+
performRequestAsyncNoCatch(request, options, listener);
174175
return listener.get();
175176
}
176177

@@ -187,12 +188,13 @@ public Response performRequest(Request request) throws IOException {
187188
* them does, in which case an {@link IOException} will be thrown.
188189
*
189190
* @param request the request to perform
191+
* @param options options on the request to perform
190192
* @param responseListener the {@link ResponseListener} to notify when the
191193
* request is completed or fails
192194
*/
193-
public void performRequestAsync(Request request, ResponseListener responseListener) {
195+
public void performRequestAsync(Request request, RequestOptions options, ResponseListener responseListener) {
194196
try {
195-
performRequestAsyncNoCatch(request, responseListener);
197+
performRequestAsyncNoCatch(request, options, responseListener);
196198
} catch (Exception e) {
197199
responseListener.onFailure(e);
198200
}
@@ -215,8 +217,9 @@ public void performRequestAsync(Request request, ResponseListener responseListen
215217
@Deprecated
216218
public Response performRequest(String method, String endpoint, Header... headers) throws IOException {
217219
Request request = new Request(method, endpoint);
218-
request.setHeaders(headers);
219-
return performRequest(request);
220+
RequestOptions.Builder options = RequestOptions.builder();
221+
options.setHeaders(headers);
222+
return performRequest(request, options.build());
220223
}
221224

222225
/**
@@ -428,7 +431,7 @@ public void performRequestAsync(String method, String endpoint, Map<String, Stri
428431
performRequestAsync(request, responseListener);
429432
}
430433

431-
void performRequestAsyncNoCatch(Request request, ResponseListener listener) {
434+
void performRequestAsyncNoCatch(Request request, RequestOptions options, ResponseListener listener) {
432435
Map<String, String> requestParams = new HashMap<>(request.getParameters());
433436
//ignore is a special parameter supported by the clients, shouldn't be sent to es
434437
String ignoreString = requestParams.remove("ignore");
@@ -457,11 +460,11 @@ void performRequestAsyncNoCatch(Request request, ResponseListener listener) {
457460
}
458461
URI uri = buildUri(pathPrefix, request.getEndpoint(), requestParams);
459462
HttpRequestBase httpRequest = createHttpRequest(request.getMethod(), uri, request.getEntity());
460-
setHeaders(httpRequest, request.getHeaders());
463+
setHeaders(httpRequest, options.getHeaders());
461464
FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(listener);
462465
long startTime = System.nanoTime();
463466
performRequestAsync(startTime, nextHost(), httpRequest, ignoreErrorCodes,
464-
request.getHttpAsyncResponseConsumerFactory(), failureTrackingResponseListener);
467+
options.getHttpAsyncResponseConsumerFactory(), failureTrackingResponseListener);
465468
}
466469

467470
private void performRequestAsync(final long startTime, final HostTuple<Iterator<HttpHost>> hostTuple, final HttpRequestBase request,

0 commit comments

Comments
 (0)