Move authentications into ApiClient

This commit is contained in:
xhh
2015-06-05 20:00:32 +08:00
parent 947935f3d9
commit fffc5d7c35
9 changed files with 255 additions and 214 deletions

View File

@@ -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<String, Client> hostMap = new HashMap<String, Client>();
@@ -41,6 +45,8 @@ public class ApiClient {
private boolean debugging = false;
private String basePath = "{{basePath}}";
private Map<String, Authentication> 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<String, Authentication>();{{#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<String, Authentication> 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<String, String> queryParams, Map<String, String> 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);
}
}

View File

@@ -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<String, Authentication> AUTH;
static {
// setup authentications
AUTH = new HashMap<String, Authentication>();
{{#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!");
}
}