Skip to content

Commit 4b0093d

Browse files
Merge pull request #164 from sreya92/master
Add jndi support for API Key lookup
2 parents 715dda2 + 4af269d commit 4b0093d

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.ibm.watson.developer_cloud.http.HttpStatus;
2929
import com.ibm.watson.developer_cloud.http.RequestBuilder;
3030
import com.ibm.watson.developer_cloud.service.model.GenericModel;
31-
import com.ibm.watson.developer_cloud.util.BluemixUtils;
31+
import com.ibm.watson.developer_cloud.util.CredentialUtils;
3232
import com.ibm.watson.developer_cloud.util.RequestUtil;
3333
import com.ibm.watson.developer_cloud.util.ResponseUtil;
3434
import com.squareup.okhttp.Credentials;
@@ -65,7 +65,7 @@ public abstract class WatsonService {
6565
*/
6666
public WatsonService(String name) {
6767
this.name = name;
68-
this.apiKey = BluemixUtils.getAPIKey(name);
68+
this.apiKey = CredentialUtils.getAPIKey(name);
6969
this.client = configureHttpClient();
7070
}
7171

src/main/java/com/ibm/watson/developer_cloud/util/BluemixUtils.java renamed to src/main/java/com/ibm/watson/developer_cloud/util/CredentialUtils.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import java.util.logging.Level;
1818
import java.util.logging.Logger;
1919

20+
import javax.naming.Context;
21+
import javax.naming.InitialContext;
22+
import javax.naming.NamingException;
23+
2024
import com.google.gson.JsonArray;
2125
import com.google.gson.JsonElement;
2226
import com.google.gson.JsonObject;
@@ -25,9 +29,9 @@
2529
import com.squareup.okhttp.Credentials;
2630

2731
/**
28-
* The Class BluemixUtils.
32+
* The Class CredentialUtils.
2933
*/
30-
public class BluemixUtils {
34+
public class CredentialUtils {
3135

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

4145
/** The Constant log. */
42-
private static final Logger log = Logger.getLogger(BluemixUtils.class.getName());
46+
private static final Logger log = Logger.getLogger(CredentialUtils.class.getName());
4347

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

8791
final JsonObject services = getVCAPServices();
8892
if (services == null)
89-
return null;
93+
return getKeyUsingJNDI(serviceName);
9094

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

118+
/**
119+
* Attempt to get the Base64-encoded API key through JNDI
120+
* @param serviceName Name of the bluemix service
121+
* @return The encoded API Key
122+
*/
123+
private static String getKeyUsingJNDI(String serviceName){
124+
try{
125+
Context context = new InitialContext();
126+
String lookupName = "watson-developer-cloud/" + serviceName + "/credentials";
127+
String apiKey = (String) context.lookup(lookupName);
128+
return apiKey;
129+
}catch(NamingException e){
130+
//ignore
131+
return null;
132+
}
133+
}
114134
/**
115135
* Gets the <b>VCAP_SERVICES</b> environment variable and return it as a {@link JsonObject}.
116136
*
@@ -138,6 +158,6 @@ private static JsonObject getVCAPServices() {
138158
* @param services the VCAP_SERVICES
139159
*/
140160
public static void setServices(String services) {
141-
BluemixUtils.services = services;
161+
CredentialUtils.services = services;
142162
}
143163
}

src/test/java/com/ibm/watson/developer_cloud/util/BluemixUtilsTest.java renamed to src/test/java/com/ibm/watson/developer_cloud/util/CredentialUtilsTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import com.ibm.watson.developer_cloud.WatsonServiceTest;
2525

2626
/**
27-
* The Class BluemixUtilsTest.
27+
* The Class CredentialUtilsTest.
2828
*/
29-
public class BluemixUtilsTest extends WatsonServiceTest {
29+
public class CredentialUtilsTest extends WatsonServiceTest {
3030

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

5454
/**
@@ -57,11 +57,11 @@ public void setup() {
5757
*/
5858
@Test
5959
public void testGetAPIKeyWithNullOrEmptyService() {
60-
assertNull(BluemixUtils.getAPIKey(null, null));
61-
assertNull(BluemixUtils.getAPIKey("", ""));
60+
assertNull(CredentialUtils.getAPIKey(null, null));
61+
assertNull(CredentialUtils.getAPIKey("", ""));
6262

63-
assertEquals(API_KEY_FREE, BluemixUtils.getAPIKey(SERVICE_NAME, null));
64-
assertEquals(API_KEY_FREE, BluemixUtils.getAPIKey(SERVICE_NAME, BluemixUtils.PLAN_FREE));
65-
assertEquals(API_KEY_STANDARD, BluemixUtils.getAPIKey(SERVICE_NAME, BluemixUtils.PLAN_STANDARD));
63+
assertEquals(API_KEY_FREE, CredentialUtils.getAPIKey(SERVICE_NAME, null));
64+
assertEquals(API_KEY_FREE, CredentialUtils.getAPIKey(SERVICE_NAME, CredentialUtils.PLAN_FREE));
65+
assertEquals(API_KEY_STANDARD, CredentialUtils.getAPIKey(SERVICE_NAME, CredentialUtils.PLAN_STANDARD));
6666
}
6767
}

0 commit comments

Comments
 (0)