forked from loafle/openapi-generator-original
[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:
@@ -66,6 +66,31 @@ Then manually install the following JARs:
|
||||
- `target/petstore-jersey2-java8-1.0.0.jar`
|
||||
- `target/lib/*.jar`
|
||||
|
||||
## Usage
|
||||
|
||||
To add a HTTP proxy for the API client, use `ClientConfig`:
|
||||
```java
|
||||
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
||||
import org.glassfish.jersey.client.ClientConfig;
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
import org.openapitools.client.*;
|
||||
import org.openapitools.client.api.AnotherFakeApi;
|
||||
|
||||
...
|
||||
|
||||
ApiClient defaultClient = Configuration.getDefaultApiClient();
|
||||
ClientConfig clientConfig = defaultClient.getClientConfig();
|
||||
clientConfig.connectorProvider(new ApacheConnectorProvider());
|
||||
clientConfig.property(ClientProperties.PROXY_URI, "http://proxy_url_here");
|
||||
clientConfig.property(ClientProperties.PROXY_USERNAME, "proxy_username");
|
||||
clientConfig.property(ClientProperties.PROXY_PASSWORD, "proxy_password");
|
||||
defaultClient.setClientConfig(clientConfig);
|
||||
|
||||
AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient);
|
||||
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
Please follow the [installation](#installation) instruction and execute the following Java code:
|
||||
|
||||
@@ -110,6 +110,7 @@ dependencies {
|
||||
compile "org.glassfish.jersey.inject:jersey-hk2:$jersey_version"
|
||||
compile "org.glassfish.jersey.media:jersey-media-multipart:$jersey_version"
|
||||
compile "org.glassfish.jersey.media:jersey-media-json-jackson:$jersey_version"
|
||||
compile "org.glassfish.jersey.connectors:jersey-apache-connector:$jersey_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
||||
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
|
||||
|
||||
@@ -14,6 +14,7 @@ lazy val root = (project in file(".")).
|
||||
"org.glassfish.jersey.inject" % "jersey-hk2" % "2.27",
|
||||
"org.glassfish.jersey.media" % "jersey-media-multipart" % "2.27",
|
||||
"org.glassfish.jersey.media" % "jersey-media-json-jackson" % "2.27",
|
||||
"org.glassfish.jersey.connectors" % "jersey-apache-connector" % "2.27",
|
||||
"com.fasterxml.jackson.core" % "jackson-core" % "2.10.4" % "compile",
|
||||
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.10.4" % "compile",
|
||||
"com.fasterxml.jackson.core" % "jackson-databind" % "2.10.4" % "compile",
|
||||
|
||||
@@ -286,6 +286,11 @@
|
||||
<version>${javax-annotation-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.connectors</groupId>
|
||||
<artifactId>jersey-apache-connector</artifactId>
|
||||
<version>${jersey-version}</version>
|
||||
</dependency>
|
||||
<!-- test dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user