diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java index 91bf395369..e595fb6a22 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AndroidVolleyClientCodegen.java @@ -260,6 +260,8 @@ public class AndroidVolleyClientCodegen extends DefaultCodegen implements Codege (sourceFolder + File.separator + requestPackage).replace(".", java.io.File.separator), "PatchRequest.java")); supportingFiles.add(new SupportingFile("auth/apikeyauth.mustache", (sourceFolder + File.separator + authPackage).replace(".", java.io.File.separator), "ApiKeyAuth.java")); + supportingFiles.add(new SupportingFile("auth/httpbasicauth.mustache", + (sourceFolder + File.separator + authPackage).replace(".", java.io.File.separator), "HttpBasicAuth.java")); supportingFiles.add(new SupportingFile("auth/authentication.mustache", (sourceFolder + File.separator + authPackage).replace(".", java.io.File.separator), "Authentication.java")); } diff --git a/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache index f5b3a763d5..04e6fdbc05 100644 --- a/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/android-volley/apiInvoker.mustache @@ -25,6 +25,7 @@ import java.util.TimeZone; import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.ApiKeyAuth; +import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.request.GetRequest; import {{invokerPackage}}.request.PostRequest; import {{invokerPackage}}.request.PutRequest; @@ -174,6 +175,9 @@ public class ApiInvoker { {{#isApiKey}} INSTANCE.authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}")); {{/isApiKey}} + {{#isBasic}} + INSTANCE.authentications.put("{{name}}", new HttpBasicAuth()); + {{/isBasic}} {{/authMethods}} // Prevent the authentications from being modified. INSTANCE.authentications = Collections.unmodifiableMap(INSTANCE.authentications); @@ -248,6 +252,31 @@ public class ApiInvoker { return authentications.get(authName); } + /** + * Helper method to set username for the first HTTP basic authentication. + */ + 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. + */ + 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. diff --git a/modules/swagger-codegen/src/main/resources/android-volley/auth/httpbasicauth.mustache b/modules/swagger-codegen/src/main/resources/android-volley/auth/httpbasicauth.mustache new file mode 100644 index 0000000000..64fc61f95c --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/android-volley/auth/httpbasicauth.mustache @@ -0,0 +1,35 @@ +package {{invokerPackage}}.auth; + +import {{invokerPackage}}.Pair; + +import android.util.Base64; + +import java.util.Map; +import java.util.List; + +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 applyToParams(List queryParams, Map headerParams) { + String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); + headerParams.put("Authorization", "Basic " + Base64.encodeToString(str.getBytes(), Base64.DEFAULT)); + } +}