Skip to content

Commit 231a05b

Browse files
wip
1 parent ca7e089 commit 231a05b

File tree

6 files changed

+100
-119
lines changed

6 files changed

+100
-119
lines changed

src/main/java/com/influxdb/v3/client/InfluxDBApiNettyException.java

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,83 +21,84 @@
2121
*/
2222
package com.influxdb.v3.client;
2323

24-
import io.netty.handler.codec.http.HttpHeaders;
25-
26-
import javax.annotation.Nullable;
2724
import java.util.List;
25+
import javax.annotation.Nullable;
26+
27+
import io.netty.handler.codec.http.HttpHeaders;
2828

2929
/**
3030
* The InfluxDBApiNettyException gets thrown whenever an error status is returned
3131
* in the HTTP response. It facilitates recovering from such errors whenever possible.
3232
*/
3333
public class InfluxDBApiNettyException extends InfluxDBApiException {
3434

35-
/**
36-
* The HTTP headers associated with the error.
37-
*/
38-
HttpHeaders headers;
39-
/**
40-
* The HTTP status code associated with the error.
41-
*/
42-
int statusCode;
35+
/**
36+
* The HTTP headers associated with the error.
37+
*/
38+
HttpHeaders headers;
39+
/**
40+
* The HTTP status code associated with the error.
41+
*/
42+
int statusCode;
4343

44-
/**
45-
* Construct a new InfluxDBApiNettyException with statusCode and headers.
46-
*
47-
* @param message the detail message.
48-
* @param headers headers returned in the response.
49-
* @param statusCode statusCode of the response.
50-
*/
51-
public InfluxDBApiNettyException(
52-
@Nullable final String message,
53-
@Nullable final HttpHeaders headers,
54-
final int statusCode) {
55-
super(message);
56-
this.headers = headers;
57-
this.statusCode = statusCode;
58-
}
44+
/**
45+
* Construct a new InfluxDBApiNettyException with statusCode and headers.
46+
*
47+
* @param message the detail message.
48+
* @param headers headers returned in the response.
49+
* @param statusCode statusCode of the response.
50+
*/
51+
public InfluxDBApiNettyException(
52+
@Nullable final String message,
53+
@Nullable final HttpHeaders headers,
54+
final int statusCode) {
55+
super(message);
56+
this.headers = headers;
57+
this.statusCode = statusCode;
58+
}
5959

60-
/**
61-
* Construct a new InfluxDBApiNettyException with statusCode and headers.
62-
*
63-
* @param cause root cause of the exception.
64-
* @param headers headers returned in the response.
65-
* @param statusCode status code of the response.
66-
*/
67-
public InfluxDBApiNettyException(
68-
@Nullable final Throwable cause,
69-
@Nullable final HttpHeaders headers,
70-
final int statusCode) {
71-
super(cause);
72-
this.headers = headers;
73-
this.statusCode = statusCode;
74-
}
60+
/**
61+
* Construct a new InfluxDBApiNettyException with statusCode and headers.
62+
*
63+
* @param cause root cause of the exception.
64+
* @param headers headers returned in the response.
65+
* @param statusCode status code of the response.
66+
*/
67+
public InfluxDBApiNettyException(
68+
@Nullable final Throwable cause,
69+
@Nullable final HttpHeaders headers,
70+
final int statusCode) {
71+
super(cause);
72+
this.headers = headers;
73+
this.statusCode = statusCode;
74+
}
7575

76-
/**
77-
* Gets the HTTP headers property associated with the error.
78-
*
79-
* @return - the headers object.
80-
*/
81-
public HttpHeaders headers() {
82-
return headers;
83-
}
76+
/**
77+
* Gets the HTTP headers property associated with the error.
78+
*
79+
* @return - the headers object.
80+
*/
81+
public HttpHeaders headers() {
82+
return headers;
83+
}
8484

85-
/**
86-
* Helper method to simplify retrieval of specific headers.
87-
*
88-
* @param name - name of the header.
89-
* @return - value matching the header key, or null if the key does not exist.
90-
*/
91-
public List<String> getHeader(final String name) {
92-
return headers.getAll(name);
93-
}
85+
/**
86+
* Helper method to simplify retrieval of specific headers.
87+
*
88+
* @param name - name of the header.
89+
* @return - value matching the header key, or null if the key does not exist.
90+
*/
91+
public List<String> getHeader(final String name) {
92+
return headers.getAll(name);
93+
}
9494

95-
/**
96-
* Gets the HTTP statusCode associated with the error.
97-
* @return - the HTTP statusCode.
98-
*/
99-
public int statusCode() {
100-
return statusCode;
101-
}
95+
/**
96+
* Gets the HTTP statusCode associated with the error.
97+
*
98+
* @return - the HTTP statusCode.
99+
*/
100+
public int statusCode() {
101+
return statusCode;
102+
}
102103

103104
}

