diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java index 7c0ac7422ff..00273078436 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/JavaClientCodegen.java @@ -50,13 +50,17 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { additionalProperties.put("artifactId", artifactId); additionalProperties.put("artifactVersion", artifactVersion); + final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator); + final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", java.io.File.separator); + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); - supportingFiles.add(new SupportingFile("apiInvoker.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.java")); - supportingFiles.add(new SupportingFile("JsonUtil.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java")); - supportingFiles.add(new SupportingFile("apiException.mustache", - (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.java")); + supportingFiles.add(new SupportingFile("apiInvoker.mustache", invokerFolder, "ApiInvoker.java")); + supportingFiles.add(new SupportingFile("JsonUtil.mustache", invokerFolder, "JsonUtil.java")); + supportingFiles.add(new SupportingFile("apiException.mustache", invokerFolder, "ApiException.java")); + supportingFiles.add(new SupportingFile("configuration.mustache", invokerFolder, "Configuration.java")); + supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java")); + supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java")); + supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java")); languageSpecificPrimitives = new HashSet( Arrays.asList( diff --git a/modules/swagger-codegen/src/main/resources/Java/api.mustache b/modules/swagger-codegen/src/main/resources/Java/api.mustache index 78bac41d9d6..5731bbc6260 100644 --- a/modules/swagger-codegen/src/main/resources/Java/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/api.mustache @@ -92,7 +92,8 @@ public class {{classname}} { } try { - String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType); + String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} }; + String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, formParams, contentType, authNames); if(response != null){ return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; } diff --git a/modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache index ac689046047..94e8f45782d 100644 --- a/modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache @@ -31,6 +31,8 @@ import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.text.ParseException; +import {{invokerPackage}}.auth.Authentication; + public class ApiInvoker { private static ApiInvoker INSTANCE = new ApiInvoker(); private Map hostMap = new HashMap(); @@ -162,11 +164,12 @@ public class ApiInvoker { } } - public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType) throws ApiException { + public String invokeAPI(String host, String path, String method, Map queryParams, Object body, Map headerParams, Map formParams, String contentType, String[] authNames) throws ApiException { + processAuthParams(authNames, queryParams, headerParams); + Client client = getClient(host); StringBuilder b = new StringBuilder(); - for(String key : queryParams.keySet()) { String value = queryParams.get(key); if (value != null){ @@ -267,6 +270,14 @@ public class ApiInvoker { } } + private void processAuthParams(String[] authNames, Map queryParams, Map headerParams) { + for(String authName : authNames) { + Authentication auth = Configuration.getAuthentication(authName); + if(auth == null) throw new RuntimeException("Authentication has not been setup for " + authName); + auth.processParams(queryParams, headerParams); + } + } + private Client getClient(String host) { if(!hostMap.containsKey(host)) { Client client = Client.create(); diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache new file mode 100644 index 00000000000..f854bab9170 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/auth/ApiKeyAuth.mustache @@ -0,0 +1,55 @@ +package {{invokerPackage}}.auth; + +import java.util.Map; + +public class ApiKeyAuth implements Authentication { + private final String location; + private final String paramName; + + private String apiKey; + private String apiKeyPrefix; + + public ApiKeyAuth(String location, String paramName) { + this.location = location; + this.paramName = paramName; + } + + public String getLocation() { + return location; + } + + public String getParamName() { + return paramName; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public String getApiKeyPrefix() { + return apiKeyPrefix; + } + + public void setApiKeyPrefix(String apiKeyPrefix) { + this.apiKeyPrefix = apiKeyPrefix; + } + + @Override + public void processParams(Map queryParams, Map headerParams) { + String value; + if (apiKeyPrefix != null) { + value = apiKeyPrefix + " " + apiKey; + } else { + value = apiKey; + } + if (location == "query") { + queryParams.put(paramName, value); + } else if (location == "header") { + headerParams.put(paramName, value); + } + } +} diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache new file mode 100644 index 00000000000..7aba61e631a --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/auth/Authentication.mustache @@ -0,0 +1,7 @@ +package {{invokerPackage}}.auth; + +import java.util.Map; + +public interface Authentication { + void processParams(Map queryParams, Map headerParams); +} diff --git a/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache new file mode 100644 index 00000000000..f77bbf57b75 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/auth/HttpBasicAuth.mustache @@ -0,0 +1,36 @@ +package {{invokerPackage}}.auth; + +import java.util.Map; + +import java.io.UnsupportedEncodingException; +import javax.xml.bind.DatatypeConverter; + +public class HttpBasicAuth implements Authentication { + private String username; + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public void processParams(Map queryParams, Map headerParams) { + try { + headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"))); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } +} diff --git a/modules/swagger-codegen/src/main/resources/Java/configuration.mustache b/modules/swagger-codegen/src/main/resources/Java/configuration.mustache new file mode 100644 index 00000000000..13928df05c3 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/configuration.mustache @@ -0,0 +1,25 @@ +package {{invokerPackage}}; + +import java.util.Map; +import java.util.HashMap; + +import {{invokerPackage}}.auth.Authentication; +import {{invokerPackage}}.auth.HttpBasicAuth; +import {{invokerPackage}}.auth.ApiKeyAuth; + +public class Configuration { + private static final Map AUTH; + + static { + AUTH = new HashMap(); + {{#authMethods}} + {{#isBasic}}AUTH.put("{{name}}", new HttpBasicAuth());{{/isBasic}} + {{#isApiKey}}AUTH.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}} + {{#isOAuth}}// TODO: support oauth{{/isOAuth}} + {{/authMethods}} + } + + public static Authentication getAuthentication(String authName) { + return AUTH.get(authName); + } +}