diff --git a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache index 9a6f4215481..910728c4dd5 100644 --- a/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache @@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family; import javax.ws.rs.core.MediaType; import java.util.Collection; +import java.util.Collections; import java.util.Map; import java.util.Map.Entry; import java.util.HashMap; @@ -34,6 +35,9 @@ import java.text.SimpleDateFormat; import java.text.ParseException; import {{invokerPackage}}.auth.Authentication; +import {{invokerPackage}}.auth.HttpBasicAuth; +import {{invokerPackage}}.auth.ApiKeyAuth; +import {{invokerPackage}}.auth.OAuth; public class ApiClient { private Map hostMap = new HashMap(); @@ -41,6 +45,8 @@ public class ApiClient { private boolean debugging = false; private String basePath = "{{basePath}}"; + private Map authentications; + private DateFormat dateFormat; public ApiClient() { @@ -53,6 +59,14 @@ public class ApiClient { // Set default User-Agent. setUserAgent("Java-Swagger"); + + // 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); } public String getBasePath() { @@ -64,6 +78,75 @@ public class ApiClient { return this; } + /** + * Get authentications (key: authentication name, value: 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. + */ + 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. + */ + 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. + */ + 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!"); + } + /** * Set the User-Agent header's value (by adding to the default header map). */ @@ -340,8 +423,8 @@ public class ApiClient { */ private void updateParamsForAuth(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); + Authentication auth = authentications.get(authName); + if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); auth.applyToParams(queryParams, headerParams); } } diff --git a/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache b/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache index a09e34b755c..e936b423a91 100644 --- a/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/Configuration.mustache @@ -1,13 +1,5 @@ package {{invokerPackage}}; -import java.util.Map; -import java.util.HashMap; - -import {{invokerPackage}}.auth.Authentication; -import {{invokerPackage}}.auth.HttpBasicAuth; -import {{invokerPackage}}.auth.ApiKeyAuth; -import {{invokerPackage}}.auth.OAuth; - public class Configuration { private static ApiClient defaultApiClient = new ApiClient(); @@ -26,64 +18,4 @@ public class Configuration { public static void setDefaultApiClient(ApiClient apiClient) { defaultApiClient = apiClient; } - - private static final Map AUTH; - - static { - // setup authentications - 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}}AUTH.put("{{name}}", new OAuth());{{/isOAuth}} - {{/authMethods}} - } - - public static Authentication getAuthentication(String authName) { - return AUTH.get(authName); - } - - /** Set username for the first HTTP basic authentication. */ - public static void setUsername(String username) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setUsername(username); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - - /** Set password for the first HTTP basic authentication. */ - public static void setPassword(String password) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setPassword(password); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - - /** Set API key value for the first API key authentication. */ - public static void setApiKey(String apiKey) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof ApiKeyAuth) { - ((ApiKeyAuth) auth).setApiKey(apiKey); - return; - } - } - throw new RuntimeException("No API key authentication configured!"); - } - - /** Set API key prefix for the first API key authentication. */ - public static void setApiKeyPrefix(String apiKeyPrefix) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof ApiKeyAuth) { - ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); - return; - } - } - throw new RuntimeException("No API key authentication configured!"); - } } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java index e6e2a308456..61d296537e6 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java @@ -17,6 +17,7 @@ import javax.ws.rs.core.Response.Status.Family; import javax.ws.rs.core.MediaType; import java.util.Collection; +import java.util.Collections; import java.util.Map; import java.util.Map.Entry; import java.util.HashMap; @@ -34,6 +35,9 @@ import java.text.SimpleDateFormat; import java.text.ParseException; import io.swagger.client.auth.Authentication; +import io.swagger.client.auth.HttpBasicAuth; +import io.swagger.client.auth.ApiKeyAuth; +import io.swagger.client.auth.OAuth; public class ApiClient { private Map hostMap = new HashMap(); @@ -41,6 +45,8 @@ public class ApiClient { private boolean debugging = false; private String basePath = "http://petstore.swagger.io/v2"; + private Map authentications; + private DateFormat dateFormat; public ApiClient() { @@ -53,6 +59,13 @@ public class ApiClient { // Set default User-Agent. setUserAgent("Java-Swagger"); + + // 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()); + // Prevent the authentications from being modified. + authentications = Collections.unmodifiableMap(authentications); } public String getBasePath() { @@ -64,6 +77,75 @@ public class ApiClient { return this; } + /** + * Get authentications (key: authentication name, value: 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. + */ + 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. + */ + 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. + */ + 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!"); + } + /** * Set the User-Agent header's value (by adding to the default header map). */ @@ -340,8 +422,8 @@ public class ApiClient { */ private void updateParamsForAuth(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); + Authentication auth = authentications.get(authName); + if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); auth.applyToParams(queryParams, headerParams); } } diff --git a/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java index 88192cbd3bf..04899a110f6 100644 --- a/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java +++ b/samples/client/petstore/java/src/main/java/io/swagger/client/Configuration.java @@ -1,13 +1,5 @@ package io.swagger.client; -import java.util.Map; -import java.util.HashMap; - -import io.swagger.client.auth.Authentication; -import io.swagger.client.auth.HttpBasicAuth; -import io.swagger.client.auth.ApiKeyAuth; -import io.swagger.client.auth.OAuth; - public class Configuration { private static ApiClient defaultApiClient = new ApiClient(); @@ -26,68 +18,4 @@ public class Configuration { public static void setDefaultApiClient(ApiClient apiClient) { defaultApiClient = apiClient; } - - private static final Map AUTH; - - static { - // setup authentications - AUTH = new HashMap(); - - - AUTH.put("api_key", new ApiKeyAuth("header", "api_key")); - - - - - AUTH.put("petstore_auth", new OAuth()); - - } - - public static Authentication getAuthentication(String authName) { - return AUTH.get(authName); - } - - /** Set username for the first HTTP basic authentication. */ - public static void setUsername(String username) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setUsername(username); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - - /** Set password for the first HTTP basic authentication. */ - public static void setPassword(String password) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setPassword(password); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - - /** Set API key value for the first API key authentication. */ - public static void setApiKey(String apiKey) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof ApiKeyAuth) { - ((ApiKeyAuth) auth).setApiKey(apiKey); - return; - } - } - throw new RuntimeException("No API key authentication configured!"); - } - - /** Set API key prefix for the first API key authentication. */ - public static void setApiKeyPrefix(String apiKeyPrefix) { - for (Authentication auth : AUTH.values()) { - if (auth instanceof ApiKeyAuth) { - ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); - return; - } - } - throw new RuntimeException("No API key authentication configured!"); - } } diff --git a/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java b/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java new file mode 100644 index 00000000000..82f39e90583 --- /dev/null +++ b/samples/client/petstore/java/src/test/java/io/swagger/client/ApiClientTest.java @@ -0,0 +1,75 @@ +package io.swagger.client; + +import io.swagger.client.auth.*; + +import java.util.Map; + +import static org.junit.Assert.*; +import org.junit.*; + +public class ApiClientTest { + ApiClient apiClient = null; + + @Before + public void setup() { + apiClient = new ApiClient(); + } + + @Test + public void testGetAuthentications() { + Map auths = apiClient.getAuthentications(); + + Authentication auth = auths.get("api_key"); + assertNotNull(auth); + assertTrue(auth instanceof ApiKeyAuth); + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth; + assertEquals("header", apiKeyAuth.getLocation()); + assertEquals("api_key", apiKeyAuth.getParamName()); + + auth = auths.get("petstore_auth"); + assertTrue(auth instanceof OAuth); + assertSame(auth, apiClient.getAuthentication("petstore_auth")); + + assertNull(auths.get("unknown")); + + try { + auths.put("my_auth", new HttpBasicAuth()); + fail("the authentications returned should not be modifiable"); + } catch (UnsupportedOperationException e) { + } + } + + @Test + public void testSetUsername() { + try { + apiClient.setUsername("my-username"); + fail("there should be no HTTP basic authentications"); + } catch (RuntimeException e) { + } + } + + @Test + public void testSetPassword() { + try { + apiClient.setPassword("my-password"); + fail("there should be no HTTP basic authentications"); + } catch (RuntimeException e) { + } + } + + @Test + public void testSetApiKeyAndPrefix() { + ApiKeyAuth auth = (ApiKeyAuth) apiClient.getAuthentications().get("api_key"); + auth.setApiKey(null); + auth.setApiKeyPrefix(null); + + apiClient.setApiKey("my-api-key"); + apiClient.setApiKeyPrefix("Token"); + assertEquals("my-api-key", auth.getApiKey()); + assertEquals("Token", auth.getApiKeyPrefix()); + + // reset values + auth.setApiKey(null); + auth.setApiKeyPrefix(null); + } +} diff --git a/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java b/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java index 3838dcb21bb..9801be416fb 100644 --- a/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java +++ b/samples/client/petstore/java/src/test/java/io/swagger/client/ConfigurationTest.java @@ -1,59 +1,9 @@ package io.swagger.client; -import io.swagger.client.auth.*; - import static org.junit.Assert.*; import org.junit.*; public class ConfigurationTest { - @Test - public void testGetAuthentication() { - Authentication auth = Configuration.getAuthentication("api_key"); - assertNotNull(auth); - assertTrue(auth instanceof ApiKeyAuth); - ApiKeyAuth apiKeyAuth = (ApiKeyAuth) auth; - assertEquals("header", apiKeyAuth.getLocation()); - assertEquals("api_key", apiKeyAuth.getParamName()); - - auth = Configuration.getAuthentication("petstore_auth"); - assertTrue(auth instanceof OAuth); - - assertNull(Configuration.getAuthentication("unknown")); - } - - @Test - public void testSetUsername() { - try { - Configuration.setUsername("my-username"); - fail("should throw RuntimeException"); - } catch (RuntimeException e) { - } - } - - @Test - public void testSetPassword() { - try { - Configuration.setPassword("my-password"); - fail("should throw RuntimeException"); - } catch (RuntimeException e) { - } - } - - @Test - public void testSetApiKeyAndPrefix() { - ApiKeyAuth auth = (ApiKeyAuth) Configuration.getAuthentication("api_key"); - auth.setApiKey(null); - auth.setApiKeyPrefix(null); - - Configuration.setApiKey("my-api-key"); - Configuration.setApiKeyPrefix("Token"); - assertEquals("my-api-key", auth.getApiKey()); - assertEquals("Token", auth.getApiKeyPrefix()); - - auth.setApiKey(null); - auth.setApiKeyPrefix(null); - } - @Test public void testDefaultApiClient() { ApiClient apiClient = Configuration.getDefaultApiClient(); diff --git a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/PetApiTest.java index b1316b7a8b4..854af7121da 100644 --- a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -16,15 +16,12 @@ import org.junit.*; public class PetApiTest { PetApi api = null; - @BeforeClass - public static void initAuth() { - ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key"); - apiKeyAuth.setApiKey("special-key"); - } - @Before public void setup() { api = new PetApi(); + // setup authentication + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); + apiKeyAuth.setApiKey("special-key"); } @Test diff --git a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/StoreApiTest.java b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/StoreApiTest.java index c5e2a9b8719..1499c4eaf86 100644 --- a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/StoreApiTest.java +++ b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/StoreApiTest.java @@ -15,15 +15,12 @@ import org.junit.*; public class StoreApiTest { StoreApi api = null; - @BeforeClass - public static void initAuth() { - ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key"); - apiKeyAuth.setApiKey("special-key"); - } - @Before public void setup() { api = new StoreApi(); + // setup authentication + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); + apiKeyAuth.setApiKey("special-key"); } @Test @@ -73,4 +70,4 @@ public class StoreApiTest { return order; } -} \ No newline at end of file +} diff --git a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/UserApiTest.java b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/UserApiTest.java index 8fcb621b85e..e3bccd0dcdb 100644 --- a/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/UserApiTest.java +++ b/samples/client/petstore/java/src/test/java/io/swagger/petstore/test/UserApiTest.java @@ -15,15 +15,12 @@ import org.junit.*; public class UserApiTest { UserApi api = null; - @BeforeClass - public static void initAuth() { - ApiKeyAuth apiKeyAuth = (ApiKeyAuth) Configuration.getAuthentication("api_key"); - apiKeyAuth.setApiKey("special-key"); - } - @Before public void setup() { api = new UserApi(); + // setup authentication + ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); + apiKeyAuth.setApiKey("special-key"); } @Test @@ -89,4 +86,4 @@ public class UserApiTest { return user; } -} \ No newline at end of file +}