();{{#authMethods}}{{#isBasic}}
+ authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
+ authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
+ authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
+ // Prevent the authentications from being modified.
+ authentications = Collections.unmodifiableMap(authentications);
+
+ rebuildHttpClient();
+ }
+
+ public static DateFormat buildDefaultDateFormat() {
+ return new RFC3339DateFormat();
+ }
+
+ /**
+ * Build the Client used to make HTTP requests with the latest settings,
+ * i.e. objectMapper and debugging.
+ * TODO: better to use the Builder Pattern?
+ * @return API client
+ */
+ public ApiClient rebuildHttpClient() {
+ // Add the JSON serialization support to Jersey
+ JacksonJsonProvider jsonProvider = new JacksonJsonProvider(objectMapper);
+ DefaultClientConfig conf = new DefaultClientConfig();
+ conf.getSingletons().add(jsonProvider);
+ Client client = Client.create(conf);
+ if (debugging) {
+ client.addFilter(new LoggingFilter());
+ }
+ this.httpClient = client;
+ return this;
+ }
+
+ /**
+ * Returns the current object mapper used for JSON serialization/deserialization.
+ *
+ * Note: If you make changes to the object mapper, remember to set it back via
+ * setObjectMapper
in order to trigger HTTP client rebuilding.
+ *
+ * @return Object mapper
+ */
+ public ObjectMapper getObjectMapper() {
+ return objectMapper;
+ }
+
+ public ApiClient setObjectMapper(ObjectMapper objectMapper) {
+ this.objectMapper = objectMapper;
+ // Need to rebuild the Client as it depends on object mapper.
+ rebuildHttpClient();
+ return this;
+ }
+
+ public Client getHttpClient() {
+ return httpClient;
+ }
+
+ public ApiClient setHttpClient(Client httpClient) {
+ this.httpClient = httpClient;
+ return this;
+ }
+
+ public String getBasePath() {
+ return basePath;
+ }
+
+ public ApiClient setBasePath(String basePath) {
+ this.basePath = basePath;
+ return this;
+ }
+
+ /**
+ * Gets the status code of the previous request
+ * @return Status code
+ */
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ /**
+ * Gets the response headers of the previous request
+ * @return Response headers
+ */
+ public Map> getResponseHeaders() {
+ return responseHeaders;
+ }
+
+ /**
+ * Get authentications (key: authentication name, value: authentication).
+ * @return Map of authentication
+ */
+ public Map getAuthentications() {
+ return authentications;
+ }
+
+ /**
+ * Get authentication for the given name.
+ *
+ * @param authName The authentication name
+ * @return The authentication, null if not found
+ */
+ public Authentication getAuthentication(String authName) {
+ return authentications.get(authName);
+ }
+
+ /**
+ * Helper method to set username for the first HTTP basic authentication.
+ * @param username Username
+ */
+ public void setUsername(String username) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof HttpBasicAuth) {
+ ((HttpBasicAuth) auth).setUsername(username);
+ return;
+ }
+ }
+ throw new RuntimeException("No HTTP basic authentication configured!");
+ }
+
+ /**
+ * Helper method to set password for the first HTTP basic authentication.
+ * @param password Password
+ */
+ public void setPassword(String password) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof HttpBasicAuth) {
+ ((HttpBasicAuth) auth).setPassword(password);
+ return;
+ }
+ }
+ throw new RuntimeException("No HTTP basic authentication configured!");
+ }
+
+ /**
+ * Helper method to set API key value for the first API key authentication.
+ * @param apiKey API key
+ */
+ public void setApiKey(String apiKey) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof ApiKeyAuth) {
+ ((ApiKeyAuth) auth).setApiKey(apiKey);
+ return;
+ }
+ }
+ throw new RuntimeException("No API key authentication configured!");
+ }
+
+ /**
+ * Helper method to set API key prefix for the first API key authentication.
+ * @param apiKeyPrefix API key prefix
+ */
+ public void setApiKeyPrefix(String apiKeyPrefix) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof ApiKeyAuth) {
+ ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
+ return;
+ }
+ }
+ throw new RuntimeException("No API key authentication configured!");
+ }
+
+ /**
+ * Helper method to set access token for the first OAuth2 authentication.
+ * @param accessToken Access token
+ */
+ public void setAccessToken(String accessToken) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof OAuth) {
+ ((OAuth) auth).setAccessToken(accessToken);
+ return;
+ }
+ }
+ throw new RuntimeException("No OAuth2 authentication configured!");
+ }
+
+ /**
+ * Set the User-Agent header's value (by adding to the default header map).
+ * @param userAgent User agent
+ * @return API client
+ */
+ public ApiClient setUserAgent(String userAgent) {
+ addDefaultHeader("User-Agent", userAgent);
+ return this;
+ }
+
+ /**
+ * Add a default header.
+ *
+ * @param key The header's key
+ * @param value The header's value
+ * @return API client
+ */
+ public ApiClient addDefaultHeader(String key, String value) {
+ defaultHeaderMap.put(key, value);
+ return this;
+ }
+
+ /**
+ * Check that whether debugging is enabled for this API client.
+ * @return True if debugging is on
+ */
+ public boolean isDebugging() {
+ return debugging;
+ }
+
+ /**
+ * Enable/disable debugging for this API client.
+ *
+ * @param debugging To enable (true) or disable (false) debugging
+ * @return API client
+ */
+ public ApiClient setDebugging(boolean debugging) {
+ this.debugging = debugging;
+ // Need to rebuild the Client as it depends on the value of debugging.
+ rebuildHttpClient();
+ return this;
+ }
+
+ /**
+ * Connect timeout (in milliseconds).
+ * @return Connection timeout
+ */
+ 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}.
+ * @param connectionTimeout Connection timeout in milliseconds
+ * @return API client
+ */
+ public ApiClient setConnectTimeout(int connectionTimeout) {
+ this.connectionTimeout = connectionTimeout;
+ httpClient.setConnectTimeout(connectionTimeout);
+ return this;
+ }
+
+ /**
+ * Get the date format used to parse/format date parameters.
+ * @return Date format
+ */
+ public DateFormat getDateFormat() {
+ return dateFormat;
+ }
+
+ /**
+ * Set the date format used to parse/format date parameters.
+ * @param dateFormat Date format
+ * @return API client
+ */
+ public ApiClient setDateFormat(DateFormat dateFormat) {
+ this.dateFormat = dateFormat;
+ // Also set the date format for model (de)serialization with Date properties.
+ this.objectMapper.setDateFormat((DateFormat) dateFormat.clone());
+ // Need to rebuild the Client as objectMapper changes.
+ rebuildHttpClient();
+ return this;
+ }
+
+ /**
+ * Parse the given string into Date object.
+ * @param str String
+ * @return Date
+ */
+ public Date parseDate(String str) {
+ try {
+ return dateFormat.parse(str);
+ } catch (java.text.ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Format the given Date object into string.
+ * @param date Date
+ * @return Date in string format
+ */
+ public String formatDate(Date date) {
+ return dateFormat.format(date);
+ }
+
+ /**
+ * Format the given parameter object into string.
+ * @param param Object
+ * @return Object in string format
+ */
+ public String parameterToString(Object param) {
+ if (param == null) {
+ return "";
+ } else if (param instanceof Date) {
+ return formatDate((Date) param);
+ } else if (param instanceof Collection) {
+ StringBuilder b = new StringBuilder();
+ for(Object o : (Collection>)param) {
+ if(b.length() > 0) {
+ b.append(',');
+ }
+ b.append(String.valueOf(o));
+ }
+ return b.toString();
+ } else {
+ return String.valueOf(param);
+ }
+ }
+
+ /*
+ * Format to {@code Pair} objects.
+ * @param collectionFormat Collection format
+ * @param name Name
+ * @param value Value
+ * @return List of pair
+ */
+ public List parameterToPairs(String collectionFormat, String name, Object value){
+ List params = new ArrayList();
+
+ // preconditions
+ if (name == null || name.isEmpty() || value == null) return params;
+
+ Collection> valueCollection;
+ if (value instanceof Collection>) {
+ valueCollection = (Collection>) value;
+ } else {
+ params.add(new Pair(name, parameterToString(value)));
+ return params;
+ }
+
+ if (valueCollection.isEmpty()){
+ return params;
+ }
+
+ // get the collection format
+ String format = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
+
+ // create the params based on the collection format
+ if ("multi".equals(format)) {
+ for (Object item : valueCollection) {
+ params.add(new Pair(name, parameterToString(item)));
+ }
+
+ return params;
+ }
+
+ String delimiter = ",";
+
+ if ("csv".equals(format)) {
+ delimiter = ",";
+ } else if ("ssv".equals(format)) {
+ delimiter = " ";
+ } else if ("tsv".equals(format)) {
+ delimiter = "\t";
+ } else if ("pipes".equals(format)) {
+ delimiter = "|";
+ }
+
+ StringBuilder sb = new StringBuilder() ;
+ for (Object item : valueCollection) {
+ sb.append(delimiter);
+ sb.append(parameterToString(item));
+ }
+
+ params.add(new Pair(name, sb.substring(1)));
+
+ return params;
+ }
+
+ /**
+ * Check if the given MIME is a JSON MIME.
+ * JSON MIME examples:
+ * application/json
+ * application/json; charset=UTF8
+ * APPLICATION/JSON
+ * @param mime MIME
+ * @return True if MIME type is boolean
+ */
+ public boolean isJsonMime(String mime) {
+ return mime != null && mime.matches("(?i)application\\/json(;.*)?");
+ }
+
+ /**
+ * Select the Accept header's value from the given accepts array:
+ * if JSON exists in the given array, use it;
+ * otherwise use all of them (joining into a string)
+ *
+ * @param accepts The accepts array to select from
+ * @return The Accept header to use. If the given array is empty,
+ * null will be returned (not to set the Accept header explicitly).
+ */
+ public String selectHeaderAccept(String[] accepts) {
+ if (accepts.length == 0) {
+ return null;
+ }
+ for (String accept : accepts) {
+ if (isJsonMime(accept)) {
+ return accept;
+ }
+ }
+ return StringUtil.join(accepts, ",");
+ }
+
+ /**
+ * Select the Content-Type header's value from the given array:
+ * if JSON exists in the given array, use it;
+ * otherwise use the first one of the array.
+ *
+ * @param contentTypes The Content-Type array to select from
+ * @return The Content-Type header to use. If the given array is empty,
+ * JSON will be used.
+ */
+ public String selectHeaderContentType(String[] contentTypes) {
+ if (contentTypes.length == 0) {
+ return "application/json";
+ }
+ for (String contentType : contentTypes) {
+ if (isJsonMime(contentType)) {
+ return contentType;
+ }
+ }
+ return contentTypes[0];
+ }
+
+ /**
+ * Escape the given string to be used as URL query value.
+ * @param str String
+ * @return Escaped string
+ */
+ public String escapeString(String str) {
+ try {
+ return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
+ } catch (UnsupportedEncodingException e) {
+ return str;
+ }
+ }
+
+ /**
+ * Serialize the given Java object into string according the given
+ * Content-Type (only JSON is supported for now).
+ * @param obj Object
+ * @param contentType Content type
+ * @param formParams Form parameters
+ * @return Object
+ * @throws ApiException API exception
+ */
+ public Object serialize(Object obj, String contentType, Map formParams) throws ApiException {
+ if (contentType.startsWith("multipart/form-data")) {
+ FormDataMultiPart mp = new FormDataMultiPart();
+ for (Entry param: formParams.entrySet()) {
+ if( param.getValue() instanceof List && !( ( List ) param.getValue() ).isEmpty()
+ && ( ( List ) param.getValue() ).get( 0 ) instanceof File ) {
+ @SuppressWarnings( "unchecked" )
+ List files = ( List ) param.getValue();
+ for( File file : files ) {
+ mp.bodyPart( new FileDataBodyPart( param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE ) );
+ }
+ } else if (param.getValue() instanceof File) {
+ File file = (File) param.getValue();
+ mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
+ } else {
+ mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
+ }
+ }
+ return mp;
+ } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
+ return this.getXWWWFormUrlencodedParams(formParams);
+ } else {
+ // We let Jersey attempt to serialize the body
+ return obj;
+ }
+ }
+
+ /**
+ * Build full URL by concatenating base path, the given sub path and query parameters.
+ *
+ * @param path The sub path
+ * @param queryParams The query parameters
+ * @return The full URL
+ */
+ private String buildUrl(String path, List queryParams) {
+ final StringBuilder url = new StringBuilder();
+ url.append(basePath).append(path);
+
+ if (queryParams != null && !queryParams.isEmpty()) {
+ // support (constant) query string in `path`, e.g. "/posts?draft=1"
+ String prefix = path.contains("?") ? "&" : "?";
+ for (Pair param : queryParams) {
+ if (param.getValue() != null) {
+ if (prefix != null) {
+ url.append(prefix);
+ prefix = null;
+ } else {
+ url.append("&");
+ }
+ String value = parameterToString(param.getValue());
+ url.append(escapeString(param.getName())).append("=").append(escapeString(value));
+ }
+ }
+ }
+
+ return url.toString();
+ }
+
+ private ClientResponse getAPIResponse(String path, String method, List queryParams, Object body, Map headerParams, Map formParams, String accept, String contentType, String[] authNames) throws ApiException {
+ if (body != null && !formParams.isEmpty()) {
+ throw new ApiException(500, "Cannot have body and form params");
+ }
+
+ updateParamsForAuth(authNames, queryParams, headerParams);
+
+ final String url = buildUrl(path, queryParams);
+ Builder builder;
+ if (accept == null) {
+ builder = httpClient.resource(url).getRequestBuilder();
+ } else {
+ builder = httpClient.resource(url).accept(accept);
+ }
+
+ for (String key : headerParams.keySet()) {
+ builder = builder.header(key, headerParams.get(key));
+ }
+ for (String key : defaultHeaderMap.keySet()) {
+ if (!headerParams.containsKey(key)) {
+ builder = builder.header(key, defaultHeaderMap.get(key));
+ }
+ }
+
+ ClientResponse response = null;
+
+ if ("GET".equals(method)) {
+ response = (ClientResponse) builder.get(ClientResponse.class);
+ } else if ("POST".equals(method)) {
+ response = builder.type(contentType).post(ClientResponse.class, serialize(body, contentType, formParams));
+ } else if ("PUT".equals(method)) {
+ response = builder.type(contentType).put(ClientResponse.class, serialize(body, contentType, formParams));
+ } else if ("DELETE".equals(method)) {
+ response = builder.type(contentType).delete(ClientResponse.class, serialize(body, contentType, formParams));
+ } else if ("PATCH".equals(method)) {
+ response = builder.type(contentType).header("X-HTTP-Method-Override", "PATCH").post(ClientResponse.class, serialize(body, contentType, formParams));
+ }
+ else {
+ throw new ApiException(500, "unknown method type " + method);
+ }
+ return response;
+ }
+
+ /**
+ * Invoke API by sending HTTP request with the given options.
+ *
+ * @param Type
+ * @param path The sub-path of the HTTP URL
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
+ * @param queryParams The query parameters
+ * @param body The request body object - if it is not binary, otherwise null
+ * @param headerParams The header parameters
+ * @param formParams The form parameters
+ * @param accept The request's Accept header
+ * @param contentType The request's Content-Type header
+ * @param authNames The authentications to apply
+ * @param returnType Return type
+ * @return The response body in type of string
+ * @throws ApiException API exception
+ */
+ 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 {
+
+ ClientResponse response = getAPIResponse(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames);
+
+ statusCode = response.getStatusInfo().getStatusCode();
+ responseHeaders = response.getHeaders();
+
+ if(response.getStatusInfo().getStatusCode() == ClientResponse.Status.NO_CONTENT.getStatusCode()) {
+ return null;
+ } else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
+ if (returnType == null)
+ return null;
+ else
+ return response.getEntity(returnType);
+ } else {
+ String message = "error";
+ String respBody = null;
+ if (response.hasEntity()) {
+ try {
+ respBody = response.getEntity(String.class);
+ message = respBody;
+ } catch (RuntimeException e) {
+ // e.printStackTrace();
+ }
+ }
+ throw new ApiException(
+ response.getStatusInfo().getStatusCode(),
+ message,
+ response.getHeaders(),
+ respBody);
+ }
+ }
+
+ /**
+ * Update query and header parameters based on authentication settings.
+ *
+ * @param authNames The authentications to apply
+ * @param queryParams Query parameters
+ * @param headerParams Header parameters
+ */
+ private void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) {
+ for (String authName : authNames) {
+ Authentication auth = authentications.get(authName);
+ if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
+ auth.applyToParams(queryParams, headerParams);
+ }
+ }
+
+ /**
+ * Encode the given form parameters as request body.
+ * @param formParams Form parameters
+ * @return HTTP form encoded parameters
+ */
+ private String getXWWWFormUrlencodedParams(Map formParams) {
+ StringBuilder formParamBuilder = new StringBuilder();
+
+ for (Entry param : formParams.entrySet()) {
+ String valueStr = parameterToString(param.getValue());
+ try {
+ formParamBuilder.append(URLEncoder.encode(param.getKey(), "utf8"))
+ .append("=")
+ .append(URLEncoder.encode(valueStr, "utf8"));
+ formParamBuilder.append("&");
+ } catch (UnsupportedEncodingException e) {
+ // move on to next
+ }
+ }
+
+ String encodedFormParams = formParamBuilder.toString();
+ if (encodedFormParams.endsWith("&")) {
+ encodedFormParams = encodedFormParams.substring(0, encodedFormParams.length() - 1);
+ }
+
+ return encodedFormParams;
+ }
+}
diff --git a/modules/openapi-generator/src/test/resources/2_0/templates/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/test/resources/2_0/templates/Java/libraries/jersey2/ApiClient.mustache
new file mode 100644
index 0000000000..96df77be8e
--- /dev/null
+++ b/modules/openapi-generator/src/test/resources/2_0/templates/Java/libraries/jersey2/ApiClient.mustache
@@ -0,0 +1,771 @@
+//overloaded template file within library folder to add this comment
+
+package {{invokerPackage}};
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Form;
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.MediaType;
+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;
+import org.glassfish.jersey.media.multipart.MultiPartFeature;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+{{^supportJava6}}
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+{{/supportJava6}}
+{{#supportJava6}}
+import org.apache.commons.io.FileUtils;
+{{/supportJava6}}
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.TimeZone;
+
+import java.net.URLEncoder;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+
+import java.text.DateFormat;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import {{invokerPackage}}.auth.Authentication;
+import {{invokerPackage}}.auth.HttpBasicAuth;
+import {{invokerPackage}}.auth.ApiKeyAuth;
+import {{invokerPackage}}.auth.OAuth;
+
+{{>generatedAnnotation}}
+public class ApiClient {
+ private Map defaultHeaderMap = new HashMap();
+ private String basePath = "{{{basePath}}}";
+ private boolean debugging = false;
+ private int connectionTimeout = 0;
+
+ private Client httpClient;
+ private JSON json;
+ private String tempFolderPath = null;
+
+ private Map authentications;
+
+ private int statusCode;
+ private Map> responseHeaders;
+
+ private DateFormat dateFormat;
+
+ public ApiClient() {
+ json = new JSON();
+ httpClient = buildHttpClient(debugging);
+
+ this.dateFormat = new RFC3339DateFormat();
+
+ // Set default User-Agent.
+ setUserAgent("{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{artifactVersion}}}/java{{/httpUserAgent}}");
+
+ // Setup authentications (key: authentication name, value: authentication).
+ authentications = new HashMap();{{#authMethods}}{{#isBasic}}
+ authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
+ authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
+ authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
+ // Prevent the authentications from being modified.
+ authentications = Collections.unmodifiableMap(authentications);
+ }
+
+ /**
+ * Gets the JSON instance to do JSON serialization and deserialization.
+ * @return JSON
+ */
+ public JSON getJSON() {
+ return json;
+ }
+
+ public Client getHttpClient() {
+ return httpClient;
+ }
+
+ public ApiClient setHttpClient(Client httpClient) {
+ this.httpClient = httpClient;
+ return this;
+ }
+
+ public String getBasePath() {
+ return basePath;
+ }
+
+ public ApiClient setBasePath(String basePath) {
+ this.basePath = basePath;
+ return this;
+ }
+
+ /**
+ * Gets the status code of the previous request
+ * @return Status code
+ */
+ public int getStatusCode() {
+ return statusCode;
+ }
+
+ /**
+ * Gets the response headers of the previous request
+ * @return Response headers
+ */
+ public Map> getResponseHeaders() {
+ return responseHeaders;
+ }
+
+ /**
+ * Get authentications (key: authentication name, value: authentication).
+ * @return Map of authentication object
+ */
+ public Map getAuthentications() {
+ return authentications;
+ }
+
+ /**
+ * Get authentication for the given name.
+ *
+ * @param authName The authentication name
+ * @return The authentication, null if not found
+ */
+ public Authentication getAuthentication(String authName) {
+ return authentications.get(authName);
+ }
+
+ /**
+ * Helper method to set username for the first HTTP basic authentication.
+ * @param username Username
+ */
+ public void setUsername(String username) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof HttpBasicAuth) {
+ ((HttpBasicAuth) auth).setUsername(username);
+ return;
+ }
+ }
+ throw new RuntimeException("No HTTP basic authentication configured!");
+ }
+
+ /**
+ * Helper method to set password for the first HTTP basic authentication.
+ * @param password Password
+ */
+ public void setPassword(String password) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof HttpBasicAuth) {
+ ((HttpBasicAuth) auth).setPassword(password);
+ return;
+ }
+ }
+ throw new RuntimeException("No HTTP basic authentication configured!");
+ }
+
+ /**
+ * Helper method to set API key value for the first API key authentication.
+ * @param apiKey API key
+ */
+ public void setApiKey(String apiKey) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof ApiKeyAuth) {
+ ((ApiKeyAuth) auth).setApiKey(apiKey);
+ return;
+ }
+ }
+ throw new RuntimeException("No API key authentication configured!");
+ }
+
+ /**
+ * Helper method to set API key prefix for the first API key authentication.
+ * @param apiKeyPrefix API key prefix
+ */
+ public void setApiKeyPrefix(String apiKeyPrefix) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof ApiKeyAuth) {
+ ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
+ return;
+ }
+ }
+ throw new RuntimeException("No API key authentication configured!");
+ }
+
+ /**
+ * Helper method to set access token for the first OAuth2 authentication.
+ * @param accessToken Access token
+ */
+ public void setAccessToken(String accessToken) {
+ for (Authentication auth : authentications.values()) {
+ if (auth instanceof OAuth) {
+ ((OAuth) auth).setAccessToken(accessToken);
+ return;
+ }
+ }
+ throw new RuntimeException("No OAuth2 authentication configured!");
+ }
+
+ /**
+ * Set the User-Agent header's value (by adding to the default header map).
+ * @param userAgent Http user agent
+ * @return API client
+ */
+ public ApiClient setUserAgent(String userAgent) {
+ addDefaultHeader("User-Agent", userAgent);
+ return this;
+ }
+
+ /**
+ * Add a default header.
+ *
+ * @param key The header's key
+ * @param value The header's value
+ * @return API client
+ */
+ public ApiClient addDefaultHeader(String key, String value) {
+ defaultHeaderMap.put(key, value);
+ return this;
+ }
+
+ /**
+ * Check that whether debugging is enabled for this API client.
+ * @return True if debugging is switched on
+ */
+ public boolean isDebugging() {
+ return debugging;
+ }
+
+ /**
+ * Enable/disable debugging for this API client.
+ *
+ * @param debugging To enable (true) or disable (false) debugging
+ * @return API client
+ */
+ public ApiClient setDebugging(boolean debugging) {
+ this.debugging = debugging;
+ // Rebuild HTTP Client according to the new "debugging" value.
+ this.httpClient = buildHttpClient(debugging);
+ return this;
+ }
+
+ /**
+ * The path of temporary folder used to store downloaded files from endpoints
+ * with file response. The default value is null
, i.e. using
+ * the system's default tempopary folder.
+ *
+ * @return Temp folder path
+ */
+ public String getTempFolderPath() {
+ return tempFolderPath;
+ }
+
+ /**
+ * Set temp folder path
+ * @param tempFolderPath Temp folder path
+ * @return API client
+ */
+ public ApiClient setTempFolderPath(String tempFolderPath) {
+ this.tempFolderPath = tempFolderPath;
+ return this;
+ }
+
+ /**
+ * Connect timeout (in milliseconds).
+ * @return Connection timeout
+ */
+ 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}.
+ * @param connectionTimeout Connection timeout in milliseconds
+ * @return API client
+ */
+ 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.
+ * @return Date format
+ */
+ public DateFormat getDateFormat() {
+ return dateFormat;
+ }
+
+ /**
+ * Set the date format used to parse/format date parameters.
+ * @param dateFormat Date format
+ * @return API client
+ */
+ public ApiClient setDateFormat(DateFormat dateFormat) {
+ this.dateFormat = dateFormat;
+ // also set the date format for model (de)serialization with Date properties
+ this.json.setDateFormat((DateFormat) dateFormat.clone());
+ return this;
+ }
+
+ /**
+ * Parse the given string into Date object.
+ * @param str String
+ * @return Date
+ */
+ public Date parseDate(String str) {
+ try {
+ return dateFormat.parse(str);
+ } catch (java.text.ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Format the given Date object into string.
+ * @param date Date
+ * @return Date in string format
+ */
+ public String formatDate(Date date) {
+ return dateFormat.format(date);
+ }
+
+ /**
+ * Format the given parameter object into string.
+ * @param param Object
+ * @return Object in string format
+ */
+ public String parameterToString(Object param) {
+ if (param == null) {
+ return "";
+ } else if (param instanceof Date) {
+ return formatDate((Date) param);
+ } else if (param instanceof Collection) {
+ StringBuilder b = new StringBuilder();
+ for(Object o : (Collection)param) {
+ if(b.length() > 0) {
+ b.append(',');
+ }
+ b.append(String.valueOf(o));
+ }
+ return b.toString();
+ } else {
+ return String.valueOf(param);
+ }
+ }
+
+ /*
+ * Format to {@code Pair} objects.
+ * @param collectionFormat Collection format
+ * @param name Name
+ * @param value Value
+ * @return List of pairs
+ */
+ public List parameterToPairs(String collectionFormat, String name, Object value){
+ List params = new ArrayList();
+
+ // preconditions
+ if (name == null || name.isEmpty() || value == null) return params;
+
+ Collection valueCollection;
+ if (value instanceof Collection) {
+ valueCollection = (Collection) value;
+ } else {
+ params.add(new Pair(name, parameterToString(value)));
+ return params;
+ }
+
+ if (valueCollection.isEmpty()){
+ return params;
+ }
+
+ // get the collection format (default: csv)
+ String format = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat);
+
+ // create the params based on the collection format
+ if ("multi".equals(format)) {
+ for (Object item : valueCollection) {
+ params.add(new Pair(name, parameterToString(item)));
+ }
+
+ return params;
+ }
+
+ String delimiter = ",";
+
+ if ("csv".equals(format)) {
+ delimiter = ",";
+ } else if ("ssv".equals(format)) {
+ delimiter = " ";
+ } else if ("tsv".equals(format)) {
+ delimiter = "\t";
+ } else if ("pipes".equals(format)) {
+ delimiter = "|";
+ }
+
+ StringBuilder sb = new StringBuilder() ;
+ for (Object item : valueCollection) {
+ sb.append(delimiter);
+ sb.append(parameterToString(item));
+ }
+
+ params.add(new Pair(name, sb.substring(1)));
+
+ return params;
+ }
+
+ /**
+ * Check if the given MIME is a JSON MIME.
+ * JSON MIME examples:
+ * application/json
+ * application/json; charset=UTF8
+ * APPLICATION/JSON
+ * @param mime MIME
+ * @return True if the MIME type is JSON
+ */
+ public boolean isJsonMime(String mime) {
+ return mime != null && mime.matches("(?i)application\\/json(;.*)?");
+ }
+
+ /**
+ * Select the Accept header's value from the given accepts array:
+ * if JSON exists in the given array, use it;
+ * otherwise use all of them (joining into a string)
+ *
+ * @param accepts The accepts array to select from
+ * @return The Accept header to use. If the given array is empty,
+ * null will be returned (not to set the Accept header explicitly).
+ */
+ public String selectHeaderAccept(String[] accepts) {
+ if (accepts.length == 0) {
+ return null;
+ }
+ for (String accept : accepts) {
+ if (isJsonMime(accept)) {
+ return accept;
+ }
+ }
+ return StringUtil.join(accepts, ",");
+ }
+
+ /**
+ * Select the Content-Type header's value from the given array:
+ * if JSON exists in the given array, use it;
+ * otherwise use the first one of the array.
+ *
+ * @param contentTypes The Content-Type array to select from
+ * @return The Content-Type header to use. If the given array is empty,
+ * JSON will be used.
+ */
+ public String selectHeaderContentType(String[] contentTypes) {
+ if (contentTypes.length == 0) {
+ return "application/json";
+ }
+ for (String contentType : contentTypes) {
+ if (isJsonMime(contentType)) {
+ return contentType;
+ }
+ }
+ return contentTypes[0];
+ }
+
+ /**
+ * Escape the given string to be used as URL query value.
+ * @param str String
+ * @return Escaped string
+ */
+ public String escapeString(String str) {
+ try {
+ return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
+ } catch (UnsupportedEncodingException e) {
+ return str;
+ }
+ }
+
+ /**
+ * Serialize the given Java object into string entity according the given
+ * Content-Type (only JSON is supported for now).
+ * @param obj Object
+ * @param formParams Form parameters
+ * @param contentType Context type
+ * @return Entity
+ * @throws ApiException API exception
+ */
+ public Entity> serialize(Object obj, Map formParams, String contentType) throws ApiException {
+ Entity> entity;
+ if (contentType.startsWith("multipart/form-data")) {
+ MultiPart multiPart = new MultiPart();
+ for (Entry param: formParams.entrySet()) {
+ if (param.getValue() instanceof File) {
+ File file = (File) param.getValue();
+ FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
+ .fileName(file.getName()).size(file.length()).build();
+ multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, MediaType.APPLICATION_OCTET_STREAM_TYPE));
+ } else {
+ FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();
+ multiPart.bodyPart(new FormDataBodyPart(contentDisp, parameterToString(param.getValue())));
+ }
+ }
+ entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);
+ } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
+ Form form = new Form();
+ for (Entry param: formParams.entrySet()) {
+ form.param(param.getKey(), parameterToString(param.getValue()));
+ }
+ entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
+ } else {
+ // We let jersey handle the serialization
+ entity = Entity.entity(obj, contentType);
+ }
+ return entity;
+ }
+
+ /**
+ * Deserialize response body to Java object according to the Content-Type.
+ * @param Type
+ * @param response Response
+ * @param returnType Return type
+ * @return Deserialize object
+ * @throws ApiException API exception
+ */
+ @SuppressWarnings("unchecked")
+ public T deserialize(Response response, GenericType returnType) throws ApiException {
+ if (response == null || returnType == null) {
+ return null;
+ }
+
+ if ("byte[]".equals(returnType.toString())) {
+ // Handle binary response (byte array).
+ return (T) response.readEntity(byte[].class);
+ } else if (returnType.getRawType() == File.class) {
+ // Handle file downloading.
+ T file = (T) downloadFileFromResponse(response);
+ return file;
+ }
+
+ String contentType = null;
+ List