forked from loafle/openapi-generator-original
Merge pull request #1853 from xhh/java-timeout
[Java] Support setting connect timeout for the default and jersey2 clients
This commit is contained in:
commit
3099aa2d85
@ -43,10 +43,12 @@ import {{invokerPackage}}.auth.OAuth;
|
||||
|
||||
{{>generatedAnnotation}}
|
||||
public class ApiClient {
|
||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
private boolean debugging = false;
|
||||
private String basePath = "{{basePath}}";
|
||||
private boolean debugging = false;
|
||||
private int connectionTimeout = 0;
|
||||
|
||||
private Client httpClient;
|
||||
private ObjectMapper mapper;
|
||||
|
||||
private Map<String, Authentication> authentications;
|
||||
@ -65,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");
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
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<String, Authentication> 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<String, Authentication>();{{#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> T invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> 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<String, List<String>> buildResponseHeaders(Response response) {
|
||||
|
@ -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-08T18:50:38.131+08:00")
|
||||
public class ApiClient {
|
||||
private Map<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
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<String, Authentication> authentications;
|
||||
@ -65,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");
|
||||
@ -79,8 +83,8 @@ public class ApiClient {
|
||||
|
||||
// Setup authentications (key: authentication name, value: authentication).
|
||||
authentications = new HashMap<String, Authentication>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String, Client> hostMap = new HashMap<String, Client>();
|
||||
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
|
||||
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<String, Authentication> 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<String, Authentication>();
|
||||
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> T invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> 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<String, List<String>> buildResponseHeaders(Response response) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user