[java][jersey2] add proxy support (#7752)

* add client config getter and setter

* update gradle, sbt config

* update client config

* update samples

* add code sample to set proxy
This commit is contained in:
William Cheng
2020-10-20 10:11:01 +08:00
committed by GitHub
parent c1f2b1cad9
commit d6c71ff0fb
27 changed files with 393 additions and 85 deletions

View File

@@ -86,6 +86,7 @@ public class ApiClient extends JavaTimeFormatter {
protected Map<String, Integer> operationServerIndex = new HashMap<String, Integer>();
protected Map<String, Map<String, String>> operationServerVariables = new HashMap<String, Map<String, String>>();
protected boolean debugging = false;
protected ClientConfig clientConfig;
protected int connectionTimeout = 0;
private int readTimeout = 0;
@@ -112,7 +113,7 @@ public class ApiClient extends JavaTimeFormatter {
*/
public ApiClient(Map<String, Authentication> authMap) {
json = new JSON();
httpClient = buildHttpClient(debugging);
httpClient = buildHttpClient();
this.dateFormat = new RFC3339DateFormat();
@@ -477,6 +478,27 @@ public class ApiClient extends JavaTimeFormatter {
return this;
}
/**
* Gets the client config.
* @return Client config
*/
public ClientConfig getClientConfig() {
return clientConfig;
}
/**
* Set the client config.
*
* @param clientConfig Set the client config
* @return API client
*/
public ApiClient setClientConfig(ClientConfig clientConfig) {
this.clientConfig = clientConfig;
// Rebuild HTTP Client according to the new "clientConfig" value.
this.httpClient = buildHttpClient();
return this;
}
/**
* Check that whether debugging is enabled for this API client.
* @return True if debugging is switched on
@@ -494,7 +516,7 @@ public class ApiClient extends JavaTimeFormatter {
public ApiClient setDebugging(boolean debugging) {
this.debugging = debugging;
// Rebuild HTTP Client according to the new "debugging" value.
this.httpClient = buildHttpClient(debugging);
this.httpClient = buildHttpClient();
return this;
}
@@ -1117,11 +1139,26 @@ public class ApiClient extends JavaTimeFormatter {
/**
* Build the Client used to make HTTP requests.
* @param debugging Debug setting
* @return Client
*/
protected Client buildHttpClient(boolean debugging) {
final ClientConfig clientConfig = new ClientConfig();
protected Client buildHttpClient() {
// use the default client config if not yet initialized
if (clientConfig == null) {
clientConfig = getDefaultClientConfig();
}
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
customizeClientBuilder(clientBuilder);
clientBuilder = clientBuilder.withConfig(clientConfig);
return clientBuilder.build();
}
/**
* Get the default client config.
* @return Client config
*/
public ClientConfig getDefaultClientConfig() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MultiPartFeature.class);
clientConfig.register(json);
clientConfig.register(JacksonFeature.class);
@@ -1137,19 +1174,8 @@ public class ApiClient extends JavaTimeFormatter {
// suppress warnings for payloads with DELETE calls:
java.util.logging.Logger.getLogger("org.glassfish.jersey.client").setLevel(java.util.logging.Level.SEVERE);
}
performAdditionalClientConfiguration(clientConfig);
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
customizeClientBuilder(clientBuilder);
clientBuilder = clientBuilder.withConfig(clientConfig);
return clientBuilder.build();
}
/**
* Perform additional configuration of the API client.
* This method can be overriden to customize the API client.
*/
protected void performAdditionalClientConfiguration(ClientConfig clientConfig) {
// No-op extension point
return clientConfig;
}
/**