Skip to content

Commit b7ac606

Browse files
authored
Backport transport refactor (#769)
* backport of transport package * manual fixes (utiltest not working) * removed ssl test * make tests work * transport version * Revert "make tests work" This reverts commit 53d79bd. * removed examples * avoid asterisk import
1 parent 275273c commit b7ac606

40 files changed

+3030
-529
lines changed

java-client/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ publishing {
177177
dependencies {
178178
// Compile and test with the last 7.x version to make sure transition scenarios where
179179
// the Java API client coexists with a 7.x HLRC work fine
180-
val elasticsearchVersion = "7.17.7"
180+
val elasticsearchVersion = "7.17.18"
181181
val jacksonVersion = "2.17.0"
182182

183183
// Apache 2.0

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ElasticsearchException.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package co.elastic.clients.elasticsearch._types;
2121

22+
import co.elastic.clients.transport.http.TransportHttpClient;
23+
2224
import javax.annotation.Nullable;
2325

2426
/**
@@ -33,11 +35,19 @@ public class ElasticsearchException extends RuntimeException {
3335

3436
private final ErrorResponse response;
3537
private final String endpointId;
38+
@Nullable
39+
private final TransportHttpClient.Response httpResponse;
3640

37-
public ElasticsearchException(String endpointId, ErrorResponse response) {
41+
public ElasticsearchException(String endpointId, ErrorResponse response,
42+
@Nullable TransportHttpClient.Response httpResponse) {
3843
super("[" + endpointId + "] failed: [" + response.error().type() + "] " + response.error().reason());
3944
this.response = response;
4045
this.endpointId = endpointId;
46+
this.httpResponse = httpResponse;
47+
}
48+
49+
public ElasticsearchException(String endpointId, ErrorResponse response) {
50+
this(endpointId, response, null);
4151
}
4252

4353
/**
@@ -68,4 +78,12 @@ public ErrorCause error() {
6878
public int status() {
6979
return this.response.status();
7080
}
81+
82+
/**
83+
* The underlying http response, if available.
84+
*/
85+
@Nullable
86+
public TransportHttpClient.Response httpResponse() {
87+
return this.httpResponse;
88+
}
7189
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. 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 B.V. 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 co.elastic.clients.transport;
21+
22+
import co.elastic.clients.transport.http.HeaderMap;
23+
import co.elastic.clients.util.ObjectBuilderBase;
24+
25+
import javax.annotation.Nullable;
26+
import java.util.Collection;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.function.Function;
32+
33+
/**
34+
* Default implementation of {@link TransportOptions}. Extensions can use it as a base class to provide additional features.
35+
*/
36+
public class DefaultTransportOptions implements TransportOptions {
37+
private final HeaderMap headers;
38+
private final Map<String, String> parameters;
39+
private final Function<List<String>, Boolean> onWarnings;
40+
41+
public static final DefaultTransportOptions EMPTY = new DefaultTransportOptions();
42+
43+
public DefaultTransportOptions() {
44+
this(new HeaderMap(), Collections.emptyMap(), null);
45+
}
46+
47+
public DefaultTransportOptions(
48+
@Nullable HeaderMap headers,
49+
@Nullable Map<String, String> parameters,
50+
@Nullable Function<List<String>, Boolean> onWarnings
51+
) {
52+
this.headers = headers == null ? HeaderMap.EMPTY : headers;
53+
this.parameters = (parameters == null || parameters.isEmpty()) ?
54+
Collections.emptyMap() : Collections.unmodifiableMap(parameters);
55+
this.onWarnings = onWarnings;
56+
}
57+
58+
protected DefaultTransportOptions(AbstractBuilder<?> builder) {
59+
this(builder.headers, builder.parameters, builder.onWarnings);
60+
}
61+
62+
public static DefaultTransportOptions of(@Nullable TransportOptions options) {
63+
if (options == null) {
64+
return new DefaultTransportOptions(null, null, null);
65+
}
66+
if (options instanceof DefaultTransportOptions) {
67+
return (DefaultTransportOptions) options;
68+
}
69+
return new DefaultTransportOptions(
70+
new HeaderMap(entriesToMap(options.headers())),
71+
options.queryParameters(),
72+
options.onWarnings()
73+
);
74+
}
75+
76+
@Override
77+
public Collection<Map.Entry<String, String>> headers() {
78+
return Collections.unmodifiableSet(headers.entrySet());
79+
}
80+
81+
@Override
82+
public Map<String, String> queryParameters() {
83+
return parameters;
84+
}
85+
86+
@Override
87+
public Function<List<String>, Boolean> onWarnings() {
88+
return onWarnings;
89+
}
90+
91+
@Override
92+
public Builder toBuilder() {
93+
return new Builder(this);
94+
}
95+
96+
private static <K, V> Map<K, V> entriesToMap(Collection<Map.Entry<K, V>> entries) {
97+
if (entries.isEmpty()) {
98+
return Collections.emptyMap();
99+
} else {
100+
HashMap<K, V> map = new HashMap<>();
101+
for (Map.Entry<K, V> entry: entries) {
102+
map.put(entry.getKey(), entry.getValue());
103+
}
104+
return map;
105+
}
106+
}
107+
108+
public abstract static class AbstractBuilder<BuilderT extends AbstractBuilder<BuilderT>>
109+
extends ObjectBuilderBase implements TransportOptions.Builder {
110+
111+
private HeaderMap headers;
112+
private Map<String, String> parameters;
113+
private Function<List<String>, Boolean> onWarnings;
114+
115+
public AbstractBuilder() {
116+
}
117+
118+
public AbstractBuilder(DefaultTransportOptions options) {
119+
this.headers = new HeaderMap(options.headers);
120+
this.parameters = copyOrNull(options.parameters);
121+
this.onWarnings = options.onWarnings;
122+
}
123+
124+
protected abstract BuilderT self();
125+
126+
@Override
127+
public BuilderT addHeader(String name, String value) {
128+
if (name.equalsIgnoreCase(HeaderMap.CLIENT_META)) {
129+
// Not overridable
130+
return self();
131+
}
132+
if (this.headers == null) {
133+
this.headers = new HeaderMap();
134+
}
135+
headers.add(name, value);
136+
return self();
137+
}
138+
139+
@Override
140+
public BuilderT setHeader(String name, String value) {
141+
if (name.equalsIgnoreCase(HeaderMap.CLIENT_META)) {
142+
// Not overridable
143+
return self();
144+
}
145+
if (this.headers == null) {
146+
this.headers = new HeaderMap();
147+
}
148+
headers.put(name, value);
149+
return self();
150+
}
151+
152+
@Override
153+
public BuilderT removeHeader(String name) {
154+
if (this.headers != null) {
155+
headers.remove(name);
156+
}
157+
return self();
158+
}
159+
160+
@Override
161+
public BuilderT setParameter(String name, String value) {
162+
if (parameters == null) {
163+
parameters = new HashMap<>();
164+
}
165+
parameters.put(name, value);
166+
return self();
167+
}
168+
169+
@Override
170+
public BuilderT removeParameter(String name) {
171+
if (parameters != null) {
172+
parameters.remove(name);
173+
};
174+
return self();
175+
}
176+
177+
@Override
178+
public BuilderT onWarnings(Function<List<String>, Boolean> listener) {
179+
this.onWarnings = listener;
180+
return self();
181+
}
182+
183+
private <K, V> Map<K, V> copyOrNull(Map<K, V> map) {
184+
return map.isEmpty() ? null : new HashMap<>(map);
185+
}
186+
}
187+
188+
public static class Builder extends AbstractBuilder<Builder> {
189+
190+
public Builder() {
191+
super();
192+
}
193+
194+
public Builder(DefaultTransportOptions options) {
195+
super(options);
196+
}
197+
198+
@Override
199+
protected Builder self() {
200+
return this;
201+
}
202+
203+
@Override
204+
public TransportOptions build() {
205+
_checkSingleUse();
206+
return new DefaultTransportOptions(this);
207+
}
208+
}
209+
}

0 commit comments

Comments
 (0)