Skip to content

Commit

Permalink
server annotations up
Browse files Browse the repository at this point in the history
  • Loading branch information
brig committed Sep 29, 2023
1 parent 81413db commit 895f81d
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 150 deletions.
1 change: 1 addition & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>server</id>
Expand Down
1 change: 0 additions & 1 deletion client2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.6.0</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,11 @@
* =====
*/

import javax.net.ssl.*;
import java.net.http.HttpClient;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class ConcordApiClient extends ApiClient {

private boolean verifyingSsl = true;
private SSLContext sslContext;

public ConcordApiClient(String baseUrl) {
updateBaseUri(baseUrl);
}

public ApiClient setVerifyingSsl(boolean verifyingSsl) {
this.verifyingSsl = verifyingSsl;
applySslSettings();
return this;
}

@Override
protected HttpClient.Builder createDefaultHttpClientBuilder() {
HttpClient.Builder result = super.createDefaultHttpClientBuilder();
if (sslContext != null) {
result.sslContext(sslContext);
}
return result;
}

private void applySslSettings() {
try {
TrustManager[] trustManagers;
if (!verifyingSsl) {
TrustManager trustAll = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() { return null; }
};
SSLContext sslContext = SSLContext.getInstance("TLS");
trustManagers = new TrustManager[]{ trustAll };
System.getProperties().setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString());

sslContext.init(null, trustManagers, new SecureRandom());
}
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
public ConcordApiClient(HttpClient httpClient) {
super(httpClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.walmartlabs.concord.client2;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2023 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.http.HttpClient;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.time.Duration;

public class DefaultApiClientFactory {

private static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(30);

private final HttpClient httpClient;
private final String defaultApiUrl;

public DefaultApiClientFactory(String defaultApiUrl) {
this(defaultApiUrl, null, true);
}

public DefaultApiClientFactory(String defaultApiUrl, Duration connectTimeout) {
this(defaultApiUrl, connectTimeout, true);
}

public DefaultApiClientFactory(String defaultApiUrl, Duration connectTimeout, boolean verifySsl) {
this.defaultApiUrl = defaultApiUrl;

this.httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(connectTimeout != null ? connectTimeout : DEFAULT_CONNECT_TIMEOUT)
.sslContext(sslContext(verifySsl))
.build();
}

public ApiClient create() {
return new ConcordApiClient(httpClient)
.setBaseUrl(defaultApiUrl);
}

public ApiClient create(ApiClientConfiguration overrides) {
String baseUrl = overrides.baseUrl() != null ? overrides.baseUrl() : defaultApiUrl;

String sessionToken = null;
if (overrides.apiKey() == null) {
sessionToken = overrides.sessionToken();
}

String apiKey = overrides.apiKey();
if (apiKey != null) {
sessionToken = null;
}

if (sessionToken == null && apiKey == null) {
throw new IllegalArgumentException("Session token or an API key is required");
}

return new ConcordApiClient(httpClient)
.setBaseUrl(baseUrl)
.setSessionToken(sessionToken)
.setApiKey(apiKey)
.addDefaultHeader("Accept", "*/*");
}

private static SSLContext sslContext(boolean verifySsl) {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
if (verifySsl) {
sslContext.init(null, null, null);
} else {
TrustManager trustAll = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
TrustManager[] trustManagers = new TrustManager[]{trustAll};
System.getProperties().setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString());

sslContext.init(null, trustManagers, new SecureRandom());
}
return sslContext;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public interface ProcessListFilter {
Integer offset();

@Nullable
Map<String, Object> meta();
Map<String, String> meta();

class Builder extends ImmutableProcessListFilter.Builder {

Expand Down
89 changes: 17 additions & 72 deletions client2/src/main/template/libraries/native/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ import static java.nio.charset.StandardCharsets.UTF_8;
{{>generatedAnnotation}}
public class ApiClient {
private HttpClient.Builder builder;
private final HttpClient httpClient;
private ObjectMapper mapper;
private String baseUri;
private String scheme;
private String host;
private int port;
Expand Down Expand Up @@ -176,8 +177,8 @@ public class ApiClient {
/**
* Create an instance of ApiClient.
*/
public ApiClient() {
this.builder = createDefaultHttpClientBuilder();
public ApiClient(HttpClient httpClient) {
this.httpClient = httpClient;
this.mapper = createDefaultObjectMapper();
updateBaseUri(getDefaultBaseUri());
interceptor = null;
Expand All @@ -187,25 +188,7 @@ public class ApiClient {
asyncResponseInterceptor = null;
}

/**
* Create an instance of ApiClient.
*
* @param builder Http client builder.
* @param mapper Object mapper.
* @param baseUri Base URI
*/
public ApiClient(HttpClient.Builder builder, ObjectMapper mapper, String baseUri) {
this.builder = builder;
this.mapper = mapper;
updateBaseUri(baseUri != null ? baseUri : getDefaultBaseUri());
interceptor = null;
readTimeout = null;
connectTimeout = null;
responseInterceptor = null;
asyncResponseInterceptor = null;
}

protected ObjectMapper createDefaultObjectMapper() {
public static ObjectMapper createDefaultObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand Down Expand Up @@ -238,11 +221,6 @@ public class ApiClient {
return "{{{basePath}}}";
}

protected HttpClient.Builder createDefaultHttpClientBuilder() {
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1);
}

public HttpRequest.Builder requestBuilder() {
HttpRequest.Builder result = HttpRequest.newBuilder();
for (Map.Entry<String, String> e : defaultHeaderMap.entrySet()) {
Expand All @@ -255,13 +233,24 @@ public class ApiClient {
}

public void updateBaseUri(String baseUri) {
this.baseUri = baseUri;
URI uri = URI.create(baseUri);
scheme = uri.getScheme();
host = uri.getHost();
port = uri.getPort();
basePath = uri.getRawPath();
}

public ApiClient setBaseUrl(String baseUrl) {
updateBaseUri(baseUrl);
return this;
}

public String getBaseUrl() {
return this.baseUri;
}

public ApiClient setSessionToken(String token) {
if (token == null) {
return this;
Expand All @@ -282,19 +271,6 @@ public class ApiClient {
return this;
}


/**
* Set a custom {@link HttpClient.Builder} object to use when creating the
* {@link HttpClient} that is used by the API client.
*
* @param builder Custom client builder.
* @return This object.
*/
public ApiClient setHttpClientBuilder(HttpClient.Builder builder) {
this.builder = builder;
return this;
}

/**
* Get an {@link HttpClient} based on the current {@link HttpClient.Builder}.
*
Expand All @@ -303,7 +279,7 @@ public class ApiClient {
* @return The HTTP client.
*/
public HttpClient getHttpClient() {
return builder.build();
return httpClient;
}

/**
Expand Down Expand Up @@ -483,35 +459,4 @@ public class ApiClient {
public Duration getReadTimeout() {
return readTimeout;
}
/**
* Sets the connect timeout (in milliseconds) for the http client.
*
* <p> In the case where a new connection needs to be established, if
* the connection cannot be established within the given {@code
* duration}, then {@link HttpClient#send(HttpRequest,BodyHandler)
* HttpClient::send} throws an {@link HttpConnectTimeoutException}, or
* {@link HttpClient#sendAsync(HttpRequest,BodyHandler)
* HttpClient::sendAsync} completes exceptionally with an
* {@code HttpConnectTimeoutException}. If a new connection does not
* need to be established, for example if a connection can be reused
* from a previous request, then this timeout duration has no effect.
*
* @param connectTimeout connection timeout in milliseconds
*
* @return This object.
*/
public ApiClient setConnectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
this.builder.connectTimeout(connectTimeout);
return this;
}

/**
* Get connection timeout (in milliseconds).
*
* @return Timeout in milliseconds
*/
public Duration getConnectTimeout() {
return connectTimeout;
}
}
Loading

0 comments on commit 895f81d

Please sign in to comment.