Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.ibm.watson.developer_cloud.http.HttpStatus;
import com.ibm.watson.developer_cloud.http.RequestBuilder;
import com.ibm.watson.developer_cloud.service.model.GenericModel;
import com.ibm.watson.developer_cloud.util.BluemixUtils;
import com.ibm.watson.developer_cloud.util.CredentialUtils;
import com.ibm.watson.developer_cloud.util.RequestUtil;
import com.ibm.watson.developer_cloud.util.ResponseUtil;
import com.squareup.okhttp.Credentials;
Expand Down Expand Up @@ -65,7 +65,7 @@ public abstract class WatsonService {
*/
public WatsonService(String name) {
this.name = name;
this.apiKey = BluemixUtils.getAPIKey(name);
this.apiKey = CredentialUtils.getAPIKey(name);
this.client = configureHttpClient();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -25,9 +29,9 @@
import com.squareup.okhttp.Credentials;

/**
* The Class BluemixUtils.
* The Class CredentialUtils.
*/
public class BluemixUtils {
public class CredentialUtils {

/** The Constant ALCHEMY_API. */
private static final String ALCHEMY_API = "alchemy_api";
Expand All @@ -39,7 +43,7 @@ public class BluemixUtils {
private static final String CREDENTIALS = "credentials";

/** The Constant log. */
private static final Logger log = Logger.getLogger(BluemixUtils.class.getName());
private static final Logger log = Logger.getLogger(CredentialUtils.class.getName());

/** The Constant PASSWORD. */
private static final String PASSWORD = "password";
Expand Down Expand Up @@ -86,7 +90,7 @@ public static String getAPIKey(String serviceName, String plan) {

final JsonObject services = getVCAPServices();
if (services == null)
return null;
return getKeyUsingJNDI(serviceName);

for (final Entry<String, JsonElement> entry : services.entrySet()) {
final String key = entry.getKey();
Expand All @@ -111,6 +115,22 @@ public static String getAPIKey(String serviceName, String plan) {
return null;
}

/**
* Attempt to get the Base64-encoded API key through JNDI
* @param serviceName Name of the bluemix service
* @return The encoded API Key
*/
private static String getKeyUsingJNDI(String serviceName){
try{
Context context = new InitialContext();
String lookupName = "watson-developer-cloud/" + serviceName + "/credentials";
String apiKey = (String) context.lookup(lookupName);
return apiKey;
}catch(NamingException e){
//ignore
return null;
}
}
/**
* Gets the <b>VCAP_SERVICES</b> environment variable and return it as a {@link JsonObject}.
*
Expand Down Expand Up @@ -138,6 +158,6 @@ private static JsonObject getVCAPServices() {
* @param services the VCAP_SERVICES
*/
public static void setServices(String services) {
BluemixUtils.services = services;
CredentialUtils.services = services;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import com.ibm.watson.developer_cloud.WatsonServiceTest;

/**
* The Class BluemixUtilsTest.
* The Class CredentialUtilsTest.
*/
public class BluemixUtilsTest extends WatsonServiceTest {
public class CredentialUtilsTest extends WatsonServiceTest {

/** The Constant API_KEY_FREE. */
private static final String API_KEY_FREE =
Expand All @@ -48,7 +48,7 @@ public class BluemixUtilsTest extends WatsonServiceTest {
public void setup() {
final InputStream in = this.getClass().getClassLoader().getResourceAsStream(VCAP_SERVICES);
final String vcapServices = getStringFromInputStream(in);
BluemixUtils.setServices(vcapServices);
CredentialUtils.setServices(vcapServices);
}

/**
Expand All @@ -57,11 +57,11 @@ public void setup() {
*/
@Test
public void testGetAPIKeyWithNullOrEmptyService() {
assertNull(BluemixUtils.getAPIKey(null, null));
assertNull(BluemixUtils.getAPIKey("", ""));
assertNull(CredentialUtils.getAPIKey(null, null));
assertNull(CredentialUtils.getAPIKey("", ""));

assertEquals(API_KEY_FREE, BluemixUtils.getAPIKey(SERVICE_NAME, null));
assertEquals(API_KEY_FREE, BluemixUtils.getAPIKey(SERVICE_NAME, BluemixUtils.PLAN_FREE));
assertEquals(API_KEY_STANDARD, BluemixUtils.getAPIKey(SERVICE_NAME, BluemixUtils.PLAN_STANDARD));
assertEquals(API_KEY_FREE, CredentialUtils.getAPIKey(SERVICE_NAME, null));
assertEquals(API_KEY_FREE, CredentialUtils.getAPIKey(SERVICE_NAME, CredentialUtils.PLAN_FREE));
assertEquals(API_KEY_STANDARD, CredentialUtils.getAPIKey(SERVICE_NAME, CredentialUtils.PLAN_STANDARD));
}
}