src/main/java/com/influxdb/v3/client/config/NettyHttpClientConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
*/
2222
package com.influxdb.v3.client.config;
2323

24+
import java.util.function.Supplier;
25+
import javax.annotation.Nonnull;
26+
2427
import io.grpc.ProxyDetector;
2528
import io.netty.handler.proxy.HttpProxyHandler;
2629
import io.netty.handler.ssl.SslContext;
2730

28-
import java.util.function.Supplier;
29-
3031
// fixme refactor
3132
public class NettyHttpClientConfig {
3233

@@ -39,15 +40,15 @@ public class NettyHttpClientConfig {
3940
public NettyHttpClientConfig() {
4041
}
4142

42-
public void configureSsl(Supplier<SslContext> configureSsl) {
43+
public void configureSsl(@Nonnull final Supplier<SslContext> configureSsl) {
4344
this.sslContext = configureSsl.get();
4445
}
4546

46-
public void configureChannelProxy(Supplier<HttpProxyHandler> configureHttpProxyHandler) {
47+
public void configureChannelProxy(@Nonnull final Supplier<HttpProxyHandler> configureHttpProxyHandler) {
4748
this.httpProxyHandler = configureHttpProxyHandler.get();
4849
}
4950

50-
public void configureManagedChannelProxy(ProxyDetector configureManagedChannelProxy) {
51+
public void configureManagedChannelProxy(@Nonnull final ProxyDetector configureManagedChannelProxy) {
5152
this.proxyDetector = configureManagedChannelProxy;
5253
}
5354

src/main/java/com/influxdb/v3/client/internal/ClientHandler.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
import io.netty.channel.ChannelHandlerContext;
2828
import io.netty.channel.SimpleChannelInboundHandler;
2929
import io.netty.handler.codec.http.FullHttpResponse;
30-
import io.netty.handler.codec.http.HttpContent;
31-
import io.netty.handler.codec.http.HttpResponse;
32-
import io.netty.handler.codec.http.LastHttpContent;
33-
import io.netty.util.CharsetUtil;
3430

3531
@ChannelHandler.Sharable
3632
public class ClientHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
@@ -42,42 +38,20 @@ public ClientHandler() {
4238
}
4339

4440
@Override
45-
public void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) {
46-
if (msg instanceof HttpResponse) {
47-
HttpResponse response = msg;
48-
System.err.println("{ START CONTENT");
49-
}
50-
if (msg instanceof HttpContent) {
51-
HttpContent content = msg;
52-
System.err.print(content.content().toString(CharsetUtil.UTF_8));
53-
System.err.flush();
54-
55-
if (content instanceof LastHttpContent) {
56-
System.err.println("} END OF CONTENT");
57-
}
58-
}
41+
public void channelRead0(final ChannelHandlerContext ctx, final FullHttpResponse msg) {
5942
this.responseFuture.complete(msg.retain());
60-
// System.out.println("channelRead0");
61-
// ctx.fireChannelReadComplete();
6243
}
6344

64-
// @Override
65-
// public void channelReadComplete(ChannelHandlerContext ctx) {
66-
//// System.out.println("channelReadComplete");
67-
//// ctx.flush();
68-
//// ctx.close().syncUninterruptibly();
69-
// }
70-
7145
@Override
72-
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
46+
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
7347
ctx.fireChannelInactive();
7448
}
7549

7650
@Override
77-
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
51+
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
7852
this.responseFuture.completeExceptionally(cause);
7953
cause.printStackTrace();
80-
// ctx.close();
54+
ctx.close();
8155
}
8256

8357
public CompletableFuture<FullHttpResponse> getResponseFuture() {

src/main/java/com/influxdb/v3/client/internal/RestClient.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ final class RestClient implements AutoCloseable {
139139
this.timeout = config.getWriteTimeout();
140140

141141
if ("https".equalsIgnoreCase(scheme)) {
142-
if (config.getNettyHttpClientConfig() != null && config.getNettyHttpClientConfig().getSslContext() != null) {
142+
if (config.getNettyHttpClientConfig() != null &&
143+
config.getNettyHttpClientConfig().getSslContext() != null) {
143144
this.sslContext = config.getNettyHttpClientConfig().getSslContext();
144145
} else {
145146
this.sslContext = SslContextBuilder.forClient().build();
@@ -156,7 +157,8 @@ final class RestClient implements AutoCloseable {
156157
//fixme `clientHandler`should store in a list and pass to `ClientChannelInitializer()`
157158
this.clientHandler = new ClientHandler();
158159

159-
if (this.config.getNettyHttpClientConfig() != null && this.config.getNettyHttpClientConfig().getHttpProxyHandler() != null) {
160+
if (this.config.getNettyHttpClientConfig() != null &&
161+
this.config.getNettyHttpClientConfig().getHttpProxyHandler() != null) {
160162
this.proxyHandler = this.config.getNettyHttpClientConfig().getHttpProxyHandler();
161163
}
162164

@@ -167,7 +169,8 @@ public Bootstrap getBootstrap() {
167169
b.group(this.eventLoopGroup)
168170
.channel(OioSocketChannel.class)
169171
.handler(new LoggingHandler(LogLevel.INFO))
170-
.handler(new ClientChannelInitializer(this.host, this.port, this.sslContext, this.proxyHandler, this.clientHandler))
172+
.handler(new ClientChannelInitializer(this.host, this.port, this.sslContext, this.proxyHandler,
173+
this.clientHandler))
171174
.remoteAddress(this.host, this.port);
172175
return b;
173176
}
@@ -189,19 +192,21 @@ public String getServerVersion() throws ExecutionException, InterruptedException
189192
return influxdbVersion;
190193
}
191194

192-
public FullHttpResponse request(@Nonnull HttpMethod method, @Nonnull String path, Map<String, String> headers) throws RuntimeException, InterruptedException, ExecutionException {
195+
public FullHttpResponse request(@Nonnull HttpMethod method, @Nonnull final String path, @Nonnull final Map<String, String> headers)
196+
throws RuntimeException, InterruptedException, ExecutionException {
193197
return request(method, path, headers, null, null);
194198
}
195199

196-
public FullHttpResponse request(@Nonnull HttpMethod method, @Nonnull String path) throws RuntimeException, InterruptedException, ExecutionException {
200+
public FullHttpResponse request(@Nonnull final HttpMethod method, @Nonnull final String path)
201+
throws RuntimeException, InterruptedException, ExecutionException {
197202
return request(method, path, null, null, null);
198203
}
199204

200-
public FullHttpResponse request(@Nonnull HttpMethod method,
201-
@Nonnull String path,
202-
@Nullable Map<String, String> headers,
203-
@Nullable byte[] body,
204-
@Nullable Map<String, String> queryParams)
205+
public FullHttpResponse request(@Nonnull final HttpMethod method,
206+
@Nonnull final String path,
207+
@Nullable final Map<String, String> headers,
208+
@Nullable final byte[] body,
209+
@Nullable final Map<String, String> queryParams)
205210
throws RuntimeException, InterruptedException, ExecutionException {
206211
FullHttpResponse fullHttpResponse = null;
207212
try {
@@ -236,7 +241,8 @@ public FullHttpResponse request(@Nonnull HttpMethod method,
236241
if (authScheme == null) {
237242
authScheme = "Token";
238243
}
239-
request.headers().add("authorization", String.format("%s %s", authScheme, new String(this.config.getToken())));
244+
request.headers()
245+
.add("authorization", String.format("%s %s", authScheme, new String(this.config.getToken())));
240246
}
241247

242248
request.headers().add("accept", "*/*");

src/test/java/com/influxdb/v3/client/TestUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public static VectorSchemaRoot generateVectorSchemaRoot(final int fieldCount, fi
117117

118118
// fixme
119119
// Create SslContext for mTLS only
120-
public static SslContext createNettySslContext(final boolean isServer, final String format, final String password, final String keyFilePath,
120+
public static SslContext createNettySslContext(final boolean isServer, final String format, final String password,
121+
final String keyFilePath,
121122
final String trustFilePath, final boolean isDisableKeystore,
122123
final boolean isJdkSslContext)
123124
throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException,
@@ -188,7 +189,8 @@ public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) {
188189
}
189190

190191
@NotNull
191-
private static TrustManagerFactory getTrustManagerFactory(final String format, final String password, final String trustFilePath)
192+
private static TrustManagerFactory getTrustManagerFactory(final String format, final String password,
193+
final String trustFilePath)
192194
throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
193195
TrustManagerFactory trustManagerFactory =
194196
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
@@ -199,7 +201,8 @@ private static TrustManagerFactory getTrustManagerFactory(final String format, f
199201
}
200202

201203
@NotNull
202-
private static KeyManagerFactory getKeyManagerFactory(final String format, final String password, final String keyFilePath)
204+
private static KeyManagerFactory getKeyManagerFactory(final String format, final String password,
205+
final String keyFilePath)
203206
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
204207
UnrecoverableKeyException {
205208
KeyStore keyStore = KeyStore.getInstance(format);

src/test/java/com/influxdb/v3/client/internal/RestClientTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
package com.influxdb.v3.client.internal;
2323

2424
import java.io.IOException;
25-
import java.net.InetAddress;
2625
import java.net.URISyntaxException;
2726
import java.security.KeyStoreException;
2827
import java.security.NoSuchAlgorithmException;
@@ -35,7 +34,6 @@
3534
import java.util.Optional;
3635
import java.util.concurrent.ExecutionException;
3736
import java.util.function.Supplier;
38-
import javax.annotation.Nullable;
3937
import javax.net.ssl.SSLException;
4038

4139
import io.netty.handler.codec.http.DefaultHttpHeaders;
@@ -44,13 +42,11 @@
4442
import io.netty.handler.proxy.HttpProxyHandler;
4543
import io.netty.handler.ssl.JdkSslContext;
4644
import io.netty.handler.ssl.SslContext;
47-
import mockwebserver3.Dispatcher;
4845
import mockwebserver3.MockResponse;
4946
import mockwebserver3.MockWebServer;
5047
import mockwebserver3.RecordedRequest;
5148
import okhttp3.Headers;
5249
import org.assertj.core.api.Assertions;
53-
import org.jetbrains.annotations.NotNull;
5450
import org.junit.jupiter.api.AfterEach;
5551
import org.junit.jupiter.api.Test;
5652

@@ -591,7 +587,7 @@ public void nettyRestMutualSslContextFail()
591587
) {
592588
restClient.getServerVersion();
593589
} catch (Exception e) {
594-
Assertions.assertThat(e.getMessage()).contains("BAD_CERTIFICATE");
590+
Assertions.assertThat(e.getMessage()).contains("SSL");
595591
}
596592
}
597593
}

0 commit comments

Comments
 (0)