All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.awssdk.services.polly.DefaultPollyClient Maven / Gradle / Ivy

Go to download

The AWS Java SDK for Amazon Polly module holds the client classes that are used for communicating with Amazon Polly.

There is a newer version: 2.29.15
Show newest version
/*
 * 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.polly;

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.awscore.client.handler.AwsSyncClientHandler;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.awscore.internal.AwsProtocolMetadata;
import software.amazon.awssdk.awscore.internal.AwsServiceProtocol;
import software.amazon.awssdk.awscore.retry.AwsRetryStrategy;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
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.retry.RetryMode;
import software.amazon.awssdk.core.sync.ResponseTransformer;
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.retries.api.RetryStrategy;
import software.amazon.awssdk.services.polly.internal.PollyServiceClientConfigurationBuilder;
import software.amazon.awssdk.services.polly.model.DeleteLexiconRequest;
import software.amazon.awssdk.services.polly.model.DeleteLexiconResponse;
import software.amazon.awssdk.services.polly.model.DescribeVoicesRequest;
import software.amazon.awssdk.services.polly.model.DescribeVoicesResponse;
import software.amazon.awssdk.services.polly.model.EngineNotSupportedException;
import software.amazon.awssdk.services.polly.model.GetLexiconRequest;
import software.amazon.awssdk.services.polly.model.GetLexiconResponse;
import software.amazon.awssdk.services.polly.model.GetSpeechSynthesisTaskRequest;
import software.amazon.awssdk.services.polly.model.GetSpeechSynthesisTaskResponse;
import software.amazon.awssdk.services.polly.model.InvalidLexiconException;
import software.amazon.awssdk.services.polly.model.InvalidNextTokenException;
import software.amazon.awssdk.services.polly.model.InvalidS3BucketException;
import software.amazon.awssdk.services.polly.model.InvalidS3KeyException;
import software.amazon.awssdk.services.polly.model.InvalidSampleRateException;
import software.amazon.awssdk.services.polly.model.InvalidSnsTopicArnException;
import software.amazon.awssdk.services.polly.model.InvalidSsmlException;
import software.amazon.awssdk.services.polly.model.InvalidTaskIdException;
import software.amazon.awssdk.services.polly.model.LanguageNotSupportedException;
import software.amazon.awssdk.services.polly.model.LexiconNotFoundException;
import software.amazon.awssdk.services.polly.model.LexiconSizeExceededException;
import software.amazon.awssdk.services.polly.model.ListLexiconsRequest;
import software.amazon.awssdk.services.polly.model.ListLexiconsResponse;
import software.amazon.awssdk.services.polly.model.ListSpeechSynthesisTasksRequest;
import software.amazon.awssdk.services.polly.model.ListSpeechSynthesisTasksResponse;
import software.amazon.awssdk.services.polly.model.MarksNotSupportedForFormatException;
import software.amazon.awssdk.services.polly.model.MaxLexemeLengthExceededException;
import software.amazon.awssdk.services.polly.model.MaxLexiconsNumberExceededException;
import software.amazon.awssdk.services.polly.model.PollyException;
import software.amazon.awssdk.services.polly.model.PutLexiconRequest;
import software.amazon.awssdk.services.polly.model.PutLexiconResponse;
import software.amazon.awssdk.services.polly.model.ServiceFailureException;
import software.amazon.awssdk.services.polly.model.SsmlMarksNotSupportedForTextTypeException;
import software.amazon.awssdk.services.polly.model.StartSpeechSynthesisTaskRequest;
import software.amazon.awssdk.services.polly.model.StartSpeechSynthesisTaskResponse;
import software.amazon.awssdk.services.polly.model.SynthesisTaskNotFoundException;
import software.amazon.awssdk.services.polly.model.SynthesizeSpeechRequest;
import software.amazon.awssdk.services.polly.model.SynthesizeSpeechResponse;
import software.amazon.awssdk.services.polly.model.TextLengthExceededException;
import software.amazon.awssdk.services.polly.model.UnsupportedPlsAlphabetException;
import software.amazon.awssdk.services.polly.model.UnsupportedPlsLanguageException;
import software.amazon.awssdk.services.polly.transform.DeleteLexiconRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.DescribeVoicesRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.GetLexiconRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.GetSpeechSynthesisTaskRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.ListLexiconsRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.ListSpeechSynthesisTasksRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.PutLexiconRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.StartSpeechSynthesisTaskRequestMarshaller;
import software.amazon.awssdk.services.polly.transform.SynthesizeSpeechRequestMarshaller;
import software.amazon.awssdk.utils.Logger;

/**
 * Internal implementation of {@link PollyClient}.
 *
 * @see PollyClient#builder()
 */
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
final class DefaultPollyClient implements PollyClient {
    private static final Logger log = Logger.loggerFor(DefaultPollyClient.class);

