<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 org.apache.camel.component.aws2.athena;

import java.util.Map;

import org.apache.camel.health.HealthCheckResultBuilder;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.apache.camel.util.ObjectHelper;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.athena.AthenaClient;
import software.amazon.awssdk.services.athena.AthenaClientBuilder;

public class Athena2ClientHealthCheck extends AbstractHealthCheck {

    private final Athena2Endpoint athena2Endpoint;
    private final String clientId;

    public Athena2ClientHealthCheck(Athena2Endpoint athena2Endpoint, String clientId) {
        super("camel", "aws2-athena-client-" + clientId);
        this.athena2Endpoint = athena2Endpoint;
        this.clientId = clientId;
    }

    @Override
    public boolean isLiveness() {
        // this health check is only readiness
        return false;
    }

    @Override
    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {

        try {
            if (!AthenaClient.serviceMetadata().regions().contains(Region.of(athena2Endpoint.getConfiguration().getRegion()))) {
                builder.message("The service is not supported in this region");
                builder.down();
                return;
            }
            AthenaClient client;
            if (!athena2Endpoint.getConfiguration().isUseDefaultCredentialsProvider()) {
                AwsBasicCredentials cred = AwsBasicCredentials.create(athena2Endpoint.getConfiguration().getAccessKey(),
                        athena2Endpoint.getConfiguration().getSecretKey());
                AthenaClientBuilder clientBuilder = AthenaClient.builder();
                client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred))
                        .region(Region.of(athena2Endpoint.getConfiguration().getRegion())).build();
            } else if (ObjectHelper.isNotEmpty(athena2Endpoint.getConfiguration().getAmazonAthenaClient())) {
                client = athena2Endpoint.getConfiguration().getAmazonAthenaClient();
            } else {
                AthenaClientBuilder clientBuilder = AthenaClient.builder();
                client = clientBuilder.region(Region.of(athena2Endpoint.getConfiguration().getRegion())).build();
            }
            client.listQueryExecutions();
        } catch (AwsServiceException e) {
            builder.message(e.getMessage());
            builder.error(e);
            builder.down();
            return;

        } catch (Exception e) {
            builder.error(e);
            builder.down();
            return;
        }
        builder.up();
    }
}<commit_msg>CAMEL-18643 - AWS Health Check: Use AwsServiceException instead of SdkClientException for health check - AWS Athena<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 org.apache.camel.component.aws2.athena;

import java.util.Map;

import org.apache.camel.health.HealthCheckResultBuilder;
import org.apache.camel.impl.health.AbstractHealthCheck;
import org.apache.camel.util.ObjectHelper;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.athena.AthenaClient;
import software.amazon.awssdk.services.athena.AthenaClientBuilder;

public class Athena2ClientHealthCheck extends AbstractHealthCheck {

    private final Athena2Endpoint athena2Endpoint;
    private final String clientId;

    public Athena2ClientHealthCheck(Athena2Endpoint athena2Endpoint, String clientId) {
        super("camel", "aws2-athena-client-" + clientId);
        this.athena2Endpoint = athena2Endpoint;
        this.clientId = clientId;
    }

    @Override
    public boolean isLiveness() {
        // this health check is only readiness
        return false;
    }

    @Override
    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {

        try {
            if (!AthenaClient.serviceMetadata().regions().contains(Region.of(athena2Endpoint.getConfiguration().getRegion()))) {
                builder.message("The service is not supported in this region");
                builder.down();
                return;
            }
            AthenaClient client;
            if (!athena2Endpoint.getConfiguration().isUseDefaultCredentialsProvider()) {
                AwsBasicCredentials cred = AwsBasicCredentials.create(athena2Endpoint.getConfiguration().getAccessKey(),
                        athena2Endpoint.getConfiguration().getSecretKey());
                AthenaClientBuilder clientBuilder = AthenaClient.builder();
                client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred))
                        .region(Region.of(athena2Endpoint.getConfiguration().getRegion())).build();
            } else if (ObjectHelper.isNotEmpty(athena2Endpoint.getConfiguration().getAmazonAthenaClient())) {
                client = athena2Endpoint.getConfiguration().getAmazonAthenaClient();
            } else {
                AthenaClientBuilder clientBuilder = AthenaClient.builder();
                client = clientBuilder.region(Region.of(athena2Endpoint.getConfiguration().getRegion())).build();
            }
            client.listQueryExecutions();
        } catch (AwsServiceException e) {
            builder.message(e.getMessage());
            builder.error(e);
            if (ObjectHelper.isNotEmpty(e.statusCode())) {
                builder.detail(SERVICE_STATUS_CODE, e.statusCode());
            }
            if (ObjectHelper.isNotEmpty(e.awsErrorDetails().errorCode())) {
                builder.detail(SERVICE_ERROR_CODE, e.awsErrorDetails().errorCode());
            }
            builder.down();
            return;

        } catch (Exception e) {
            builder.error(e);
            builder.down();
            return;
        }
        builder.up();
    }
}
{'fraction_non_alphanumeric': 0.09036679781698186, 'fraction_numerical': 0.006219063332910267, 'mean_word_length': 3.030690537084399, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6446234', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1998, 'n_words': 487}