Skip to content

Commit 19c2b36

Browse files
committed
Retrieve service URL from JDNI
1 parent e62b234 commit 19c2b36

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

core/src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,31 @@ public String getUsername() {
109109
/** The Constant API_KEY. */
110110
private static final String API_KEY = "api_key";
111111

112+
/** The Constant LOOKUP_NAME_EXTENSION_API_KEY. */
113+
private static final String LOOKUP_NAME_EXTENSION_API_KEY = "/credentials";
114+
115+
/** The Constant LOOKUP_NAME_EXTENSION_URL. */
116+
private static final String LOOKUP_NAME_EXTENSION_URL = "/url";
117+
112118
private CredentialUtils() {
113119
// This is a utility class - no instantiation allowed.
114120
}
115121

116122
/**
117-
* Attempt to get the Base64-encoded API key through JNDI.
123+
* Attempt to get the Base64-encoded value through JNDI.
118124
*
119125
* This method should always return null on Android due to the javax functions being unsupported
120126
*
121127
* @param serviceName Name of the bluemix service
122-
* @return The encoded API Key
128+
* @param lookupNameExtension Extension to determine which value should be retrieved through JDNI
129+
* @return The encoded desired value
123130
*/
124-
private static String getKeyUsingJNDI(String serviceName) {
131+
private static String getJDNIValue(String serviceName, String lookupNameExtension) {
125132
if (!isClassAvailable("javax.naming.Context") || !isClassAvailable("javax.naming.InitialContext")) {
126133
log.info("JNDI string lookups is not available.");
127134
return null;
128135
}
129-
String lookupName = "watson-developer-cloud/" + serviceName + "/credentials";
136+
String lookupName = "watson-developer-cloud/" + serviceName + lookupNameExtension;
130137
try {
131138
Context context = new InitialContext();
132139
return (String) context.lookup(lookupName);
@@ -192,7 +199,7 @@ public static String getAPIKey(String serviceName, String plan) {
192199

193200
final JsonObject services = getVCAPServices();
194201
if (services == null) {
195-
return getKeyUsingJNDI(serviceName);
202+
return getJDNIValue(serviceName, LOOKUP_NAME_EXTENSION_API_KEY);
196203
}
197204
if (serviceName.equalsIgnoreCase(ALCHEMY_API)) {
198205
final JsonObject credentials = getCredentialsObject(services, serviceName, plan);
@@ -299,12 +306,12 @@ public static String getAPIUrl(String serviceName) {
299306
}
300307

301308
/**
302-
* Returns the apiKey from the VCAP_SERVICES or null if doesn't exists. If plan is specified, then only credentials
303-
* for the given plan will be returned.
309+
* Returns the API URL from the VCAP_SERVICES, JDNI, or null if doesn't exists. If plan is specified, then only
310+
* credentials for the given plan will be returned.
304311
*
305312
* @param serviceName the service name
306313
* @param plan the service plan: standard, free or experimental
307-
* @return the API key
314+
* @return the API URL
308315
*/
309316
public static String getAPIUrl(String serviceName, String plan) {
310317
if ((serviceName == null) || serviceName.isEmpty()) {
@@ -313,7 +320,7 @@ public static String getAPIUrl(String serviceName, String plan) {
313320

314321
final JsonObject services = getVCAPServices();
315322
if (services == null) {
316-
return null;
323+
return getJDNIValue(serviceName, LOOKUP_NAME_EXTENSION_URL);
317324
}
318325

319326
final JsonObject credentials = getCredentialsObject(services, serviceName, plan);

0 commit comments

Comments
 (0)