Skip to content

Commit de27a94

Browse files
committed
Extract HttpRequest.Node (future-proof this class for later use)
1 parent ba2feca commit de27a94

File tree

7 files changed

+145
-79
lines changed

7 files changed

+145
-79
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,142 @@
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+
120
package co.elastic.clients.transport.http;
221

22+
import javax.annotation.Nullable;
23+
import java.net.URI;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
import java.util.Set;
28+
29+
/**
30+
* A node/host to send requests to.
31+
*/
332
public class Node {
33+
private final URI uri;
34+
private final String name;
35+
private final String version;
36+
private final Set<String> roles;
37+
private final Map<String, String> attributes;
38+
private final List<String> boundHosts;
39+
40+
/**
41+
* Create a node with its URI, roles and attributes.
42+
* <p>
43+
* If the URI doesn't end with a '{@code /}', then one is added.
44+
*
45+
* @param uri the node's URI
46+
* @param name the node name/identifier
47+
* @param version the node's version, if known
48+
* @param roles the node's roles (such as "master", "ingest", etc).
49+
* @param attributes the node's attributes. This can be used for routing decisions by multi-node implementations.
50+
* @param boundHosts addresses on which the node is listening. Useful to find a host based on any address it's listening to.
51+
*/
52+
public Node(
53+
URI uri,
54+
@Nullable String name,
55+
@Nullable String version,
56+
@Nullable Set<String> roles,
57+
@Nullable Map<String, String> attributes,
58+
@Nullable List<String> boundHosts
59+
) {
60+
61+
if (!uri.isAbsolute()) {
62+
throw new IllegalArgumentException("Node URIs must be absolute: " + uri);
63+
}
64+
65+
if (!uri.getRawPath().endsWith("/")) {
66+
uri = uri.resolve(uri.getRawPath() + "/");
67+
}
68+
69+
this.uri = uri;
70+
this.name = name;
71+
this.version = version;
72+
this.roles = roles;
73+
this.attributes = attributes;
74+
this.boundHosts = boundHosts;
75+
}
76+
77+
public Node(URI uri) {
78+
this(uri, null, null, null, null, null);
79+
}
80+
81+
public Node(String uri) {
82+
this(URI.create(uri), null, null, null, null, null);
83+
}
84+
85+
/**
86+
* The URI of this node. This is an absolute URL with a path ending with a "/".
87+
*/
88+
public URI uri() {
89+
return this.uri;
90+
}
91+
92+
/**
93+
* The node name/identifier
94+
*/
95+
@Nullable
96+
public String name() {
97+
return name;
98+
}
99+
100+
@Nullable
101+
public String version() {
102+
return version;
103+
}
104+
105+
@Nullable
106+
public Set<String> roles() {
107+
return roles;
108+
}
109+
110+
@Nullable
111+
public Map<String, String> attributes() {
112+
return attributes;
113+
}
114+
115+
@Nullable
116+
public List<String> boundHosts() {
117+
return boundHosts;
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return uri.toString();
123+
}
124+
125+
/**
126+
* Two nodes are considered equal if their URIs are equal. Other properties are ignored.
127+
*/
128+
@Override
129+
public boolean equals(Object o) {
130+
if (this == o) return true;
131+
if (!(o instanceof Node node)) return false;
132+
return Objects.equals(uri, node.uri);
133+
}
134+
135+
/**
136+
* A node's hash code is that of its URI. Other properties are ignored.
137+
*/
138+
@Override
139+
public int hashCode() {
140+
return Objects.hash(uri);
141+
}
4142
}

java-client/src/main/java/co/elastic/clients/transport/http/RepeatableBodyResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public RepeatableBodyResponse(TransportHttpClient.Response response) throws IOEx
4545
}
4646

