Search in sources :

Example 6 with BridgeServiceException

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);
    }
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 7 with BridgeServiceException

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");
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) ItemCollection(com.amazonaws.services.dynamodbv2.document.ItemCollection) Index(com.amazonaws.services.dynamodbv2.document.Index) IteratorSupport(com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) Item(com.amazonaws.services.dynamodbv2.document.Item) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.testng.annotations.Test)

Example 8 with BridgeServiceException

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();
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) Test(org.testng.annotations.Test)

Example 9 with BridgeServiceException

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);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) Test(org.testng.annotations.Test)

Example 10 with BridgeServiceException

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) IOException(java.io.IOException)

Aggregations

BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)78 IOException (java.io.IOException)25 Test (org.testng.annotations.Test)21 PersistenceException (javax.persistence.PersistenceException)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 CacheKey (org.sagebionetworks.bridge.cache.CacheKey)5 App (org.sagebionetworks.bridge.models.apps.App)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Stopwatch (com.google.common.base.Stopwatch)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 List (java.util.List)3 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)3 FailedBatch (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.FailedBatch)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 SendMessageResult (com.amazonaws.services.sqs.model.SendMessageResult)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2