    private static final AwsProtocolMetadata protocolMetadata = AwsProtocolMetadata.builder()
            .serviceProtocol(AwsServiceProtocol.REST_JSON).build();

    private final SyncClientHandler clientHandler;

    private final AwsJsonProtocolFactory protocolFactory;

    private final SdkClientConfiguration clientConfiguration;

    protected DefaultPollyClient(SdkClientConfiguration clientConfiguration) {
        this.clientHandler = new AwsSyncClientHandler(clientConfiguration);
        this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this).build();
        this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
    }

    /**
     * 

* Deletes the specified pronunciation lexicon stored in an Amazon Web Services Region. A lexicon which has been * deleted is not available for speech synthesis, nor is it possible to retrieve it using either the * GetLexicon or ListLexicon APIs. *

*

* For more information, see Managing * Lexicons. *

* * @param deleteLexiconRequest * @return Result of the DeleteLexicon operation returned by the service. * @throws LexiconNotFoundException * Amazon Polly can't find the specified lexicon. This could be caused by a lexicon that is missing, its * name is misspelled or specifying a lexicon that is in a different region.

*

* Verify that the lexicon exists, is in the region (see ListLexicons) and that you spelled its name * is spelled correctly. Then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.DeleteLexicon * @see AWS API * Documentation */ @Override public DeleteLexiconResponse deleteLexicon(DeleteLexiconRequest deleteLexiconRequest) throws LexiconNotFoundException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, DeleteLexiconResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(deleteLexiconRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, deleteLexiconRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DeleteLexicon"); return clientHandler.execute(new ClientExecutionParams() .withOperationName("DeleteLexicon").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(deleteLexiconRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new DeleteLexiconRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Returns the list of voices that are available for use when requesting speech synthesis. Each voice speaks a * specified language, is either male or female, and is identified by an ID, which is the ASCII version of the voice * name. *

*

* When synthesizing speech ( SynthesizeSpeech ), you provide the voice ID for the voice you want from * the list of voices returned by DescribeVoices. *

*

* For example, you want your news reader application to read news in a specific language, but giving a user the * option to choose the voice. Using the DescribeVoices operation you can provide the user with a list * of available voices to select from. *

*

* You can optionally specify a language code to filter the available voices. For example, if you specify * en-US, the operation returns a list of all available US English voices. *

*

* This operation requires permissions to perform the polly:DescribeVoices action. *

* * @param describeVoicesRequest * @return Result of the DescribeVoices operation returned by the service. * @throws InvalidNextTokenException * The NextToken is invalid. Verify that it's spelled correctly, and then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.DescribeVoices * @see AWS API * Documentation */ @Override public DescribeVoicesResponse describeVoices(DescribeVoicesRequest describeVoicesRequest) throws InvalidNextTokenException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, DescribeVoicesResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(describeVoicesRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, describeVoicesRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "DescribeVoices"); return clientHandler.execute(new ClientExecutionParams() .withOperationName("DescribeVoices").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(describeVoicesRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new DescribeVoicesRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Returns the content of the specified pronunciation lexicon stored in an Amazon Web Services Region. For more * information, see Managing * Lexicons. *

* * @param getLexiconRequest * @return Result of the GetLexicon operation returned by the service. * @throws LexiconNotFoundException * Amazon Polly can't find the specified lexicon. This could be caused by a lexicon that is missing, its * name is misspelled or specifying a lexicon that is in a different region.

*

* Verify that the lexicon exists, is in the region (see ListLexicons) and that you spelled its name * is spelled correctly. Then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.GetLexicon * @see AWS API * Documentation */ @Override public GetLexiconResponse getLexicon(GetLexiconRequest getLexiconRequest) throws LexiconNotFoundException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, GetLexiconResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getLexiconRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getLexiconRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetLexicon"); return clientHandler.execute(new ClientExecutionParams() .withOperationName("GetLexicon").withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) .withInput(getLexiconRequest).withMetricCollector(apiCallMetricCollector) .withMarshaller(new GetLexiconRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Retrieves a specific SpeechSynthesisTask object based on its TaskID. This object contains information about the * given speech synthesis task, including the status of the task, and a link to the S3 bucket containing the output * of the task. *

* * @param getSpeechSynthesisTaskRequest * @return Result of the GetSpeechSynthesisTask operation returned by the service. * @throws InvalidTaskIdException * The provided Task ID is not valid. Please provide a valid Task ID and try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @throws SynthesisTaskNotFoundException * The Speech Synthesis task with requested Task ID cannot be found. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.GetSpeechSynthesisTask * @see AWS * API Documentation */ @Override public GetSpeechSynthesisTaskResponse getSpeechSynthesisTask(GetSpeechSynthesisTaskRequest getSpeechSynthesisTaskRequest) throws InvalidTaskIdException, ServiceFailureException, SynthesisTaskNotFoundException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, GetSpeechSynthesisTaskResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(getSpeechSynthesisTaskRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, getSpeechSynthesisTaskRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "GetSpeechSynthesisTask"); return clientHandler .execute(new ClientExecutionParams() .withOperationName("GetSpeechSynthesisTask").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(getSpeechSynthesisTaskRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new GetSpeechSynthesisTaskRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Returns a list of pronunciation lexicons stored in an Amazon Web Services Region. For more information, see Managing Lexicons. *

* * @param listLexiconsRequest * @return Result of the ListLexicons operation returned by the service. * @throws InvalidNextTokenException * The NextToken is invalid. Verify that it's spelled correctly, and then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.ListLexicons * @see AWS API * Documentation */ @Override public ListLexiconsResponse listLexicons(ListLexiconsRequest listLexiconsRequest) throws InvalidNextTokenException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, ListLexiconsResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(listLexiconsRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, listLexiconsRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "ListLexicons"); return clientHandler.execute(new ClientExecutionParams() .withOperationName("ListLexicons").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(listLexiconsRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new ListLexiconsRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Returns a list of SpeechSynthesisTask objects ordered by their creation date. This operation can filter the tasks * by their status, for example, allowing users to list only tasks that are completed. *

* * @param listSpeechSynthesisTasksRequest * @return Result of the ListSpeechSynthesisTasks operation returned by the service. * @throws InvalidNextTokenException * The NextToken is invalid. Verify that it's spelled correctly, and then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.ListSpeechSynthesisTasks * @see AWS API Documentation */ @Override public ListSpeechSynthesisTasksResponse listSpeechSynthesisTasks( ListSpeechSynthesisTasksRequest listSpeechSynthesisTasksRequest) throws InvalidNextTokenException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, ListSpeechSynthesisTasksResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(listSpeechSynthesisTasksRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, listSpeechSynthesisTasksRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "ListSpeechSynthesisTasks"); return clientHandler .execute(new ClientExecutionParams() .withOperationName("ListSpeechSynthesisTasks").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(listSpeechSynthesisTasksRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new ListSpeechSynthesisTasksRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Stores a pronunciation lexicon in an Amazon Web Services Region. If a lexicon with the same name already exists * in the region, it is overwritten by the new lexicon. Lexicon operations have eventual consistency, therefore, it * might take some time before the lexicon is available to the SynthesizeSpeech operation. *

*

* For more information, see Managing * Lexicons. *

* * @param putLexiconRequest * @return Result of the PutLexicon operation returned by the service. * @throws InvalidLexiconException * Amazon Polly can't find the specified lexicon. Verify that the lexicon's name is spelled correctly, and * then try again. * @throws UnsupportedPlsAlphabetException * The alphabet specified by the lexicon is not a supported alphabet. Valid values are x-sampa * and ipa. * @throws UnsupportedPlsLanguageException * The language specified in the lexicon is unsupported. For a list of supported languages, see Lexicon Attributes. * @throws LexiconSizeExceededException * The maximum size of the specified lexicon would be exceeded by this operation. * @throws MaxLexemeLengthExceededException * The maximum size of the lexeme would be exceeded by this operation. * @throws MaxLexiconsNumberExceededException * The maximum number of lexicons would be exceeded by this operation. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.PutLexicon * @see AWS API * Documentation */ @Override public PutLexiconResponse putLexicon(PutLexiconRequest putLexiconRequest) throws InvalidLexiconException, UnsupportedPlsAlphabetException, UnsupportedPlsLanguageException, LexiconSizeExceededException, MaxLexemeLengthExceededException, MaxLexiconsNumberExceededException, ServiceFailureException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, PutLexiconResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(putLexiconRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, putLexiconRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "PutLexicon"); return clientHandler.execute(new ClientExecutionParams() .withOperationName("PutLexicon").withProtocolMetadata(protocolMetadata).withResponseHandler(responseHandler) .withErrorResponseHandler(errorResponseHandler).withRequestConfiguration(clientConfiguration) .withInput(putLexiconRequest).withMetricCollector(apiCallMetricCollector) .withMarshaller(new PutLexiconRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Allows the creation of an asynchronous synthesis task, by starting a new SpeechSynthesisTask. This * operation requires all the standard information needed for speech synthesis, plus the name of an Amazon S3 bucket * for the service to store the output of the synthesis task and two optional parameters ( * OutputS3KeyPrefix and SnsTopicArn). Once the synthesis task is created, this operation * will return a SpeechSynthesisTask object, which will include an identifier of this task as well as * the current status. The SpeechSynthesisTask object is available for 72 hours after starting the * asynchronous synthesis task. *

* * @param startSpeechSynthesisTaskRequest * @return Result of the StartSpeechSynthesisTask operation returned by the service. * @throws TextLengthExceededException * The value of the "Text" parameter is longer than the accepted limits. For the * SynthesizeSpeech API, the limit for input text is a maximum of 6000 characters total, of * which no more than 3000 can be billed characters. For the StartSpeechSynthesisTask API, the * maximum is 200,000 characters, of which no more than 100,000 can be billed characters. SSML tags are not * counted as billed characters. * @throws InvalidS3BucketException * The provided Amazon S3 bucket name is invalid. Please check your input with S3 bucket naming requirements * and try again. * @throws InvalidS3KeyException * The provided Amazon S3 key prefix is invalid. Please provide a valid S3 object key name. * @throws InvalidSampleRateException * The specified sample rate is not valid. * @throws InvalidSnsTopicArnException * The provided SNS topic ARN is invalid. Please provide a valid SNS topic ARN and try again. * @throws InvalidSsmlException * The SSML you provided is invalid. Verify the SSML syntax, spelling of tags and values, and then try * again. * @throws EngineNotSupportedException * This engine is not compatible with the voice that you have designated. Choose a new voice that is * compatible with the engine or change the engine and restart the operation. * @throws LexiconNotFoundException * Amazon Polly can't find the specified lexicon. This could be caused by a lexicon that is missing, its * name is misspelled or specifying a lexicon that is in a different region.

*

* Verify that the lexicon exists, is in the region (see ListLexicons) and that you spelled its name * is spelled correctly. Then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @throws MarksNotSupportedForFormatException * Speech marks are not supported for the OutputFormat selected. Speech marks are only * available for content in json format. * @throws SsmlMarksNotSupportedForTextTypeException * SSML speech marks are not supported for plain text-type input. * @throws LanguageNotSupportedException * The language specified is not currently supported by Amazon Polly in this capacity. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.StartSpeechSynthesisTask * @see AWS API Documentation */ @Override public StartSpeechSynthesisTaskResponse startSpeechSynthesisTask( StartSpeechSynthesisTaskRequest startSpeechSynthesisTaskRequest) throws TextLengthExceededException, InvalidS3BucketException, InvalidS3KeyException, InvalidSampleRateException, InvalidSnsTopicArnException, InvalidSsmlException, EngineNotSupportedException, LexiconNotFoundException, ServiceFailureException, MarksNotSupportedForFormatException, SsmlMarksNotSupportedForTextTypeException, LanguageNotSupportedException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false) .isPayloadJson(true).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler( operationMetadata, StartSpeechSynthesisTaskResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(startSpeechSynthesisTaskRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, startSpeechSynthesisTaskRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "StartSpeechSynthesisTask"); return clientHandler .execute(new ClientExecutionParams() .withOperationName("StartSpeechSynthesisTask").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(startSpeechSynthesisTaskRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new StartSpeechSynthesisTaskRequestMarshaller(protocolFactory))); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } /** *

* Synthesizes UTF-8 input, plain text or SSML, to a stream of bytes. SSML input must be valid, well-formed SSML. * Some alphabets might not be available with all the voices (for example, Cyrillic might not be read at all by * English voices) unless phoneme mapping is used. For more information, see How it Works. *

* * @param synthesizeSpeechRequest * @param responseTransformer * Functional interface for processing the streamed response content. The unmarshalled * SynthesizeSpeechResponse 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 ' *

* Stream containing the synthesized speech. *

* '. * @return The transformed result of the ResponseTransformer. * @throws TextLengthExceededException * The value of the "Text" parameter is longer than the accepted limits. For the * SynthesizeSpeech API, the limit for input text is a maximum of 6000 characters total, of * which no more than 3000 can be billed characters. For the StartSpeechSynthesisTask API, the * maximum is 200,000 characters, of which no more than 100,000 can be billed characters. SSML tags are not * counted as billed characters. * @throws InvalidSampleRateException * The specified sample rate is not valid. * @throws InvalidSsmlException * The SSML you provided is invalid. Verify the SSML syntax, spelling of tags and values, and then try * again. * @throws LexiconNotFoundException * Amazon Polly can't find the specified lexicon. This could be caused by a lexicon that is missing, its * name is misspelled or specifying a lexicon that is in a different region.

*

* Verify that the lexicon exists, is in the region (see ListLexicons) and that you spelled its name * is spelled correctly. Then try again. * @throws ServiceFailureException * An unknown condition has caused a service failure. * @throws MarksNotSupportedForFormatException * Speech marks are not supported for the OutputFormat selected. Speech marks are only * available for content in json format. * @throws SsmlMarksNotSupportedForTextTypeException * SSML speech marks are not supported for plain text-type input. * @throws LanguageNotSupportedException * The language specified is not currently supported by Amazon Polly in this capacity. * @throws EngineNotSupportedException * This engine is not compatible with the voice that you have designated. Choose a new voice that is * compatible with the engine or change the engine and restart the operation. * @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 PollyException * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample PollyClient.SynthesizeSpeech * @see AWS API * Documentation */ @Override public ReturnT synthesizeSpeech(SynthesizeSpeechRequest synthesizeSpeechRequest, ResponseTransformer responseTransformer) throws TextLengthExceededException, InvalidSampleRateException, InvalidSsmlException, LexiconNotFoundException, ServiceFailureException, MarksNotSupportedForFormatException, SsmlMarksNotSupportedForTextTypeException, LanguageNotSupportedException, EngineNotSupportedException, AwsServiceException, SdkClientException, PollyException { JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(true) .isPayloadJson(false).build(); HttpResponseHandler responseHandler = protocolFactory.createResponseHandler(operationMetadata, SynthesizeSpeechResponse::builder); HttpResponseHandler errorResponseHandler = createErrorResponseHandler(protocolFactory, operationMetadata); SdkClientConfiguration clientConfiguration = updateSdkClientConfiguration(synthesizeSpeechRequest, this.clientConfiguration); List metricPublishers = resolveMetricPublishers(clientConfiguration, synthesizeSpeechRequest .overrideConfiguration().orElse(null)); MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector .create("ApiCall"); try { apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Polly"); apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "SynthesizeSpeech"); return clientHandler.execute( new ClientExecutionParams() .withOperationName("SynthesizeSpeech").withProtocolMetadata(protocolMetadata) .withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler) .withRequestConfiguration(clientConfiguration).withInput(synthesizeSpeechRequest) .withMetricCollector(apiCallMetricCollector) .withMarshaller(new SynthesizeSpeechRequestMarshaller(protocolFactory)), responseTransformer); } finally { metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect())); } } @Override public final String serviceName() { return SERVICE_NAME; } 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 void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { ClientOverrideConfiguration.Builder builder = configuration.asOverrideConfigurationBuilder(); RetryMode retryMode = builder.retryMode(); if (retryMode != null) { configuration.option(SdkClientOption.RETRY_STRATEGY, AwsRetryStrategy.forRetryMode(retryMode)); } else { Consumer> configurator = builder.retryStrategyConfigurator(); if (configurator != null) { RetryStrategy.Builder defaultBuilder = AwsRetryStrategy.defaultRetryStrategy().toBuilder(); configurator.accept(defaultBuilder); configuration.option(SdkClientOption.RETRY_STRATEGY, defaultBuilder.build()); } else { RetryStrategy retryStrategy = builder.retryStrategy(); if (retryStrategy != null) { configuration.option(SdkClientOption.RETRY_STRATEGY, retryStrategy); } } } configuration.option(SdkClientOption.CONFIGURED_RETRY_MODE, null); configuration.option(SdkClientOption.CONFIGURED_RETRY_STRATEGY, null); configuration.option(SdkClientOption.CONFIGURED_RETRY_CONFIGURATOR, null); } private SdkClientConfiguration updateSdkClientConfiguration(SdkRequest request, SdkClientConfiguration clientConfiguration) { List plugins = request.overrideConfiguration().map(c -> c.plugins()).orElse(Collections.emptyList()); SdkClientConfiguration.Builder configuration = clientConfiguration.toBuilder(); if (plugins.isEmpty()) { return configuration.build(); } PollyServiceClientConfigurationBuilder serviceConfigBuilder = new PollyServiceClientConfigurationBuilder(configuration); for (SdkPlugin plugin : plugins) { plugin.configureClient(serviceConfigBuilder); } updateRetryStrategyClientConfiguration(configuration); return configuration.build(); } private > T init(T builder) { return builder .clientConfiguration(clientConfiguration) .defaultServiceExceptionSupplier(PollyException::builder) .protocol(AwsJsonProtocol.REST_JSON) .protocolVersion("1.1") .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidS3BucketException") .exceptionBuilderSupplier(InvalidS3BucketException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidSampleRateException") .exceptionBuilderSupplier(InvalidSampleRateException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("SynthesisTaskNotFoundException") .exceptionBuilderSupplier(SynthesisTaskNotFoundException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("MaxLexiconsNumberExceededException") .exceptionBuilderSupplier(MaxLexiconsNumberExceededException::builder).httpStatusCode(400) .build()) .registerModeledException( ExceptionMetadata.builder().errorCode("MaxLexemeLengthExceededException") .exceptionBuilderSupplier(MaxLexemeLengthExceededException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("UnsupportedPlsAlphabetException") .exceptionBuilderSupplier(UnsupportedPlsAlphabetException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("LanguageNotSupportedException") .exceptionBuilderSupplier(LanguageNotSupportedException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("TextLengthExceededException") .exceptionBuilderSupplier(TextLengthExceededException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidLexiconException") .exceptionBuilderSupplier(InvalidLexiconException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("SsmlMarksNotSupportedForTextTypeException") .exceptionBuilderSupplier(SsmlMarksNotSupportedForTextTypeException::builder).httpStatusCode(400) .build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidS3KeyException") .exceptionBuilderSupplier(InvalidS3KeyException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidSnsTopicArnException") .exceptionBuilderSupplier(InvalidSnsTopicArnException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("MarksNotSupportedForFormatException") .exceptionBuilderSupplier(MarksNotSupportedForFormatException::builder).httpStatusCode(400) .build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidTaskIdException") .exceptionBuilderSupplier(InvalidTaskIdException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("UnsupportedPlsLanguageException") .exceptionBuilderSupplier(UnsupportedPlsLanguageException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("LexiconNotFoundException") .exceptionBuilderSupplier(LexiconNotFoundException::builder).httpStatusCode(404).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("LexiconSizeExceededException") .exceptionBuilderSupplier(LexiconSizeExceededException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidSsmlException") .exceptionBuilderSupplier(InvalidSsmlException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("InvalidNextTokenException") .exceptionBuilderSupplier(InvalidNextTokenException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("EngineNotSupportedException") .exceptionBuilderSupplier(EngineNotSupportedException::builder).httpStatusCode(400).build()) .registerModeledException( ExceptionMetadata.builder().errorCode("ServiceFailureException") .exceptionBuilderSupplier(ServiceFailureException::builder).httpStatusCode(500).build()); } @Override public final PollyServiceClientConfiguration serviceClientConfiguration() { return new PollyServiceClientConfigurationBuilder(this.clientConfiguration.toBuilder()).build(); } @Override public void close() { clientHandler.close(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy