Skip to content

Commit 4c3a13b

Browse files
authored
Refactor RestClientTransport to allow using other http client libraries (#584)
1 parent 38ec037 commit 4c3a13b

26 files changed

+2435
-464
lines changed

Diff for: .github/workflows/checkstyle.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
java-version: [ 11 ]
9+
java-version: [ 17 ]
1010
steps:
1111
- uses: actions/checkout@v2
1212

Diff for: example-transports/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory contains experimental implementations of the `TransportHttpClient` interface. They are to be used as examples and inspiration and should not be considered production-ready.

Diff for: example-transports/build.gradle.kts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
plugins {
21+
java
22+
`java-library`
23+
`java-test-fixtures`
24+
}
25+
26+
tasks.withType<Test> {
27+
useJUnitPlatform()
28+
}
29+
30+
java {
31+
targetCompatibility = JavaVersion.VERSION_17
32+
}
33+
34+
35+
dependencies {
36+
val jacksonVersion = "2.13.3"
37+
38+
api("io.netty", "netty-codec-http", "4.1.93.Final")
39+
40+
implementation(project(":java-client"))
41+
42+
// Apache 2.0
43+
// https://github.com/FasterXML/jackson
44+
testImplementation("com.fasterxml.jackson.core", "jackson-core", jacksonVersion)
45+
testImplementation("com.fasterxml.jackson.core", "jackson-databind", jacksonVersion)
46+
47+
// EPL-2.0
48+
// https://junit.org/junit5/
49+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
50+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.0")
51+
52+
}
53+
repositories {
54+
mavenCentral()
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.netty;
21+
22+
import co.elastic.clients.util.BinaryData;
23+
import co.elastic.clients.util.NoCopyByteArrayOutputStream;
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
import java.nio.ByteBuffer;
29+
30+
public class InputStreamBinaryData implements BinaryData {
31+
32+
private final String contentType;
33+
private final InputStream inputStream;
34+
private boolean consumed = false;
35+
36+
public InputStreamBinaryData(String contentType, InputStream inputStream) {
37+
this.contentType = contentType;
38+
this.inputStream = inputStream;
39+
}
40+
41+
@Override
42+
public String contentType() {
43+
return contentType;
44+
}
45+
46+
@Override
47+
public void writeTo(OutputStream out) throws IOException {
48+
consume();
49+
try {
50+
byte[] buffer = new byte[8192];
51+
int len;
52+
while ((len = inputStream.read(buffer)) > 0) {
53+
out.write(buffer, 0, len);
54+
}
55+
} finally {
56+
inputStream.close();
57+
}
58+
}
59+
60+
@Override
61+
public ByteBuffer asByteBuffer() throws IOException {
62+
consume();
63+
NoCopyByteArrayOutputStream baos = new NoCopyByteArrayOutputStream();
64+
writeTo(baos);
65+
return baos.asByteBuffer();
66+
}
67+
68+
@Override
69+
public InputStream asInputStream() throws IOException {
70+
consume();
71+
return inputStream;
72+
}
73+
74+
@Override
75+
public boolean isRepeatable() {
76+
return false;
77+
}
78+
79+
@Override
80+
public long size() {
81+
return -1;
82+
}
83+
84+
private void consume() throws IllegalStateException {
85+
if (consumed) {
86+
throw new IllegalStateException("Data has already been consumed");
87+
}
88+
consumed = true;
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.netty;
21+
22+
import co.elastic.clients.json.JsonpMapper;
23+
import co.elastic.clients.transport.ElasticsearchTransportBase;
24+
import co.elastic.clients.transport.TransportOptions;
25+
import co.elastic.clients.transport.http.TransportHttpClient;
26+
27+
import javax.annotation.Nullable;
28+
import java.io.IOException;
29+
import java.util.concurrent.CompletableFuture;
30+
31+
public class NettyElasticsearchTransport extends ElasticsearchTransportBase {
32+
33+
public NettyElasticsearchTransport(TransportHttpClient.Node node, TransportOptions options, JsonpMapper jsonpMapper) {
34+
super(new SingleNodeHttpClient(new NettyTransportHttpClient(), node), options, jsonpMapper);
35+
}
36+
37+
public static class SingleNodeHttpClient implements TransportHttpClient {
38+
private final TransportHttpClient client;
39+
private final Node node;
40+
41+
public SingleNodeHttpClient(TransportHttpClient client, Node node) {
42+
this.client = client;
43+
this.node = node;
44+
}
45+
46+
@Override
47+
public TransportOptions createOptions(@Nullable TransportOptions options) {
48+
return client.createOptions(options);
49+
}
50+
51+
@Override
52+
public Response performRequest(
53+
String endpointId, @Nullable Node ignoredNode, Request request, TransportOptions options
54+
) throws IOException {
55+
return client.performRequest(endpointId, node, request, options);
56+
}
57+
58+
@Override
59+
public CompletableFuture<Response> performRequestAsync(
60+
String endpointId, @Nullable Node ignoredNode, Request request, TransportOptions options
61+
) {
62+
return client.performRequestAsync(endpointId, node, request, options);
63+
}
64+
65+
@Override
66+
public void close() throws IOException {
67+
client.close();
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)