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:
parent
c1f2b1cad9
commit
d6c71ff0fb
@ -399,6 +399,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
|
||||
}
|
||||
} else if (JERSEY2.equals(getLibrary())) {
|
||||
additionalProperties.put("jersey2", true);
|
||||
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
|
||||
supportingFiles.add(new SupportingFile("ApiResponse.mustache", invokerFolder, "ApiResponse.java"));
|
||||
if (ProcessUtils.hasHttpSignatureMethods(openAPI)) {
|
||||
|
@ -73,6 +73,33 @@ Then manually install the following JARs:
|
||||
- `target/{{{artifactId}}}-{{{artifactVersion}}}.jar`
|
||||
- `target/lib/*.jar`
|
||||
|
||||
{{#jersey2}}
|
||||
## Usage
|
||||
|
||||
To add a HTTP proxy for the API client, use `ClientConfig`:
|
||||
```java
|
||||
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
|
||||
import org.glassfish.jersey.client.ClientConfig;
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
import {{{invokerPackage}}}.*;
|
||||
import {{{package}}}.{{{classname}}};
|
||||
|
||||
...
|
||||
|
||||
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);
|
||||
|
||||
{{{classname}}} apiInstance = new {{{classname}}}(defaultClient);
|
||||
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
|
||||
```
|
||||
|
||||
{{/jersey2}}
|
||||
## Getting Started
|
||||
|
||||
Please follow the [installation](#installation) instruction and execute the following Java code:
|
||||
|
@ -156,6 +156,7 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
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{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
*/
|
||||
public ApiClient(Map<String, Authentication> authMap) {
|
||||
json = new JSON();
|
||||
httpClient = buildHttpClient(debugging);
|
||||
httpClient = buildHttpClient();
|
||||
|
||||
this.dateFormat = new RFC3339DateFormat();
|
||||
|
||||
@ -562,6 +563,27 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
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
|
||||
@ -579,7 +601,7 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -1210,11 +1232,26 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
@ -1235,19 +1272,8 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} {
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,6 +151,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",{{/supportJava6}}
|
||||
"org.glassfish.jersey.media" % "jersey-media-multipart" % {{#supportJava6}}"2.6"{{/supportJava6}}{{^supportJava6}}"2.27"{{/supportJava6}},
|
||||
"org.glassfish.jersey.media" % "jersey-media-json-jackson" % {{#supportJava6}}"2.6"{{/supportJava6}}{{^supportJava6}}"2.27"{{/supportJava6}},
|
||||
"org.glassfish.jersey.connectors" % "jersey-apache-connector" % {{#supportJava6}}"2.6"{{/supportJava6}}{{^supportJava6}}"2.27"{{/supportJava6}},
|
||||
"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",
|
||||
|
@ -381,6 +381,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>
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,6 +66,31 @@ Then manually install the following JARs:
|
||||
- `target/openapi3-extensions-x-auth-id-alias-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.UsageApi;
|
||||
|
||||
...
|
||||
|
||||
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);
|
||||
|
||||
UsageApi apiInstance = new UsageApi(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",
|
||||
|
@ -287,6 +287,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>
|
||||
|
@ -121,6 +121,7 @@ public class ApiClient {
|
||||
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;
|
||||
|
||||
@ -147,7 +148,7 @@ public class ApiClient {
|
||||
*/
|
||||
public ApiClient(Map<String, Authentication> authMap) {
|
||||
json = new JSON();
|
||||
httpClient = buildHttpClient(debugging);
|
||||
httpClient = buildHttpClient();
|
||||
|
||||
this.dateFormat = new RFC3339DateFormat();
|
||||
|
||||
@ -412,6 +413,27 @@ public class ApiClient {
|
||||
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
|
||||
@ -429,7 +451,7 @@ public class ApiClient {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -1034,11 +1056,26 @@ public class ApiClient {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
@ -1054,19 +1091,8 @@ public class ApiClient {
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,6 +66,31 @@ Then manually install the following JARs:
|
||||
- `target/petstore-openapi3-jersey2-java8-special-characters-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.DefaultApi;
|
||||
|
||||
...
|
||||
|
||||
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);
|
||||
|
||||
DefaultApi apiInstance = new DefaultApi(defaultClient);
|
||||
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
Please follow the [installation](#installation) instruction and execute the following Java code:
|
||||
|
@ -109,6 +109,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",
|
||||
|
@ -281,6 +281,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>
|
||||
|
@ -84,6 +84,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;
|
||||
|
||||
@ -110,7 +111,7 @@ public class ApiClient extends JavaTimeFormatter {
|
||||
*/
|
||||
public ApiClient(Map<String, Authentication> authMap) {
|
||||
json = new JSON();
|
||||
httpClient = buildHttpClient(debugging);
|
||||
httpClient = buildHttpClient();
|
||||
|
||||
this.dateFormat = new RFC3339DateFormat();
|
||||
|
||||
@ -358,6 +359,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
|
||||
@ -375,7 +397,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;
|
||||
}
|
||||
|
||||
@ -982,11 +1004,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);
|
||||
@ -1002,19 +1039,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,6 +66,31 @@ Then manually install the following JARs:
|
||||
- `target/petstore-openapi3-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:
|
||||
|
@ -111,6 +111,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",
|
||||
|
@ -291,6 +291,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>
|
||||
|
@ -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>();
|
||||
|
Loading…
x
Reference in New Issue
Block a user