+ * Wraps all session management, such as renewal etc. + * + * @return + * @throws RestApiException + */ + public String getBhRestToken() throws RestApiException { + + if (isSessionExpired() && !this.isSessionProvided) { + createSession(); + } + + return bhRestToken; + } + + /** + * Refreshes the BhRestToken, expired or not expired, and returns the brand new BhRestToken to be used when making rest api calls. + *
+ * Wraps all session management, such as renewal etc.
+ *
+ * @return
+ * @throws RestApiException
+ */
+ public String refreshBhRestToken() throws RestApiException {
+
+ createSession();
+
+ return bhRestToken;
+ }
+
+ private void createSession() {
+ for (int tryNumber = 1; tryNumber <= SESSION_RETRY; tryNumber++) {
+ try {
+ String authCode = getAuthorizationCode();
+ getAccessToken(authCode);
+ login();
+ break;
+ } catch (Exception e) {
+
+ if (tryNumber < SESSION_RETRY) {
+ log.error("Error creating REST session. Try number: " + tryNumber + " out of " + SESSION_RETRY + " trying again.", e);
+ } else {
+ log.error("Final error creating REST session. Shutting down.", e);
+ throw new RestApiException("Failed to create rest session", e);
+ }
+ }
+ }
+ }
+
+ private String getAuthorizationCode() throws RestApiException {
+ String authorizeUrl = restCredentials.getRestAuthorizeUrl();
+ String clientId = restCredentials.getRestClientId();
+ String username = getUserName();
+ String password = getPassword();
+ String authCode = null;
+
+ String url = authorizeUrl
+ + "?client_id={clientId}&response_type={responseType}&action={action}&username={username}&password={password}";
+
+ Map
+ * Pinging every time will decrease performance.
+ *
+ * @return
+ * @throws RestApiException
+ */
+ public boolean isSessionExpired() throws RestApiException {
+ boolean sessionExpired = false;
+
+ if (bhRestTokenExpired()) {
+ // sessionExpired = ping();
+ sessionExpired = true;
+ }
+
+ return sessionExpired;
+ }
+
+ /**
+ * Uses the DateTime in this class to calculate if the session is expired
+ *
+ * @return
+ */
+
+ private boolean bhRestTokenExpired() {
+ if (dateTimeBhRestTokenWillExpire.isBeforeNow()) {
+ return true;
+ }
+ return false;
+
+ }
+
+ public String getRestUrl() {
+ return restUrl;
+ }
+
+ private synchronized void setBhRestToken(String bhRestToken) {
+ this.bhRestToken = bhRestToken;
+
+ if (!this.isSessionProvided) {
+ updateDateTimeBhRestTokenWillExpire();
+ }
+
+ }
+
+ private void updateDateTimeBhRestTokenWillExpire() {
+ // set the DateTime the session will expire, subtracting one minute to be on the safe side.
+ DateTime timeToExpire = getNow();
+ int sessionMinutesToLive = Integer.valueOf(restCredentials.getRestSessionMinutesToLive());
+ if (sessionMinutesToLive > MAX_TTL) {
+ sessionMinutesToLive = MAX_TTL;
+ }
+ timeToExpire = timeToExpire.plusMinutes(sessionMinutesToLive - 1);
+ this.dateTimeBhRestTokenWillExpire = timeToExpire;
+ }
+
+ private DateTime getNow() {
+ return new DateTime(DateTimeZone.forID("EST5EDT"));
+ }
+
+ public DateTime getDateTimeBhRestTokenWillExpire() {
+ return dateTimeBhRestTokenWillExpire;
+ }
+
+ public void setDateTimeBhRestTokenWillExpire(DateTime dateTimeBhRestTokenWillExpire) {
+ this.dateTimeBhRestTokenWillExpire = dateTimeBhRestTokenWillExpire;
+ }
+
+ public boolean isSessionProvided(BullhornRestCredentials restCredentials) {
+ return StringUtils.isNotBlank(restCredentials.getRestUrl()) && StringUtils.isNotBlank(restCredentials.getBhRestToken());
+ }
+
+ /**
+ * Will return the un-encrypted RestCredentials for this RestApiSession. Note that this is only needed for a multi-tenant solution
+ *
+ * @return a valid {@link RestCredentials} object if multi-tenant otherwise null
+ */
+ public BullhornRestCredentials getRestCredentials() {
+ return restCredentials;
+ }
}
diff --git a/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java b/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java
new file mode 100644
index 00000000..2289dc7d
--- /dev/null
+++ b/src/test/java/com/bullhornsdk/data/api/helper/TestCredentiallessRestApiSession.java
@@ -0,0 +1,82 @@
+package com.bullhornsdk.data.api.helper;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import com.bullhornsdk.data.BaseTest;
+import com.bullhornsdk.data.api.BullhornRestCredentials;
+
+import static org.junit.Assert.*;
+
+@Ignore
+public class TestCredentiallessRestApiSession extends BaseTest {
+
+ private RestApiSession restApiSession;
+
+ private BullhornRestCredentials bullhornRestCredentials;
+
+ /**
+ * Add your own rest credentials here to test the connection.
+ */
+ @Before
+ public void setupTheCredentials() {
+ BullhornRestCredentials creds = new BullhornRestCredentials();
+
+ creds.setRestUrl("MY-RESTURL");
+ creds.setBhRestToken("MY-BHRESTTOKEN");
+ this.bullhornRestCredentials = creds;
+ this.restApiSession = new RestApiSession(bullhornRestCredentials);
+ }
+
+ @Test
+ public void testRestApiSession() {
+
+ RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
+ assertNotNull("RestApiSession is null", restApiSessionManual);
+ assertNull("Username is not null", bullhornRestCredentials.getUsername());
+ assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
+ assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
+ assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
+ assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
+ assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
+ assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
+ }
+
+ @Test
+ public void testGetBhRestToken() {
+
+ String restToken = restApiSession.getBhRestToken();
+ assertNotNull("BhRestToken is null", restToken);
+ assertNull("Username is not null", bullhornRestCredentials.getUsername());
+ assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
+ assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
+ assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
+ assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
+ assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
+ assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
+ }
+
+ @Test
+ public void testGetRestUrl() {
+
+ String restUrl = restApiSession.getRestUrl();
+ assertNotNull("restUrl is null", restUrl);
+ assertNull("Username is not null", bullhornRestCredentials.getUsername());
+ assertNull("RestTokenUrl is not null", bullhornRestCredentials.getRestTokenUrl());
+ assertNull("RestSessionMinutesToLive is not null", bullhornRestCredentials.getRestSessionMinutesToLive());
+ assertNull("RestLoginUrl is not null", bullhornRestCredentials.getRestLoginUrl());
+ assertNull("RestClientSecret is not null", bullhornRestCredentials.getRestClientSecret());
+ assertNull("RestClientId is not null", bullhornRestCredentials.getRestClientId());
+ assertNull("RestAuthorizeUrl is not null", bullhornRestCredentials.getRestAuthorizeUrl());
+ }
+
+ @Test
+ public void testHasNoSessionProvided() {
+
+ boolean hasNoSession = restApiSession.isSessionProvided(bullhornRestCredentials);
+ assertTrue(hasNoSession);
+
+ }
+
+}
diff --git a/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java b/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java
index 30f4e294..501d863f 100644
--- a/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java
+++ b/src/test/java/com/bullhornsdk/data/api/helper/TestRestApiSession.java
@@ -1,13 +1,11 @@
package com.bullhornsdk.data.api.helper;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
import java.io.IOException;
+import com.bullhornsdk.data.exception.RestApiException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -17,132 +15,177 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
+import static org.junit.Assert.*;
+
+import mockit.Expectations;
+import mockit.Injectable;
+import mockit.Mocked;
+import mockit.Verifications;
+
@Ignore
public class TestRestApiSession extends BaseTest {
- private RestApiSession restApiSession;
+ private RestApiSession restApiSession;
+
+ private BullhornRestCredentials bullhornRestCredentials;
+
+ /**
+ * Add your own rest credentials here to test the connection.
+ */
+ @Before
+ public void setupTheCredentials() {
+ BullhornRestCredentials creds = new BullhornRestCredentials();
+
+ creds.setRestAuthorizeUrl("https://auth9.bullhornstaffing.com/oauth/authorize");
+ creds.setRestClientId("MY-CLIENT-ID");
+ creds.setRestClientSecret("MY-CLIENT-SECRET");
+ creds.setRestLoginUrl("https://rest9.bullhornstaffing.com/rest-services/login");
+ creds.setRestSessionMinutesToLive("1400");
+ creds.setRestTokenUrl("https://auth9.bullhornstaffing.com/oauth/token");
+ creds.setUsername("MY-USERNAME");
+ creds.setPassword("MY-PASSWORD");
+ this.bullhornRestCredentials = creds;
+ this.restApiSession = new RestApiSession(bullhornRestCredentials);
+ }
+
+ @Test
+ public void testRestApiSession() {
+
+ RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
+ assertNotNull("RestApiSession is null", restApiSessionManual);
+ }
+
+ @Test
+ public void testGetBhRestToken() {
+
+ String restToken = restApiSession.getBhRestToken();
+ assertNotNull("BhRestToken is null", restToken);
+ }
+
+ @Test
+ public void testGetRestUrl() {
+
+ String restUrl = restApiSession.getRestUrl();
+ assertNotNull("restUrl is null", restUrl);
+ }
- private BullhornRestCredentials bullhornRestCredentials;
+ @Test
+ public void testSessionExpired_1() throws InterruptedException {
- /**
- * Add your own rest credentials here to test the connection.
- */
- @Before
- public void setupTheCredentials() {
- BullhornRestCredentials creds = new BullhornRestCredentials();
+ bullhornRestCredentials.setRestSessionMinutesToLive("1");
- creds.setRestAuthorizeUrl("https://auth9.bullhornstaffing.com/oauth/authorize");
- creds.setRestClientId("MY-CLIENT-ID");
- creds.setRestClientSecret("MY-CLIENT-SECRET");
- creds.setRestLoginUrl("https://rest9.bullhornstaffing.com/rest-services/login");
- creds.setRestSessionMinutesToLive("1400");
- creds.setRestTokenUrl("https://auth9.bullhornstaffing.com/oauth/token");
- creds.setUsername("MY-USERNAME");
- creds.setPassword("MY-PASSWORD");
- this.bullhornRestCredentials = creds;
- this.restApiSession = new RestApiSession(bullhornRestCredentials);
- }
+ RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
+ Thread.sleep(5000);
+ assertTrue("session should have expired is null", restApiSessionManual.isSessionExpired());
- @Test
- public void testRestApiSession() {
+ String restToken = restApiSessionManual.getBhRestToken();
+ assertNotNull("BhRestToken is null", restToken);
- RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
- assertNotNull("RestApiSession is null", restApiSessionManual);
- }
+ String restUrl = restApiSessionManual.getRestUrl();
+ assertNotNull("restUrl is null", restUrl);
- @Test
- public void testGetBhRestToken() {
+ }
- String restToken = restApiSession.getBhRestToken();
- assertNotNull("BhRestToken is null", restToken);
- }
+ @Test
+ public void testSessionExpired_2() {
- @Test
- public void testGetRestUrl() {
+ bullhornRestCredentials.setRestSessionMinutesToLive("5000");
- String restUrl = restApiSession.getRestUrl();
- assertNotNull("restUrl is null", restUrl);
- }
+ RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
- @Test
- public void testSessionExpired_1() throws InterruptedException {
+ assertFalse("session should not be expired", restApiSessionManual.isSessionExpired());
- bullhornRestCredentials.setRestSessionMinutesToLive("1");
+ String restToken = restApiSessionManual.getBhRestToken();
+ assertNotNull("BhRestToken is null", restToken);
- RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
- Thread.sleep(5000);
- assertTrue("session should have expired is null", restApiSessionManual.isSessionExpired());
+ String restUrl = restApiSessionManual.getRestUrl();
+ assertNotNull("restUrl is null", restUrl);
- String restToken = restApiSessionManual.getBhRestToken();
- assertNotNull("BhRestToken is null", restToken);
+ DateTime expirationDate = restApiSessionManual.getDateTimeBhRestTokenWillExpire();
+ DateTime expirationDateManual = getNow().plusMinutes(RestApiSession.MAX_TTL - 1);
+ assertTrue(expirationDate.getDayOfYear() == expirationDateManual.getDayOfYear());
+ assertTrue(expirationDate.getHourOfDay() == expirationDateManual.getHourOfDay());
+ assertTrue(expirationDate.getMinuteOfDay() == expirationDateManual.getMinuteOfDay());
+ assertTrue(expirationDate.getSecondOfDay() == expirationDateManual.getSecondOfDay());
- String restUrl = restApiSessionManual.getRestUrl();
- assertNotNull("restUrl is null", restUrl);
+ }
- }
+ @Test
+ public void testRefreshBhRestToken() throws InterruptedException {
- @Test
- public void testSessionExpired_2() {
+ bullhornRestCredentials.setRestSessionMinutesToLive("1");
- bullhornRestCredentials.setRestSessionMinutesToLive("5000");
+ RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
+ String restToken = restApiSessionManual.getBhRestToken();
+ assertNotNull("restToken is null", restToken);
+ Thread.sleep(5000);
+ assertTrue("session should have expired is null", restApiSessionManual.isSessionExpired());
- RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
+ String newRestToken = restApiSessionManual.refreshBhRestToken();
+ assertNotNull("newRestToken is null", newRestToken);
- assertFalse("session should not be expired", restApiSessionManual.isSessionExpired());
+ assertFalse("new BhRestToken is the same as old BhRestToken", restToken.equals(newRestToken));
- String restToken = restApiSessionManual.getBhRestToken();
- assertNotNull("BhRestToken is null", restToken);
+ String restUrl = restApiSessionManual.getRestUrl();
+ assertNotNull("restUrl is null", restUrl);
- String restUrl = restApiSessionManual.getRestUrl();
- assertNotNull("restUrl is null", restUrl);
+ }
- DateTime expirationDate = restApiSessionManual.getDateTimeBhRestTokenWillExpire();
- DateTime expirationDateManual = getNow().plusMinutes(RestApiSession.MAX_TTL - 1);
- assertTrue(expirationDate.getDayOfYear() == expirationDateManual.getDayOfYear());
- assertTrue(expirationDate.getHourOfDay() == expirationDateManual.getHourOfDay());
- assertTrue(expirationDate.getMinuteOfDay() == expirationDateManual.getMinuteOfDay());
- assertTrue(expirationDate.getSecondOfDay() == expirationDateManual.getSecondOfDay());
+ @Test
+ public void testSessionSerialization() throws IOException {
- }
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.registerModule(new JodaModule());
- @Test
- public void testRefreshBhRestToken() throws InterruptedException {
+ String json = mapper.writeValueAsString(restApiSession);
- bullhornRestCredentials.setRestSessionMinutesToLive("1");
+ System.out.println(json);
- RestApiSession restApiSessionManual = new RestApiSession(bullhornRestCredentials);
- String restToken = restApiSessionManual.getBhRestToken();
- assertNotNull("restToken is null", restToken);
- Thread.sleep(5000);
- assertTrue("session should have expired is null", restApiSessionManual.isSessionExpired());
+ final RestApiSession newSession = mapper.readValue(json, RestApiSession.class);
+ assertNotNull(newSession);
- String newRestToken = restApiSessionManual.refreshBhRestToken();
- assertNotNull("newRestToken is null", newRestToken);
+ }
- assertFalse("new BhRestToken is the same as old BhRestToken", restToken.equals(newRestToken));
+ @Test
+ public void testHasNoSessionProvided() {
- String restUrl = restApiSessionManual.getRestUrl();
- assertNotNull("restUrl is null", restUrl);
+ boolean hasNoSession = restApiSession.isSessionProvided(bullhornRestCredentials);
+ assertFalse(hasNoSession);
- }
+ }
- @Test
- public void testSessionSerialization() throws IOException {
+ @Test
+ public void testSetDateTimeBhRestTokenWillExpire() {
+ DateTime testDate = getNow();
- ObjectMapper mapper = new ObjectMapper();
- mapper.registerModule(new JodaModule());
+ restApiSession.setDateTimeBhRestTokenWillExpire(testDate);
- String json = mapper.writeValueAsString(restApiSession);
+ assertTrue(restApiSession.getDateTimeBhRestTokenWillExpire().equals(testDate));
+ }
- System.out.println(json);
+ @Test
+ public void testCreateSessionWithBadCreds_shouldThrowRestException() throws RestApiException {
+ BullhornRestCredentials creds = new BullhornRestCredentials();
- final RestApiSession newSession = mapper.readValue(json, RestApiSession.class);
- assertNotNull(newSession);
+ creds.setRestAuthorizeUrl("NO_VALUE");
+ creds.setRestClientId("NO_VALUE");
+ creds.setRestClientSecret("NO_VALUE");
+ creds.setRestLoginUrl("NO_VALUE");
+ creds.setRestSessionMinutesToLive("NO_VALUE");
+ creds.setRestTokenUrl("NO_VALUE");
+ creds.setUsername("NO_VALUE");
+ creds.setPassword("NO_VALUE");
- }
+ try {
+ new RestApiSession(creds);
+ Assert.fail("Should have thrown an exception");
+ } catch (RestApiException e) {
+ assertTrue(e.getMessage().equals("Failed to create rest session"));
+ }
+ }
- private DateTime getNow() {
- return new DateTime(DateTimeZone.forID("EST5EDT"));
- }
+ private DateTime getNow() {
+ return new DateTime(DateTimeZone.forID("EST5EDT"));
+ }
}