From d6c71ff0fb7c1fc3b9ec014186f41fc02623492c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 20 Oct 2020 10:11:01 +0800 Subject: [PATCH] [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 --- .../codegen/languages/JavaClientCodegen.java | 1 + .../src/main/resources/Java/README.mustache | 27 +++++++++ .../Java/libraries/jersey2/ApiClient.mustache | 60 +++++++++++++------ .../libraries/jersey2/build.gradle.mustache | 1 + .../Java/libraries/jersey2/build.sbt.mustache | 1 + .../Java/libraries/jersey2/pom.mustache | 5 ++ .../petstore/java/jersey2-java8/README.md | 25 ++++++++ .../petstore/java/jersey2-java8/build.gradle | 1 + .../petstore/java/jersey2-java8/build.sbt | 1 + .../petstore/java/jersey2-java8/pom.xml | 5 ++ .../org/openapitools/client/ApiClient.java | 60 +++++++++++++------ .../java/jersey2-java8/README.md | 25 ++++++++ .../java/jersey2-java8/build.gradle | 1 + .../java/jersey2-java8/build.sbt | 1 + .../java/jersey2-java8/pom.xml | 5 ++ .../org/openapitools/client/ApiClient.java | 60 +++++++++++++------ .../README.md | 25 ++++++++ .../build.gradle | 1 + .../build.sbt | 1 + .../jersey2-java8-special-characters/pom.xml | 5 ++ .../org/openapitools/client/ApiClient.java | 60 +++++++++++++------ .../petstore/java/jersey2-java8/README.md | 25 ++++++++ .../petstore/java/jersey2-java8/build.gradle | 1 + .../petstore/java/jersey2-java8/build.sbt | 1 + .../petstore/java/jersey2-java8/pom.xml | 5 ++ .../org/openapitools/client/ApiClient.java | 60 +++++++++++++------ .../openapitools/client/ApiClientTest.java | 15 +++++ 27 files changed, 393 insertions(+), 85 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index c60fcdd2acb..452936d3992 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -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)) { diff --git a/modules/openapi-generator/src/main/resources/Java/README.mustache b/modules/openapi-generator/src/main/resources/Java/README.mustache index 5f2e5943a49..1ff5731a035 100644 --- a/modules/openapi-generator/src/main/resources/Java/README.mustache +++ b/modules/openapi-generator/src/main/resources/Java/README.mustache @@ -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: diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 4ccf4e9cb09..713332e20b7 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -156,6 +156,7 @@ public class ApiClient{{#java8}} extends JavaTimeFormatter{{/java8}} { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); 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 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; } /** diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.gradle.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.gradle.mustache index bcbfba97447..392c3fda0b2 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.gradle.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.gradle.mustache @@ -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" diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.sbt.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.sbt.mustache index d4f17f702c2..42afc6016ba 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.sbt.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/build.sbt.mustache @@ -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", diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/pom.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/pom.mustache index 907c3317664..293356545b0 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/pom.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/pom.mustache @@ -381,6 +381,11 @@ ${javax-annotation-version} provided + + org.glassfish.jersey.connectors + jersey-apache-connector + ${jersey-version} + junit diff --git a/samples/client/petstore/java/jersey2-java8/README.md b/samples/client/petstore/java/jersey2-java8/README.md index 634f09c3160..72ffb62b3e4 100644 --- a/samples/client/petstore/java/jersey2-java8/README.md +++ b/samples/client/petstore/java/jersey2-java8/README.md @@ -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: diff --git a/samples/client/petstore/java/jersey2-java8/build.gradle b/samples/client/petstore/java/jersey2-java8/build.gradle index ec11318c716..e325d1da307 100644 --- a/samples/client/petstore/java/jersey2-java8/build.gradle +++ b/samples/client/petstore/java/jersey2-java8/build.gradle @@ -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" diff --git a/samples/client/petstore/java/jersey2-java8/build.sbt b/samples/client/petstore/java/jersey2-java8/build.sbt index efed61e880c..b34b5aa8dcb 100644 --- a/samples/client/petstore/java/jersey2-java8/build.sbt +++ b/samples/client/petstore/java/jersey2-java8/build.sbt @@ -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", diff --git a/samples/client/petstore/java/jersey2-java8/pom.xml b/samples/client/petstore/java/jersey2-java8/pom.xml index 632c8a0e099..a06892af61b 100644 --- a/samples/client/petstore/java/jersey2-java8/pom.xml +++ b/samples/client/petstore/java/jersey2-java8/pom.xml @@ -286,6 +286,11 @@ ${javax-annotation-version} provided + + org.glassfish.jersey.connectors + jersey-apache-connector + ${jersey-version} + junit diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 8f8b62e84be..d8325401312 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -86,6 +86,7 @@ public class ApiClient extends JavaTimeFormatter { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); 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 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; } /** diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/README.md b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/README.md index 7bf2775d7a8..4b9a815e8ae 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/README.md +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/README.md @@ -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: diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.gradle b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.gradle index cac9f8ac7e3..95c8daff26a 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.gradle +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.gradle @@ -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" diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.sbt b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.sbt index 3fb5a17cca4..b5c8100c0fe 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.sbt +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/build.sbt @@ -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", diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/pom.xml b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/pom.xml index d3e257263f3..b3740ad166c 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/pom.xml +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/pom.xml @@ -287,6 +287,11 @@ ${javax-annotation-version} provided + + org.glassfish.jersey.connectors + jersey-apache-connector + ${jersey-version} + junit diff --git a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index f6ecb14f80e..3c90595a871 100644 --- a/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/extensions/x-auth-id-alias/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -121,6 +121,7 @@ public class ApiClient { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); 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 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; } /** diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/README.md b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/README.md index d0c38901254..d7b835f508a 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/README.md +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/README.md @@ -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: diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.gradle b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.gradle index 495c56baf8c..39195e95962 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.gradle +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.gradle @@ -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" diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.sbt b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.sbt index 2cb1327aa62..4af2231a9fa 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.sbt +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/build.sbt @@ -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", diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/pom.xml b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/pom.xml index 42d78d6b2e5..7acd8bda43b 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/pom.xml +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/pom.xml @@ -281,6 +281,11 @@ ${javax-annotation-version} provided + + org.glassfish.jersey.connectors + jersey-apache-connector + ${jersey-version} + junit diff --git a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java index ce0383039f6..012b865ec4d 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8-special-characters/src/main/java/org/openapitools/client/ApiClient.java @@ -84,6 +84,7 @@ public class ApiClient extends JavaTimeFormatter { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); 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 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; } /** diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/README.md b/samples/openapi3/client/petstore/java/jersey2-java8/README.md index cb77330f025..fdca66069f2 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/README.md +++ b/samples/openapi3/client/petstore/java/jersey2-java8/README.md @@ -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: diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/build.gradle b/samples/openapi3/client/petstore/java/jersey2-java8/build.gradle index ff1bf742469..76350694251 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/build.gradle +++ b/samples/openapi3/client/petstore/java/jersey2-java8/build.gradle @@ -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" diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/build.sbt b/samples/openapi3/client/petstore/java/jersey2-java8/build.sbt index 1efbc6ab954..c2bb7d5d4d6 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/build.sbt +++ b/samples/openapi3/client/petstore/java/jersey2-java8/build.sbt @@ -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", diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/pom.xml b/samples/openapi3/client/petstore/java/jersey2-java8/pom.xml index a6b9e3bbd20..f7a03eb6b19 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/pom.xml +++ b/samples/openapi3/client/petstore/java/jersey2-java8/pom.xml @@ -291,6 +291,11 @@ ${javax-annotation-version} provided + + org.glassfish.jersey.connectors + jersey-apache-connector + ${jersey-version} + junit diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index f52640fad1c..d6a95552497 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -156,6 +156,7 @@ public class ApiClient extends JavaTimeFormatter { protected Map operationServerIndex = new HashMap(); protected Map> operationServerVariables = new HashMap>(); 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 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; } /** diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java index 55d3103597c..9239b5386b2 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/test/java/org/openapitools/client/ApiClientTest.java @@ -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 headerParams = new HashMap();