Add support of HTTP basic and API key auth to Java codegen

This commit is contained in:
xhh 2015-05-21 22:05:46 +08:00
parent 0c3f7a54cd
commit 3d4b5a10c9
7 changed files with 148 additions and 9 deletions

View File

@ -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<String>(
Arrays.asList(

View File

@ -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}};
}

View File

@ -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<String, Client> hostMap = new HashMap<String, Client>();
@ -162,11 +164,12 @@ public class ApiInvoker {
}
}
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException {
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> 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<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);
auth.processParams(queryParams, headerParams);
}
}
private Client getClient(String host) {
if(!hostMap.containsKey(host)) {
Client client = Client.create();

View File

@ -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<String, String> queryParams, Map<String, String> 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);
}
}
}

View File

@ -0,0 +1,7 @@
package {{invokerPackage}}.auth;
import java.util.Map;
public interface Authentication {
void processParams(Map<String, String> queryParams, Map<String, String> headerParams);
}

View File

@ -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<String, String> queryParams, Map<String, String> headerParams) {
try {
headerParams.put("Authorization", "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -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<String, Authentication> AUTH;
static {
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}}// TODO: support oauth{{/isOAuth}}
{{/authMethods}}
}
public static Authentication getAuthentication(String authName) {
return AUTH.get(authName);
}
}