Search in sources :

Example 26 with BridgeServiceException

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

the class GBFOrderService method parseShippingConfirmations.

ShippingConfirmations parseShippingConfirmations(HttpResponse response) {
    String responseXml;
    try {
        ConfirmShippingResponse responseObj = jsonMapper.readValue(response.getEntity().getContent(), ConfirmShippingResponse.class);
        responseXml = responseObj.XML;
    } catch (IOException e) {
        LOG.error("parseShippingConfirmations failed to read Json from remote response", e);
        throw new BridgeServiceException("Error parsing response Json.");
    }
    return parseShippingConfirmations(responseXml);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) IOException(java.io.IOException)

Example 27 with BridgeServiceException

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

the class SendMailViaAmazonService method sendEmail.

@Override
public void sendEmail(MimeTypeEmailProvider provider) {
    String senderEmail = provider.getPlainSenderEmail();
    if (!emailVerificationService.isVerified(senderEmail)) {
        throw new BridgeServiceException(UNVERIFIED_EMAIL_ERROR);
    }
    try {
        String fullSenderEmail = provider.getMimeTypeEmail().getSenderAddress();
        MimeTypeEmail email = provider.getMimeTypeEmail();
        for (String recipient : email.getRecipientAddresses()) {
            sendEmail(fullSenderEmail, recipient, email, provider.getApp().getIdentifier());
        }
    } catch (MessageRejectedException ex) {
        // This happens if the sender email is not verified in SES. In general, it's not useful to app users to
        // receive a 500 Internal Error when this happens. Plus, if this exception gets thrown, the user session
        // won't be updated properly, and really weird things happen. The best course of option is to log an error
        // and swallow the exception.
        logger.error("SES rejected email: " + ex.getMessage(), ex);
    } catch (MessagingException | AmazonServiceException | IOException e) {
        throw new BridgeServiceException(e);
    }
}
Also used : MessagingException(javax.mail.MessagingException) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) AmazonServiceException(com.amazonaws.AmazonServiceException) MimeTypeEmail(org.sagebionetworks.bridge.services.email.MimeTypeEmail) IOException(java.io.IOException) MessageRejectedException(com.amazonaws.services.simpleemail.model.MessageRejectedException)

Example 28 with BridgeServiceException

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

the class OAuthProviderService method executeInternal.

private OAuthProviderService.Response executeInternal(HttpPost client) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        CloseableHttpResponse response = httpclient.execute(client);
        int statusCode = response.getStatusLine().getStatusCode();
        JsonNode body;
        try {
            body = BridgeObjectMapper.get().readTree(response.getEntity().getContent());
        } catch (JsonParseException ex) {
            // Log the error and the status code. Set body to a null node, so we don't break any callers.
            LOG.error("OAuth call failed with invalid JSON, status code " + statusCode);
            body = NullNode.getInstance();
        }
        return new Response(statusCode, body);
    } catch (IOException e) {
        LOG.error(SERVICE_ERROR_MSG, e);
        throw new BridgeServiceException(SERVICE_ERROR_MSG);
    }
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 29 with BridgeServiceException

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

the class OAuthService method retrieveAccessToken.

private OAuthAccessToken retrieveAccessToken(App app, String vendorId, String healthCode, OAuthAuthorizationToken authToken) {
    checkNotNull(app);
    checkNotNull(vendorId);
    checkNotNull(healthCode);
    OAuthProvider provider = app.getOAuthProviders().get(vendorId);
    if (provider == null) {
        throw new EntityNotFoundException(OAuthProvider.class);
    }
    OAuthAccessGrant grant = null;
    try {
        // If client has submitted an authorization token, we always refresh the grant
        if (authToken != null && authToken.getAuthToken() != null) {
            grant = providerService.requestAccessGrant(provider, authToken);
        } else {
            // If not, start first by seeing if a grant has been saved
            grant = grantDao.getAccessGrant(app.getIdentifier(), vendorId, healthCode);
        }
        // If no grant was saved or successfully returned from a grant, it's not found.
        if (grant == null) {
            throw new EntityNotFoundException(OAuthAccessGrant.class);
        } else if (getDateTime().isAfter(grant.getExpiresOn())) {
            // If there's a grant record, but it has expired, attempt to refresh it
            grant = providerService.refreshAccessGrant(provider, vendorId, grant.getRefreshToken());
        }
    } catch (BridgeServiceException e) {
        // It is in an unknown state.
        if (e.getStatusCode() < 502 || e.getStatusCode() > 504) {
            grantDao.deleteAccessGrant(app.getIdentifier(), vendorId, healthCode);
        }
        throw e;
    }
    grant.setVendorId(vendorId);
    grant.setHealthCode(healthCode);
    grantDao.saveAccessGrant(app.getIdentifier(), grant);
    return getTokenForGrant(grant);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) OAuthProvider(org.sagebionetworks.bridge.models.apps.OAuthProvider) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) OAuthAccessGrant(org.sagebionetworks.bridge.models.oauth.OAuthAccessGrant)

Example 30 with BridgeServiceException

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

the class DynamoSchedulePlan method setData.

public void setData(ObjectNode data) {
    if (data != null) {
        String typeName = JsonUtils.asText(data, "type");
        try {
            String className = BridgeConstants.SCHEDULE_STRATEGY_PACKAGE + typeName;
            Class<?> clazz = Class.forName(className);
            strategy = (ScheduleStrategy) BridgeObjectMapper.get().treeToValue(data, clazz);
        } catch (ClassCastException | ClassNotFoundException e) {
            throw new BadRequestException("Invalid type " + typeName);
        } catch (JsonProcessingException e) {
            throw new BridgeServiceException(e);
        }
    }
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

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