use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.
the class CacheProvider method getUserSession.
public UserSession getUserSession(String sessionToken) {
checkNotNull(sessionToken);
try {
CacheKey tokenToUserIdKey = CacheKey.tokenToUserId(sessionToken);
String userId = jedisOps.get(tokenToUserIdKey.toString());
if (userId != null) {
CacheKey userIdToSessionKey = CacheKey.userIdToSession(userId);
String ser = jedisOps.get(userIdToSessionKey.toString());
if (ser != null) {
JsonNode node = adjustJsonWithStudyIdentifier(ser);
UserSession session = BridgeObjectMapper.get().treeToValue(node, UserSession.class);
// invalidate its own session.
if (session.getSessionToken().equals(sessionToken)) {
return session;
}
// Otherwise, delete the key sessionToken key (it's known to be invalid)
removeObject(tokenToUserIdKey);
}
}
return null;
} catch (Throwable e) {
promptToStartRedisIfLocal(e);
throw new BridgeServiceException(e);
}
}
use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.
the class DynamoHealthDataDaoTest method deleteRecordsForHealthCodeMapperException.
@Test
public void deleteRecordsForHealthCodeMapperException() {
// mock failed batch
// we have to mock extra stuff because BridgeUtils.ifFailuresThrowException() checks all these things
DynamoDBMapper.FailedBatch failure = new DynamoDBMapper.FailedBatch();
failure.setException(new Exception("dummy exception message"));
failure.setUnprocessedItems(Collections.emptyMap());
// mock mapper
DynamoDBMapper mockMapper = mock(DynamoDBMapper.class);
ArgumentCaptor<List> arg = ArgumentCaptor.forClass(List.class);
when(mockMapper.batchDelete(arg.capture())).thenReturn(Collections.singletonList(failure));
// mock index helper
ItemCollection mockItemCollection = mock(ItemCollection.class);
IteratorSupport mockIteratorSupport = mock(IteratorSupport.class);
Item mockItem = new Item().with("healthCode", "test health code").with("id", "error record");
when(mockItemCollection.iterator()).thenReturn(mockIteratorSupport);
when(mockIteratorSupport.hasNext()).thenReturn(true, false);
when(mockIteratorSupport.next()).thenReturn(mockItem);
DynamoIndexHelper mockIndexHelper = mock(DynamoIndexHelper.class);
Index mockIndex = mock(Index.class);
when(mockIndexHelper.getIndex()).thenReturn(mockIndex);
when(mockIndex.query("healthCode", "test health code")).thenReturn(mockItemCollection);
// set up
DynamoHealthDataDao dao = new DynamoHealthDataDao();
dao.setMapper(mockMapper);
dao.setHealthCodeIndex(mockIndexHelper);
// execute and validate exception
Exception thrownEx = null;
try {
dao.deleteRecordsForHealthCode("test health code");
fail();
} catch (BridgeServiceException ex) {
thrownEx = ex;
}
assertNotNull(thrownEx);
// validate intermediate results
List<HealthDataRecord> recordKeyList = arg.getValue();
assertEquals(recordKeyList.size(), 1);
assertEquals(recordKeyList.get(0).getId(), "error record");
}
use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.
the class EmailVerificationServiceCallWrapperTest method nonRetryableError.
@Test
public void nonRetryableError() throws Exception {
// Set up callable and its exceptions.
AmazonServiceException awsEx = makeAwsBadRequest("error1");
when(mockCallable.call()).thenThrow(awsEx);
// Execute.
try {
asyncHandler.callWrapper(mockCallable);
fail("expected exception");
} catch (BridgeServiceException thrownEx) {
// Only 1 error thrown.
assertSame(thrownEx.getCause(), awsEx);
}
// We call the callable once.
verify(mockCallable, times(1)).call();
}
use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.
the class OAuthServiceTest method transientProviderErrorDoesNotDeleteGrant.
@Test
public void transientProviderErrorDoesNotDeleteGrant() {
setupInvalidGrantCall(new BridgeServiceException("Temporary error", 503));
try {
service.requestAccessToken(app, HEALTH_CODE, AUTH_TOKEN);
fail("Should have thrown exception");
} catch (BridgeServiceException e) {
}
verify(mockProviderService).requestAccessGrant(PROVIDER, AUTH_TOKEN);
verifyNoMoreInteractions(mockGrantDao);
verifyNoMoreInteractions(mockProviderService);
}
use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.
the class UploadArchiveService method decrypt.
/**
* Decrypts the specified data, using the encryption materials for the specified app.
*
* @param appId
* appId, must be non-null, non-empty, and refer to a valid app
* @param bytes
* data to decrypt, must be non-null
* @return decrypted data as a byte array
* @throws BridgeServiceException
* if we fail to load the encryptor, or if decryption fails
*/
public byte[] decrypt(String appId, byte[] bytes) throws BridgeServiceException {
// validate
checkNotNull(appId);
checkArgument(StringUtils.isNotBlank(appId));
checkNotNull(bytes);
// decrypt
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
InputStream decryptedStream = decrypt(appId, byteArrayInputStream)) {
return ByteStreams.toByteArray(decryptedStream);
} catch (IOException ex) {
throw new BridgeServiceException(ex);
}
}
Aggregations