Skip to content

Commit

Permalink
Rebuild Java Jersey2 sample client
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Jan 15, 2020
1 parent 9bce846 commit 01b7d89
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class ApiClient {
protected String tempFolderPath = null;

protected Map<String, Authentication> authentications;
protected Map<String, String> authenticationLookup;

protected DateFormat dateFormat;

Expand All @@ -85,6 +86,9 @@ public ApiClient() {
authentications.put("petstore_auth", new OAuth());
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);

// Setup authentication lookup (key: authentication alias, value: authentication name)
authenticationLookup = new HashMap<String, String>();
}

/**
Expand Down Expand Up @@ -173,6 +177,28 @@ public void setApiKey(String apiKey) {
throw new RuntimeException("No API key authentication configured!");
}

/**
* Helper method to configure authentications.
*
* NOTE: This method respects API key aliases using "x-lookup" property
* from OpenAPI specification.
*
* @param secrets Hash map from authentication name to its secret.
*/
public void configureApiKeys(HashMap<String, String> secrets) {
for (Map.Entry<String, Authentication> authEntry : authentications.entrySet()) {
Authentication auth = authEntry.getValue();
if (auth instanceof ApiKeyAuth) {
String name = authEntry.getKey();
// respect x-lookup property
name = authenticationLookup.getOrDefault(name, name);
if (secrets.containsKey(name)) {
((ApiKeyAuth) auth).setApiKey(secrets.get(name));
}
}
}
}

/**
* Helper method to set API key prefix for the first API key authentication.
* @param apiKeyPrefix API key prefix
Expand Down

0 comments on commit 01b7d89

Please sign in to comment.