4747
@Override
48-
public TransportHttpClient.Node node() {
48+
public Node node() {
4949
return response.node();
5050
}
5151

java-client/src/main/java/co/elastic/clients/transport/http/TransportHttpClient.java

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@
2626
import javax.annotation.Nullable;
2727
import java.io.Closeable;
2828
import java.io.IOException;
29-
import java.net.URI;
3029
import java.nio.ByteBuffer;
31-
import java.util.Collections;
3230
import java.util.List;
3331
import java.util.Map;
34-
import java.util.Objects;
35-
import java.util.Set;
3632
import java.util.concurrent.CompletableFuture;
3733

3834
/**
@@ -90,78 +86,6 @@ default TransportOptions createOptions(@Nullable TransportOptions options) {
9086
*/
9187
void close() throws IOException;
9288

93-
/**
94-
* A node/host to send requests to.
95-
*/
96-
class Node {
97-
private final URI uri;
98-
private final Set<String> roles;
99-
private final Map<String, String> attributes;
100-
101-
/**
102-
* Create a node with its URI, roles and attributes.
103-
* <p>
104-
* If the URI doesn't end with a '{@code /}', then one is added.
105-
*
106-
* @param uri the node's URI
107-
* @param roles the node's roles (such as "master", "ingest", etc). This can be used for routing decisions by multi-node
108-
* implementations.
109-
* @param attributes the node's attributes. This can be used for routing decisions by multi-node implementations.
110-
*/
111-
public Node(URI uri, Set<String> roles, Map<String, String> attributes) {
112-
if (!uri.isAbsolute()) {
113-
throw new IllegalArgumentException("Node URIs must be absolute: " + uri);
114-
}
115-
116-
if (!uri.getRawPath().endsWith("/")) {
117-
uri = uri.resolve(uri.getRawPath() + "/");
118-
}
119-
120-
this.uri = uri;
121-
this.roles = roles;
122-
this.attributes = attributes;
123-
}
124-
125-
public Node(URI uri) {
126-
this(uri, Collections.emptySet(), Collections.emptyMap());
127-
}
128-
129-
public Node(String uri) {
130-
this(URI.create(uri), Collections.emptySet(), Collections.emptyMap());
131-
}
132-
133-
/**
134-
* The URI of this node. This is an absolute URL with a path ending with a "/".
135-
*/
136-
public URI uri() {
137-
return this.uri;
138-
}
139-
140-
@Override
141-
public String toString() {
142-
return uri.toString();
143-
}
144-
145-
/**
146-
* Two nodes are considered equal if their URIs are equal. Roles and attributes are ignored.
147-
*/
148-
@Override
149-
public boolean equals(Object o) {
150-
if (this == o) return true;
151-
if (!(o instanceof Node)) return false;
152-
Node node = (Node) o;
153-
return Objects.equals(uri, node.uri);
154-
}
155-
156-
/**
157-
* A node's hash code is that of its URI. Roles and attributes are ignored.
158-
*/
159-
@Override
160-
public int hashCode() {
161-
return Objects.hash(uri);
162-
}
163-
}
164-
16589
/**
16690
* An http request.
16791
*/

java-client/src/main/java/co/elastic/clients/transport/rest5_client/Rest5ClientHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import co.elastic.clients.transport.TransportOptions;
2323
import co.elastic.clients.transport.http.HeaderMap;
24+
import co.elastic.clients.transport.http.Node;
2425
import co.elastic.clients.transport.http.TransportHttpClient;
2526
import co.elastic.clients.transport.rest5_client.low_level.Cancellable;
2627
import co.elastic.clients.transport.rest5_client.low_level.ResponseListener;

java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import co.elastic.clients.transport.TransportOptions;
2323
import co.elastic.clients.transport.http.HeaderMap;
24+
import co.elastic.clients.transport.http.Node;
2425
import co.elastic.clients.transport.http.TransportHttpClient;
2526
import co.elastic.clients.util.BinaryData;
2627
import co.elastic.clients.util.NoCopyByteArrayOutputStream;

java-client/src/test/java/co/elastic/clients/testkit/MockHttpClient.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import co.elastic.clients.transport.ElasticsearchTransportBase;
2525
import co.elastic.clients.transport.TransportException;
2626
import co.elastic.clients.transport.TransportOptions;
27+
import co.elastic.clients.transport.http.Node;
2728
import co.elastic.clients.transport.http.TransportHttpClient;
2829
import co.elastic.clients.util.BinaryData;
2930
import org.jetbrains.annotations.Nullable;
@@ -69,7 +70,7 @@ public void close() throws IOException {
6970

7071
@Override
7172
public Response performRequest(
72-
String endpointId, @Nullable TransportHttpClient.Node node, Request request, TransportOptions option
73+
String endpointId, @Nullable Node node, Request request, TransportOptions option
7374
) throws IOException {
7475
Response response = responses.get(request.path());
7576

@@ -82,7 +83,7 @@ public Response performRequest(
8283

8384
@Override
8485
public CompletableFuture<Response> performRequestAsync(
85-
String endpointId, @Nullable TransportHttpClient.Node node, Request request, TransportOptions options
86+
String endpointId, @Nullable Node node, Request request, TransportOptions options
8687
) {
8788
CompletableFuture<Response> result = new CompletableFuture<>();
8889
try {

java-client/src/test/java/co/elastic/clients/transport/endpoints/SimpleEndpointTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import co.elastic.clients.transport.ElasticsearchTransport;
2727
import co.elastic.clients.transport.ElasticsearchTransportBase;
2828
import co.elastic.clients.transport.TransportOptions;
29+
import co.elastic.clients.transport.http.Node;
2930
import co.elastic.clients.transport.http.TransportHttpClient;
3031
import org.jetbrains.annotations.Nullable;
3132
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)