Example usage for com.amazonaws AmazonServiceException AmazonServiceException

List of usage examples for com.amazonaws AmazonServiceException AmazonServiceException

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException AmazonServiceException.

Prototype

public AmazonServiceException(String errorMessage, Exception cause) 

Source Link

Document

Constructs a new AmazonServiceException with the specified message and exception indicating the root cause.

Usage

From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private void deleteMessagePayloadFromS3(String receiptHandle) {
    String s3MsgBucketName = getFromReceiptHandleByMarker(receiptHandle,
            SQSExtendedClientConstants.S3_BUCKET_NAME_MARKER);
    String s3MsgKey = getFromReceiptHandleByMarker(receiptHandle, SQSExtendedClientConstants.S3_KEY_MARKER);
    try {//from  ww  w  .j  a  v a2s  .co m
        clientConfiguration.getAmazonS3Client().deleteObject(s3MsgBucketName, s3MsgKey);
    } catch (AmazonServiceException e) {
        String errorMessage = "Failed to delete the S3 object which contains the SQS message payload. SQS message was not deleted.";
        LOG.error(errorMessage, e);
        throw new AmazonServiceException(errorMessage, e);
    } catch (AmazonClientException e) {
        String errorMessage = "Failed to delete the S3 object which contains the SQS message payload. SQS message was not deleted.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
    LOG.info("S3 object deleted, Bucket name: " + s3MsgBucketName + ", Object key: " + s3MsgKey + ".");
}

From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private String getTextFromS3(String s3BucketName, String s3Key) {
    GetObjectRequest getObjectRequest = new GetObjectRequest(s3BucketName, s3Key);
    String embeddedText = null;/*from ww  w  . java2 s .co m*/
    S3Object obj = null;
    try {
        obj = clientConfiguration.getAmazonS3Client().getObject(getObjectRequest);
    } catch (AmazonServiceException e) {
        String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonServiceException(errorMessage, e);
    } catch (AmazonClientException e) {
        String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
    try {
        InputStream objContent = obj.getObjectContent();
        java.util.Scanner objContentScanner = new java.util.Scanner(objContent, "UTF-8");
        objContentScanner.useDelimiter("\\A");
        embeddedText = objContentScanner.hasNext() ? objContentScanner.next() : "";
        objContentScanner.close();
        objContent.close();
    } catch (IOException e) {
        String errorMessage = "Failure when handling the message which was read from S3 object. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
    return embeddedText;
}

From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private void storeTextInS3(String s3Key, String messageContentStr, Long messageContentSize) {
    InputStream messageContentStream = new ByteArrayInputStream(
            messageContentStr.getBytes(StandardCharsets.UTF_8));
    ObjectMetadata messageContentStreamMetadata = new ObjectMetadata();
    messageContentStreamMetadata.setContentLength(messageContentSize);
    PutObjectRequest putObjectRequest = new PutObjectRequest(clientConfiguration.getS3BucketName(), s3Key,
            messageContentStream, messageContentStreamMetadata);
    try {//  w w w  .j a v a  2  s  .c o  m
        clientConfiguration.getAmazonS3Client().putObject(putObjectRequest);
    } catch (AmazonServiceException e) {
        String errorMessage = "Failed to store the message content in an S3 object. SQS message was not sent.";
        LOG.error(errorMessage, e);
        throw new AmazonServiceException(errorMessage, e);
    } catch (AmazonClientException e) {
        String errorMessage = "Failed to store the message content in an S3 object. SQS message was not sent.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
}

From source file:com.netflix.simianarmy.client.vsphere.VSphereClient.java

License:Apache License

@Override
/**/*from w  w w  .ja  v a 2 s  .c  o m*/
 * reinstall the given instance. If it is powered down this will be ignored and the
 * reinstall occurs the next time the machine is powered up.
 */
public void terminateInstance(String instanceId) {
    try {
        connection.connect();

        VirtualMachine virtualMachine = connection.getVirtualMachineById(instanceId);
        this.terminationStrategy.terminate(virtualMachine);
    } catch (RemoteException e) {
        throw new AmazonServiceException("cannot destroy & recreate " + instanceId, e);
    } finally {
        connection.disconnect();
    }
}

From source file:com.netflix.simianarmy.client.vsphere.VSphereServiceConnection.java

License:Apache License

/** connect to the service if not already connected. */
public void connect() throws AmazonServiceException {
    try {/*from w w w  .j ava  2s . co  m*/
        if (service == null) {
            service = new ServiceInstance(new URL(url), username, password, true);
        }
    } catch (RemoteException e) {
        throw new AmazonServiceException("cannot connect to VSphere", e);
    } catch (MalformedURLException e) {
        throw new AmazonServiceException("cannot connect to VSphere", e);
    }
}

From source file:com.netflix.simianarmy.client.vsphere.VSphereServiceConnection.java

License:Apache License

/**
 * Return all VirtualMachines from VSpehere Center.
 *
 * @throws AmazonServiceException/*from  w  w w  .  j  a  va 2  s . c o m*/
 *             If there is any communication error or if no VirtualMachine's are found. */
public VirtualMachine[] describeVirtualMachines() throws AmazonServiceException {
    ManagedEntity[] mes = null;

    try {
        mes = getInventoryNavigator().searchManagedEntities(VIRTUAL_MACHINE_TYPE_NAME);
    } catch (InvalidProperty e) {
        throw new AmazonServiceException("cannot query VSphere", e);
    } catch (RuntimeFault e) {
        throw new AmazonServiceException("cannot query VSphere", e);
    } catch (RemoteException e) {
        throw new AmazonServiceException("cannot query VSphere", e);
    }

    if (mes == null || mes.length == 0) {
        throw new AmazonServiceException(
                "vsphere returned zero entities of type \"" + VIRTUAL_MACHINE_TYPE_NAME + "\"");
    } else {
        return Arrays.copyOf(mes, mes.length, VirtualMachine[].class);
    }
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.AmazonClientInvocationHandler.java

License:Apache License

private <T> List<T> describe(AmazonWebServiceRequest request, String idKey, final String object,
        final Class<T> singleType) {
    lastModified.set(null);//w w  w. j av a  2 s  .co  m
    try {
        JavaType singleMeta = objectMapper.getTypeFactory().constructParametrizedType(Metadata.class,
                Metadata.class, singleType);
        Collection<String> ids = getRequestIds(request, idKey);
        Long mtime = null;
        List<T> results = new ArrayList<>();
        if (ids.isEmpty()) {
            final byte[] json = getJson(object, null);
            JavaType listMeta = objectMapper.getTypeFactory().constructParametrizedType(List.class, List.class,
                    singleMeta);
            List<Metadata<T>> metadataResults = objectMapper.readValue(json, listMeta);
            for (Metadata<T> meta : metadataResults) {
                mtime = mtime == null ? meta.mtime : Math.min(mtime, meta.mtime);
                results.add(meta.data);
            }
        } else {
            for (String id : ids) {
                final byte[] json = getJson(object, id);
                Metadata<T> result = objectMapper.readValue(json, singleMeta);
                mtime = mtime == null ? result.mtime : Math.min(mtime, result.mtime);
                results.add(result.data);
            }
        }
        lastModified.set(mtime);
        return results;
    } catch (Exception e) {
        AmazonServiceException ex = new AmazonServiceException(
                "400 Bad Request -- Edda could not find one of the managed objects requested.", e);
        ex.setStatusCode(400);
        ex.setServiceName(serviceName);
        ex.setErrorType(AmazonServiceException.ErrorType.Unknown);
        throw ex;
    }
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.sdkclient.AmazonClientInvocationHandler.java

License:Apache License

private <T> List<T> describe(AmazonWebServiceRequest request, String idKey, final String object,
        final Class<T> singleType) {
    lastModified.set(null);/*from   ww w.  ja v  a 2s.  c  o m*/
    final Map<String, String> metricTags = new HashMap<>(this.metricTags);
    metricTags.put("collection", object);
    try {
        final Collection<String> ids = getRequestIds(request, idKey);
        metricTags.put("collectionMode", ids.isEmpty() ? "full" : "byId");
        final JavaType singleMeta = objectMapper.getTypeFactory().constructParametrizedType(Metadata.class,
                Metadata.class, singleType);
        Long mtime = null;
        final List<T> results = new ArrayList<>();

        final Id deserializeJsonTimer = registry.createId("edda.deserializeJson", metricTags);
        final Id resultSizeCounter = registry.createId("edda.resultSize", metricTags);
        if (ids.isEmpty()) {
            HttpEntity entity = getHttpEntity(metricTags, object, null);
            try {
                final JavaType listMeta = objectMapper.getTypeFactory().constructParametrizedType(List.class,
                        List.class, singleMeta);
                final List<Metadata<T>> metadataResults = registry.timer(deserializeJsonTimer)
                        .record(() -> objectMapper.readValue(entity.getContent(), listMeta));
                for (Metadata<T> meta : metadataResults) {
                    mtime = mtime == null ? meta.mtime : Math.min(mtime, meta.mtime);
                    results.add(meta.data);
                }
            } finally {
                EntityUtils.consume(entity);
            }
        } else {
            for (String id : ids) {
                HttpEntity entity = getHttpEntity(metricTags, object, id);
                try {
                    final Metadata<T> result = registry.timer(deserializeJsonTimer)
                            .record(() -> objectMapper.readValue(entity.getContent(), singleMeta));
                    mtime = mtime == null ? result.mtime : Math.min(mtime, result.mtime);
                    results.add(result.data);
                } finally {
                    EntityUtils.consume(entity);
                }
            }
        }
        registry.counter(resultSizeCounter).increment(results.size());
        lastModified.set(mtime);
        return results;
    } catch (Exception e) {
        log.error(e.getMessage() + " (retries exhausted)");

        registry.counter(registry.createId("edda.failures", metricTags)).increment();
        final AmazonServiceException ex = new AmazonServiceException(
                "400 Bad Request -- Edda could not find one of the managed objects requested.", e);
        ex.setStatusCode(400);
        ex.setServiceName(serviceName);
        ex.setErrorType(AmazonServiceException.ErrorType.Unknown);
        throw ex;
    }
}

From source file:com.netflix.spinnaker.kork.aws.AwsMetricsSupport.java

License:Apache License

static AmazonServiceException amazonServiceException(Exception exception, String serviceName, int statusCode) {
    if (exception instanceof AmazonServiceException) {
        return (AmazonServiceException) exception;
    }//  w ww  . jav  a  2  s .c  o  m

    final AmazonServiceException ase = new AmazonServiceException(exception.getMessage(), exception);
    ase.setStatusCode(statusCode);
    ase.setErrorCode(DEFAULT_UNKNOWN);
    ase.setServiceName(serviceName);
    ase.setErrorType(AmazonServiceException.ErrorType.Unknown);
    return ase;
}

From source file:com.upplication.s3fs.util.AmazonS3ClientMock.java

License:Open Source License

@Override
public PutObjectResult putObject(String bucketName, String key, File file) throws AmazonClientException {

    try {//from   w w w.  jav a  2 s.  c o  m
        ByteArrayInputStream stream = new ByteArrayInputStream(Files.readAllBytes(file.toPath()));
        S3Element elem = parse(stream, bucketName, key);

        persist(bucketName, elem);

        PutObjectResult putObjectResult = new PutObjectResult();
        putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
        return putObjectResult;
    } catch (IOException e) {
        throw new AmazonServiceException("", e);
    }

}