software.amazon.awssdk.services.mediastoredata.DefaultMediaStoreDataClient Maven / Gradle / Ivy
Show all versions of mediastoredata Show documentation
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package software.amazon.awssdk.services.mediastoredata;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.signer.Aws4UnsignedPayloadSigner;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.ApiName;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.core.runtime.transform.StreamingRequestMarshaller;
import software.amazon.awssdk.core.signer.Signer;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.core.util.VersionInfo;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.metrics.NoOpMetricCollector;
import software.amazon.awssdk.protocols.core.ExceptionMetadata;
import software.amazon.awssdk.protocols.json.AwsJsonProtocol;
import software.amazon.awssdk.protocols.json.AwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.BaseAwsJsonProtocolFactory;
import software.amazon.awssdk.protocols.json.JsonOperationMetadata;
import software.amazon.awssdk.services.mediastoredata.model.ContainerNotFoundException;
import software.amazon.awssdk.services.mediastoredata.model.DeleteObjectRequest;
import software.amazon.awssdk.services.mediastoredata.model.DeleteObjectResponse;
import software.amazon.awssdk.services.mediastoredata.model.DescribeObjectRequest;
import software.amazon.awssdk.services.mediastoredata.model.DescribeObjectResponse;
import software.amazon.awssdk.services.mediastoredata.model.GetObjectRequest;
import software.amazon.awssdk.services.mediastoredata.model.GetObjectResponse;
import software.amazon.awssdk.services.mediastoredata.model.InternalServerErrorException;
import software.amazon.awssdk.services.mediastoredata.model.ListItemsRequest;
import software.amazon.awssdk.services.mediastoredata.model.ListItemsResponse;
import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataException;
import software.amazon.awssdk.services.mediastoredata.model.MediaStoreDataRequest;
import software.amazon.awssdk.services.mediastoredata.model.ObjectNotFoundException;
import software.amazon.awssdk.services.mediastoredata.model.PutObjectRequest;
import software.amazon.awssdk.services.mediastoredata.model.PutObjectResponse;
import software.amazon.awssdk.services.mediastoredata.model.RequestedRangeNotSatisfiableException;
import software.amazon.awssdk.services.mediastoredata.paginators.ListItemsIterable;
import software.amazon.awssdk.services.mediastoredata.transform.DeleteObjectRequestMarshaller;
import software.amazon.awssdk.services.mediastoredata.transform.DescribeObjectRequestMarshaller;
import software.amazon.awssdk.services.mediastoredata.transform.GetObjectRequestMarshaller;
import software.amazon.awssdk.services.mediastoredata.transform.ListItemsRequestMarshaller;
import software.amazon.awssdk.services.mediastoredata.transform.PutObjectRequestMarshaller;
import software.amazon.awssdk.utils.Logger;
/**
* Internal implementation of {@link MediaStoreDataClient}.
*
* @see MediaStoreDataClient#builder()
*/
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
final class DefaultMediaStoreDataClient implements MediaStoreDataClient {
private static final Logger log = Logger.loggerFor(DefaultMediaStoreDataClient.class);
private final SyncClientHandler clientHandler;
private final AwsJsonProtocolFactory protocolFactory;
private final SdkClientConfiguration clientConfiguration;
protected DefaultMediaStoreDataClient(SdkClientConfiguration clientConfiguration) {
this.clientHandler = new AwsSyncClientHandler(clientConfiguration);
this.clientConfiguration = clientConfiguration;
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
}
@Override
public final String serviceName() {
return SERVICE_NAME;
}
/**
*
* Deletes an object at the specified path.
*
*
* @param deleteObjectRequest
* @return Result of the DeleteObject operation returned by the service.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws ObjectNotFoundException
* Could not perform an operation on an object that does not exist.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.DeleteObject
* @see AWS
* API Documentation
*/
@Override
public DeleteObjectResponse deleteObject(DeleteObjectRequest deleteObjectRequest) throws ContainerNotFoundException,
ObjectNotFoundException, InternalServerErrorException, AwsServiceException, SdkClientException,
MediaStoreDataException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
DeleteObjectResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, deleteObjectRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "MediaStore Data");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DeleteObject");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("DeleteObject").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(deleteObjectRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new DeleteObjectRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Gets the headers for an object at the specified path.
*
*
* @param describeObjectRequest
* @return Result of the DescribeObject operation returned by the service.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws ObjectNotFoundException
* Could not perform an operation on an object that does not exist.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.DescribeObject
* @see AWS API Documentation
*/
@Override
public DescribeObjectResponse describeObject(DescribeObjectRequest describeObjectRequest) throws ContainerNotFoundException,
ObjectNotFoundException, InternalServerErrorException, AwsServiceException, SdkClientException,
MediaStoreDataException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
DescribeObjectResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, describeObjectRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "MediaStore Data");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DescribeObject");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("DescribeObject").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(describeObjectRequest)
.withMetricCollector(apiCallMetricCollector)
.withMarshaller(new DescribeObjectRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Downloads the object at the specified path. If the object’s upload availability is set to streaming
,
* AWS Elemental MediaStore downloads the object even if it’s still uploading the object.
*
*
* @param getObjectRequest
* @param responseTransformer
* Functional interface for processing the streamed response content. The unmarshalled GetObjectResponse and
* an InputStream to the response content are provided as parameters to the callback. The callback may return
* a transformed type which will be the return value of this method. See
* {@link software.amazon.awssdk.core.sync.ResponseTransformer} for details on implementing this interface
* and for links to pre-canned implementations for common scenarios like downloading to a file. The service
* documentation for the response content is as follows '
*
* The bytes of the object.
*
* '.
* @return The transformed result of the ResponseTransformer.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws ObjectNotFoundException
* Could not perform an operation on an object that does not exist.
* @throws RequestedRangeNotSatisfiableException
* The requested content range is not valid.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.GetObject
* @see AWS API
* Documentation
*/
@Override
public ReturnT getObject(GetObjectRequest getObjectRequest,
ResponseTransformer responseTransformer) throws ContainerNotFoundException,
ObjectNotFoundException, RequestedRangeNotSatisfiableException, InternalServerErrorException, AwsServiceException,
SdkClientException, MediaStoreDataException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true)
.isPayloadJson(false).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
GetObjectResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, getObjectRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "MediaStore Data");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetObject");
return clientHandler.execute(
new ClientExecutionParams().withOperationName("GetObject")
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withInput(getObjectRequest).withMetricCollector(apiCallMetricCollector)
.withMarshaller(new GetObjectRequestMarshaller(protocolFactory)), responseTransformer);
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Provides a list of metadata entries about folders and objects in the specified folder.
*
*
* @param listItemsRequest
* @return Result of the ListItems operation returned by the service.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.ListItems
* @see AWS API
* Documentation
*/
@Override
public ListItemsResponse listItems(ListItemsRequest listItemsRequest) throws ContainerNotFoundException,
InternalServerErrorException, AwsServiceException, SdkClientException, MediaStoreDataException {
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
ListItemsResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, listItemsRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "MediaStore Data");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "ListItems");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("ListItems").withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler).withInput(listItemsRequest)
.withMetricCollector(apiCallMetricCollector).withMarshaller(new ListItemsRequestMarshaller(protocolFactory)));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
/**
*
* Provides a list of metadata entries about folders and objects in the specified folder.
*
*
*
* This is a variant of {@link #listItems(software.amazon.awssdk.services.mediastoredata.model.ListItemsRequest)}
* operation. The return type is a custom iterable that can be used to iterate through all the pages. SDK will
* internally handle making service calls for you.
*
*
* When this operation is called, a custom iterable is returned but no service calls are made yet. So there is no
* guarantee that the request is valid. As you iterate through the iterable, SDK will start lazily loading response
* pages by making service calls until there are no pages left or your iteration stops. If there are errors in your
* request, you will see the failures only after you start iterating through the iterable.
*
*
*
* The following are few ways to iterate through the response pages:
*
* 1) Using a Stream
*
*
* {@code
* software.amazon.awssdk.services.mediastoredata.paginators.ListItemsIterable responses = client.listItemsPaginator(request);
* responses.stream().forEach(....);
* }
*
*
* 2) Using For loop
*
*
* {
* @code
* software.amazon.awssdk.services.mediastoredata.paginators.ListItemsIterable responses = client.listItemsPaginator(request);
* for (software.amazon.awssdk.services.mediastoredata.model.ListItemsResponse response : responses) {
* // do something;
* }
* }
*
*
* 3) Use iterator directly
*
*
* {@code
* software.amazon.awssdk.services.mediastoredata.paginators.ListItemsIterable responses = client.listItemsPaginator(request);
* responses.iterator().forEachRemaining(....);
* }
*
*
* Please notice that the configuration of MaxResults won't limit the number of results you get with the
* paginator. It only limits the number of results in each page.
*
*
* Note: If you prefer to have control on service calls, use the
* {@link #listItems(software.amazon.awssdk.services.mediastoredata.model.ListItemsRequest)} operation.
*
*
* @param listItemsRequest
* @return A custom iterable that can be used to iterate through all the response pages.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.ListItems
* @see AWS API
* Documentation
*/
@Override
public ListItemsIterable listItemsPaginator(ListItemsRequest listItemsRequest) throws ContainerNotFoundException,
InternalServerErrorException, AwsServiceException, SdkClientException, MediaStoreDataException {
return new ListItemsIterable(this, applyPaginatorUserAgent(listItemsRequest));
}
/**
*
* Uploads an object to the specified path. Object sizes are limited to 25 MB for standard upload availability and
* 10 MB for streaming upload availability.
*
*
* @param putObjectRequest
* @param requestBody
* The content to send to the service. A {@link RequestBody} can be created using one of several factory
* methods for various sources of data. For example, to create a request body from a file you can do the
* following.
*
*
* {@code RequestBody.fromFile(new File("myfile.txt"))}
*
*
* See documentation in {@link RequestBody} for additional details and which sources of data are supported.
* The service documentation for the request content is as follows '
*
* The bytes to be stored.
*
* '
* @return Result of the PutObject operation returned by the service.
* @throws ContainerNotFoundException
* The specified container was not found for the specified account.
* @throws InternalServerErrorException
* The service is temporarily unavailable.
* @throws SdkException
* Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for
* catch all scenarios.
* @throws SdkClientException
* If any client side error occurs such as an IO related failure, failure to get credentials, etc.
* @throws MediaStoreDataException
* Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type.
* @sample MediaStoreDataClient.PutObject
* @see AWS API
* Documentation
*/
@Override
public PutObjectResponse putObject(PutObjectRequest putObjectRequest, RequestBody requestBody)
throws ContainerNotFoundException, InternalServerErrorException, AwsServiceException, SdkClientException,
MediaStoreDataException {
putObjectRequest = applySignerOverride(putObjectRequest, Aws4UnsignedPayloadSigner.create());
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();
HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata,
PutObjectResponse::builder);
HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);
List metricPublishers = resolveMetricPublishers(clientConfiguration, putObjectRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "MediaStore Data");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutObject");
return clientHandler.execute(new ClientExecutionParams()
.withOperationName("PutObject")
.withResponseHandler(responseHandler)
.withErrorResponseHandler(errorResponseHandler)
.withInput(putObjectRequest)
.withMetricCollector(apiCallMetricCollector)
.withRequestBody(requestBody)
.withMarshaller(
StreamingRequestMarshaller.builder()
.delegateMarshaller(new PutObjectRequestMarshaller(protocolFactory)).requestBody(requestBody)
.transferEncoding(true).build()));
} finally {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
}
}
private static List resolveMetricPublishers(SdkClientConfiguration clientConfiguration,
RequestOverrideConfiguration requestOverrideConfiguration) {
List publishers = null;
if (requestOverrideConfiguration != null) {
publishers = requestOverrideConfiguration.metricPublishers();
}
if (publishers == null || publishers.isEmpty()) {
publishers = clientConfiguration.option(SdkClientOption.METRIC_PUBLISHERS);
}
if (publishers == null) {
publishers = Collections.emptyList();
}
return publishers;
}
private HttpResponseHandler createErrorResponseHandler(BaseAwsJsonProtocolFactory protocolFactory,
JsonOperationMetadata operationMetadata) {
return protocolFactory.createErrorResponseHandler(operationMetadata);
}
private > T init(T builder) {
return builder
.clientConfiguration(clientConfiguration)
.defaultServiceExceptionSupplier(MediaStoreDataException::builder)
.protocol(AwsJsonProtocol.REST_JSON)
.protocolVersion("1.1")
.registerModeledException(
ExceptionMetadata.builder().errorCode("RequestedRangeNotSatisfiableException")
.exceptionBuilderSupplier(RequestedRangeNotSatisfiableException::builder).httpStatusCode(416)
.build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ObjectNotFoundException")
.exceptionBuilderSupplier(ObjectNotFoundException::builder).httpStatusCode(404).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("InternalServerError")
.exceptionBuilderSupplier(InternalServerErrorException::builder).build())
.registerModeledException(
ExceptionMetadata.builder().errorCode("ContainerNotFoundException")
.exceptionBuilderSupplier(ContainerNotFoundException::builder).httpStatusCode(404).build());
}
@Override
public void close() {
clientHandler.close();
}
private T applyPaginatorUserAgent(T request) {
Consumer userAgentApplier = b -> b.addApiName(ApiName.builder()
.version(VersionInfo.SDK_VERSION).name("PAGINATED").build());
AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration()
.map(c -> c.toBuilder().applyMutation(userAgentApplier).build())
.orElse((AwsRequestOverrideConfiguration.builder().applyMutation(userAgentApplier).build()));
return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build();
}
private T applySignerOverride(T request, Signer signer) {
if (request.overrideConfiguration().flatMap(c -> c.signer()).isPresent()) {
return request;
}
Consumer signerOverride = b -> b.signer(signer).build();
AwsRequestOverrideConfiguration overrideConfiguration = request.overrideConfiguration()
.map(c -> c.toBuilder().applyMutation(signerOverride).build())
.orElse((AwsRequestOverrideConfiguration.builder().applyMutation(signerOverride).build()));
return (T) request.toBuilder().overrideConfiguration(overrideConfiguration).build();
}
}