Search in sources :

Example 16 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class CacheAdminService method removeItem.

/**
 * Delete an item by its key from the cache (cannot delete sessions).
 * @param cacheKey
 */
public void removeItem(String cacheKey) {
    checkArgument(isNotBlank(cacheKey));
    Long removed = null;
    if (CacheKey.isPublic(cacheKey)) {
        try (Jedis jedis = jedisPool.getResource()) {
            removed = jedis.del(cacheKey);
        }
    }
    if (removed == null || removed == 0) {
        throw new BridgeServiceException("Item could not be removed from cache: does key '" + cacheKey + "' exist?");
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException)

Example 17 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class ConsentPdf method createPdf.

private byte[] createPdf(final String consentDoc) {
    try (ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder()) {
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(consentDoc);
        renderer.layout();
        renderer.createPDF(byteArrayBuilder);
        byteArrayBuilder.flush();
        return byteArrayBuilder.toByteArray();
    } catch (DocumentException e) {
        throw new BridgeServiceException(e);
    }
}
Also used : ITextRenderer(org.xhtmlrenderer.pdf.ITextRenderer) DocumentException(com.lowagie.text.DocumentException) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder)

Example 18 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class AppService method restoreVerification.

// Helper method to fetch consent notification email verification data from the cache.
private VerificationData restoreVerification(String sptoken) {
    checkArgument(isNotBlank(sptoken));
    CacheKey cacheKey = CacheKey.verificationToken(sptoken);
    String json = cacheProvider.getObject(cacheKey, String.class);
    if (json != null) {
        try {
            cacheProvider.removeObject(cacheKey);
            return BridgeObjectMapper.get().readValue(json, VerificationData.class);
        } catch (IOException e) {
            throw new BridgeServiceException(e);
        }
    }
    return null;
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) IOException(java.io.IOException) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 19 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class AccountWorkflowService method resetPassword.

/**
 * Use a supplied password reset token to change the password on an account. If the supplied
 * token is not valid, this method throws an exception. If the token is valid but the account
 * does not exist, an exception is also thrown (this would be unusual).
 */
public void resetPassword(PasswordReset passwordReset) {
    checkNotNull(passwordReset);
    // This pathway is unusual as the token may have been sent via email or phone, so test for both.
    CacheKey emailCacheKey = CacheKey.passwordResetForEmail(passwordReset.getSptoken(), passwordReset.getAppId());
    CacheKey phoneCacheKey = CacheKey.passwordResetForPhone(passwordReset.getSptoken(), passwordReset.getAppId());
    String email = cacheProvider.getObject(emailCacheKey, String.class);
    Phone phone = cacheProvider.getObject(phoneCacheKey, Phone.class);
    if (email == null && phone == null) {
        throw new BadRequestException(PASSWORD_RESET_TOKEN_EXPIRED);
    }
    cacheProvider.removeObject(emailCacheKey);
    cacheProvider.removeObject(phoneCacheKey);
    App app = appService.getApp(passwordReset.getAppId());
    ChannelType channelType = null;
    AccountId accountId = null;
    if (email != null) {
        accountId = AccountId.forEmail(app.getIdentifier(), email);
        channelType = ChannelType.EMAIL;
    } else if (phone != null) {
        accountId = AccountId.forPhone(app.getIdentifier(), phone);
        channelType = ChannelType.PHONE;
    } else {
        throw new BridgeServiceException("Could not reset password");
    }
    Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
    accountService.changePassword(account, channelType, passwordReset.getPassword());
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) Phone(org.sagebionetworks.bridge.models.accounts.Phone) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) ChannelType(org.sagebionetworks.bridge.services.AuthenticationService.ChannelType) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 20 with BridgeServiceException

use of org.sagebionetworks.bridge.exceptions.BridgeServiceException in project BridgeServer2 by Sage-Bionetworks.

the class ConsentService method sendConsentViaSMS.

private void sendConsentViaSMS(App app, Subpopulation subpop, StudyParticipant participant, ConsentPdf consentPdf) {
    String shortUrl;
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        String fileName = getSignedConsentUrl();
        DateTime expiresOn = getDownloadExpiration();
        s3Helper.writeBytesToS3(USERSIGNED_CONSENTS_BUCKET, fileName, consentPdf.getBytes(), metadata);
        URL url = s3Helper.generatePresignedUrl(USERSIGNED_CONSENTS_BUCKET, fileName, expiresOn, HttpMethod.GET);
        shortUrl = urlShortenerService.shortenUrl(url.toString(), SIGNED_CONSENT_DOWNLOAD_EXPIRE_IN_SECONDS);
    } catch (IOException e) {
        throw new BridgeServiceException(e);
    }
    TemplateRevision revision = templateService.getRevisionForUser(app, SMS_SIGNED_CONSENT);
    SmsMessageProvider provider = new SmsMessageProvider.Builder().withApp(app).withPhone(participant.getPhone()).withExpirationPeriod(EXPIRATION_PERIOD_KEY, SIGNED_CONSENT_DOWNLOAD_EXPIRE_IN_SECONDS).withTransactionType().withTemplateRevision(revision).withToken(BridgeConstants.CONSENT_URL, shortUrl).build();
    smsService.sendSmsMessage(participant.getId(), provider);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) TemplateRevision(org.sagebionetworks.bridge.models.templates.TemplateRevision) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DateTime(org.joda.time.DateTime) URL(java.net.URL) SmsMessageProvider(org.sagebionetworks.bridge.sms.SmsMessageProvider)

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