mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-13 08:31:19 +00:00
[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:
@@ -156,6 +156,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;
|
||||
|
||||
@@ -182,7 +183,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
*/
|
||||
public ApiClient(Map<String, Authentication> authMap) {
|
||||
json = new JSON();
|
||||
httpClient = buildHttpClient(debugging);
|
||||
httpClient = buildHttpClient();
|
||||
|
||||
this.dateFormat = new RFC3339DateFormat();
|
||||
|
||||
@@ -561,6 +562,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
|
||||
@@ -578,7 +600,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;
|
||||
}
|
||||
|
||||
@@ -1201,11 +1223,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);
|
||||
@@ -1221,19 +1258,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,10 @@ import java.security.spec.AlgorithmParameterSpec;
|
||||
import java.util.*;
|
||||
import java.net.URI;
|
||||
import org.junit.*;
|
||||
import org.glassfish.jersey.client.ClientConfig;
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
||||
import org.tomitribe.auth.signatures.Algorithm;
|
||||
import org.tomitribe.auth.signatures.Signer;
|
||||
import org.tomitribe.auth.signatures.SigningAlgorithm;
|
||||
@@ -44,6 +48,17 @@ public class ApiClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientConfig() {
|
||||
ApiClient testClient = new ApiClient();
|
||||
ClientConfig config = testClient.getDefaultClientConfig();
|
||||
config.connectorProvider(new ApacheConnectorProvider());
|
||||
config.property(ClientProperties.PROXY_URI, "http://localhost:8080");
|
||||
config.property(ClientProperties.PROXY_USERNAME,"proxy_user");
|
||||
config.property(ClientProperties.PROXY_PASSWORD,"proxy_password");
|
||||
testClient.setClientConfig(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateParamsForAuth() throws Exception {
|
||||
Map<String, String> headerParams = new HashMap<String, String>();
|
||||
|
||||
Reference in New Issue
Block a user