From 9657d4437e25600a80b17077a6a6a425879f8257 Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 8 Jan 2016 17:31:09 +0800 Subject: [PATCH 1/2] Java client: get/set connect timeout, fix "debugging" --- .../main/resources/Java/ApiClient.mustache | 58 +++++++++++------ .../java/io/swagger/client/ApiClient.java | 62 ++++++++++++------- 2 files changed, 80 insertions(+), 40 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index ef31058d561..e5f64a8c908 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -43,10 +43,12 @@ import {{invokerPackage}}.auth.OAuth; {{>generatedAnnotation}} public class ApiClient { - private Map hostMap = new HashMap(); private Map defaultHeaderMap = new HashMap(); - private boolean debugging = false; private String basePath = "{{basePath}}"; + private boolean debugging = false; + private int connectionTimeout = 0; + + private Client httpClient; private ObjectMapper mapper; private Map authentications; @@ -57,6 +59,8 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { + httpClient = buildHttpClient(debugging); + mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -64,7 +68,7 @@ public class ApiClient { mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); - + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); @@ -224,9 +228,29 @@ public class ApiClient { */ public ApiClient setDebugging(boolean debugging) { this.debugging = debugging; + // Rebuild HTTP Client according to the new "debugging" value. + this.httpClient = buildHttpClient(debugging); return this; } + /** + * Connect timeout (in milliseconds). + */ + public int getConnectTimeout() { + return connectionTimeout; + } + + /** + * Set the connect timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + */ + public ApiClient setConnectTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + httpClient.setConnectTimeout(connectionTimeout); + return this; + } + /** * Get the date format used to parse/format date parameters. */ @@ -436,8 +460,6 @@ public class ApiClient { updateParamsForAuth(authNames, queryParams, headerParams); - Client client = getClient(); - StringBuilder b = new StringBuilder(); b.append("?"); if (queryParams != null){ @@ -455,9 +477,9 @@ public class ApiClient { Builder builder; if (accept == null) - builder = client.resource(basePath + path + querystring).getRequestBuilder(); + builder = httpClient.resource(basePath + path + querystring).getRequestBuilder(); else - builder = client.resource(basePath + path + querystring).accept(accept); + builder = httpClient.resource(basePath + path + querystring).accept(accept); for (String key : headerParams.keySet()) { builder = builder.header(key, headerParams.get(key)); @@ -571,19 +593,17 @@ public class ApiClient { } /** - * Get an existing client or create a new client to handle HTTP request. + * Build the Client used to make HTTP requests. */ - private Client getClient() { - if(!hostMap.containsKey(basePath)) { - // Add the JSON serialization support to Jersey - JacksonJsonProvider jsonProvider = new JacksonJsonProvider(mapper); - DefaultClientConfig conf = new DefaultClientConfig(); - conf.getSingletons().add(jsonProvider); - Client client = Client.create(conf); - if (debugging) - client.addFilter(new LoggingFilter()); - hostMap.put(basePath, client); + private Client buildHttpClient(boolean debugging) { + // Add the JSON serialization support to Jersey + JacksonJsonProvider jsonProvider = new JacksonJsonProvider(mapper); + DefaultClientConfig conf = new DefaultClientConfig(); + conf.getSingletons().add(jsonProvider); + Client client = Client.create(conf); + if (debugging) { + client.addFilter(new LoggingFilter()); } - return hostMap.get(basePath); + return client; } } diff --git a/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java index 057dcd7d72d..daa584094bb 100644 --- a/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java @@ -41,12 +41,14 @@ import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.OAuth; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:16.440+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-08T17:06:02.039+08:00") public class ApiClient { - private Map hostMap = new HashMap(); private Map defaultHeaderMap = new HashMap(); - private boolean debugging = false; private String basePath = "http://petstore.swagger.io/v2"; + private boolean debugging = false; + private int connectionTimeout = 0; + + private Client httpClient; private ObjectMapper mapper; private Map authentications; @@ -57,6 +59,8 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { + httpClient = buildHttpClient(debugging); + mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -64,7 +68,7 @@ public class ApiClient { mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); - + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); @@ -79,8 +83,8 @@ public class ApiClient { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); - authentications.put("api_key", new ApiKeyAuth("header", "api_key")); authentications.put("petstore_auth", new OAuth()); + authentications.put("api_key", new ApiKeyAuth("header", "api_key")); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); } @@ -223,9 +227,29 @@ public class ApiClient { */ public ApiClient setDebugging(boolean debugging) { this.debugging = debugging; + // Rebuild HTTP Client according to the new "debugging" value. + this.httpClient = buildHttpClient(debugging); return this; } + /** + * Connect timeout (in milliseconds). + */ + public int getConnectTimeout() { + return connectionTimeout; + } + + /** + * Set the connect timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + */ + public ApiClient setConnectTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + httpClient.setConnectTimeout(connectionTimeout); + return this; + } + /** * Get the date format used to parse/format date parameters. */ @@ -435,8 +459,6 @@ public class ApiClient { updateParamsForAuth(authNames, queryParams, headerParams); - Client client = getClient(); - StringBuilder b = new StringBuilder(); b.append("?"); if (queryParams != null){ @@ -454,9 +476,9 @@ public class ApiClient { Builder builder; if (accept == null) - builder = client.resource(basePath + path + querystring).getRequestBuilder(); + builder = httpClient.resource(basePath + path + querystring).getRequestBuilder(); else - builder = client.resource(basePath + path + querystring).accept(accept); + builder = httpClient.resource(basePath + path + querystring).accept(accept); for (String key : headerParams.keySet()) { builder = builder.header(key, headerParams.get(key)); @@ -570,19 +592,17 @@ public class ApiClient { } /** - * Get an existing client or create a new client to handle HTTP request. + * Build the Client used to make HTTP requests. */ - private Client getClient() { - if(!hostMap.containsKey(basePath)) { - // Add the JSON serialization support to Jersey - JacksonJsonProvider jsonProvider = new JacksonJsonProvider(mapper); - DefaultClientConfig conf = new DefaultClientConfig(); - conf.getSingletons().add(jsonProvider); - Client client = Client.create(conf); - if (debugging) - client.addFilter(new LoggingFilter()); - hostMap.put(basePath, client); + private Client buildHttpClient(boolean debugging) { + // Add the JSON serialization support to Jersey + JacksonJsonProvider jsonProvider = new JacksonJsonProvider(mapper); + DefaultClientConfig conf = new DefaultClientConfig(); + conf.getSingletons().add(jsonProvider); + Client client = Client.create(conf); + if (debugging) { + client.addFilter(new LoggingFilter()); } - return hostMap.get(basePath); + return client; } } From 3d652186192732c1fea0395ba1a37712269327b0 Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 8 Jan 2016 19:05:43 +0800 Subject: [PATCH 2/2] Java jersey2 client: get/set connect timeout --- .../main/resources/Java/ApiClient.mustache | 4 +- .../Java/libraries/jersey2/ApiClient.mustache | 48 +++++++++++++---- .../java/io/swagger/client/ApiClient.java | 6 +-- .../java/io/swagger/client/ApiClient.java | 52 ++++++++++++++----- 4 files changed, 81 insertions(+), 29 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index e5f64a8c908..9f55d5a14d4 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -59,8 +59,6 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { - httpClient = buildHttpClient(debugging); - mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -69,6 +67,8 @@ public class ApiClient { mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); + httpClient = buildHttpClient(debugging); + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index f482b8c34d4..83e92921a82 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -12,7 +12,9 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.filter.LoggingFilter; +import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.MultiPart; @@ -43,12 +45,13 @@ import {{invokerPackage}}.auth.OAuth; {{>generatedAnnotation}} public class ApiClient { - private Client client; - private Map hostMap = new HashMap(); private Map defaultHeaderMap = new HashMap(); - private boolean debugging = false; private String basePath = "{{basePath}}"; - private JSON json = new JSON(); + private boolean debugging = false; + private int connectionTimeout = 0; + + private Client httpClient; + private JSON json; private Map authentications; @@ -58,6 +61,9 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { + json = new JSON(); + httpClient = buildHttpClient(debugging); + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); @@ -70,8 +76,6 @@ public class ApiClient { // Set default User-Agent. setUserAgent("Java-Swagger"); - buildClient(); - // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap();{{#authMethods}}{{#isBasic}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} @@ -226,10 +230,29 @@ public class ApiClient { */ public ApiClient setDebugging(boolean debugging) { this.debugging = debugging; - buildClient(); + // Rebuild HTTP Client according to the new "debugging" value. + this.httpClient = buildHttpClient(debugging); return this; } + /** + * Connect timeout (in milliseconds). + */ + public int getConnectTimeout() { + return connectionTimeout; + } + + /** + * Set the connect timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + */ + public ApiClient setConnectTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + httpClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout); + return this; + } + /** * Get the date format used to parse/format date parameters. */ @@ -472,7 +495,7 @@ public class ApiClient { public T invokeAPI(String path, String method, List queryParams, Object body, Map headerParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); - WebTarget target = client.target(this.basePath).path(path); + WebTarget target = httpClient.target(this.basePath).path(path); if (queryParams != null) { for (Pair queryParam : queryParams) { @@ -545,15 +568,18 @@ public class ApiClient { } } - private void buildClient() { + /** + * Build the Client used to make HTTP requests. + */ + private Client buildHttpClient(boolean debugging) { final ClientConfig clientConfig = new ClientConfig(); clientConfig.register(MultiPartFeature.class); clientConfig.register(json); - clientConfig.register(org.glassfish.jersey.jackson.JacksonFeature.class); + clientConfig.register(JacksonFeature.class); if (debugging) { clientConfig.register(LoggingFilter.class); } - this.client = ClientBuilder.newClient(clientConfig); + return ClientBuilder.newClient(clientConfig); } private Map> buildResponseHeaders(Response response) { diff --git a/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java index daa584094bb..581eafa51ca 100644 --- a/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiClient.java @@ -41,7 +41,7 @@ import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.OAuth; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-08T17:06:02.039+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-08T18:50:38.131+08:00") public class ApiClient { private Map defaultHeaderMap = new HashMap(); private String basePath = "http://petstore.swagger.io/v2"; @@ -59,8 +59,6 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { - httpClient = buildHttpClient(debugging); - mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -69,6 +67,8 @@ public class ApiClient { mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.registerModule(new JodaModule()); + httpClient = buildHttpClient(debugging); + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); diff --git a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java index 73deed7fad8..7f06e36633f 100644 --- a/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2/src/main/java/io/swagger/client/ApiClient.java @@ -12,7 +12,9 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.filter.LoggingFilter; +import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.MultiPart; @@ -41,14 +43,15 @@ import io.swagger.client.auth.HttpBasicAuth; import io.swagger.client.auth.ApiKeyAuth; import io.swagger.client.auth.OAuth; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-05T14:39:17.660+08:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-08T18:51:26.068+08:00") public class ApiClient { - private Client client; - private Map hostMap = new HashMap(); private Map defaultHeaderMap = new HashMap(); - private boolean debugging = false; private String basePath = "http://petstore.swagger.io/v2"; - private JSON json = new JSON(); + private boolean debugging = false; + private int connectionTimeout = 0; + + private Client httpClient; + private JSON json; private Map authentications; @@ -58,6 +61,9 @@ public class ApiClient { private DateFormat dateFormat; public ApiClient() { + json = new JSON(); + httpClient = buildHttpClient(debugging); + // Use RFC3339 format for date and datetime. // See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14 this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); @@ -70,12 +76,10 @@ public class ApiClient { // Set default User-Agent. setUserAgent("Java-Swagger"); - buildClient(); - // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); - authentications.put("api_key", new ApiKeyAuth("header", "api_key")); authentications.put("petstore_auth", new OAuth()); + authentications.put("api_key", new ApiKeyAuth("header", "api_key")); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); } @@ -225,10 +229,29 @@ public class ApiClient { */ public ApiClient setDebugging(boolean debugging) { this.debugging = debugging; - buildClient(); + // Rebuild HTTP Client according to the new "debugging" value. + this.httpClient = buildHttpClient(debugging); return this; } + /** + * Connect timeout (in milliseconds). + */ + public int getConnectTimeout() { + return connectionTimeout; + } + + /** + * Set the connect timeout (in milliseconds). + * A value of 0 means no timeout, otherwise values must be between 1 and + * {@link Integer#MAX_VALUE}. + */ + public ApiClient setConnectTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + httpClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout); + return this; + } + /** * Get the date format used to parse/format date parameters. */ @@ -471,7 +494,7 @@ public class ApiClient { public T invokeAPI(String path, String method, List queryParams, Object body, Map headerParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); - WebTarget target = client.target(this.basePath).path(path); + WebTarget target = httpClient.target(this.basePath).path(path); if (queryParams != null) { for (Pair queryParam : queryParams) { @@ -544,15 +567,18 @@ public class ApiClient { } } - private void buildClient() { + /** + * Build the Client used to make HTTP requests. + */ + private Client buildHttpClient(boolean debugging) { final ClientConfig clientConfig = new ClientConfig(); clientConfig.register(MultiPartFeature.class); clientConfig.register(json); - clientConfig.register(org.glassfish.jersey.jackson.JacksonFeature.class); + clientConfig.register(JacksonFeature.class); if (debugging) { clientConfig.register(LoggingFilter.class); } - this.client = ClientBuilder.newClient(clientConfig); + return ClientBuilder.newClient(clientConfig); } private Map> buildResponseHeaders(Response response) {