<commit_before>/*
 * ******************************************************************************
 *   Copyright 2014 Spectra Logic Corporation. 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://www.apache.org/licenses/LICENSE-2.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 com.spectralogic.ds3client;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.SignatureException;

import com.spectralogic.ds3client.commands.*;
import com.spectralogic.ds3client.models.Credentials;
import com.spectralogic.ds3client.networking.NetworkClient;

/**
 * The main class for communicating with a DS3 appliance.  All communication with a DS3 appliance should start with
 * this class.
 *
 * Here is an example showing how the Ds3Client class is used to get a list of buckets from a remote DS3 appliance.
 *
 * <pre>
 *     {@code
 *     final Ds3Client client = Ds3Client.builder("ds3Endpoint:8080",
 *                                  new Credentials("accessKey", "secretKey")).build();
 *
 *     final GetServiceResponse response = client.getService(new GetServiceRequest());
 *
 *     for(final Bucket bucket: response.getResult().getBuckets()) {
 *         System.out.println(bucket.getName());
 *     }
 *     }
 * </pre>
 */
public class Ds3Client {

    /**
     * A Builder class used to create a Ds3Client instance.
     */
    public static class Builder implements com.spectralogic.ds3client.utils.Builder<Ds3Client> {

        final private String endpoint;
        final private Credentials credentials;

        private boolean secure = true;
        private URI proxy = null;
        private int retries = 5;

        private Builder(final String endpoint, final Credentials credentials) throws IllegalArgumentException {
            if (endpoint == null || endpoint.isEmpty()) {
                throw new IllegalArgumentException("Endpoint must be non empty");
            }
            if(credentials == null || !credentials.isValid()) {
                throw new IllegalArgumentException("Credentials must be filled out.");
            }
            this.endpoint = endpoint;
            this.credentials = credentials;
        }

        /**
         * Specifies if the library should use HTTP or HTTPS.  The default is HTTP.
         * @param secure True will use HTTPS, false will use HTTP.
         * @return The current builder.
         */
        public Builder withHttpSecure(final boolean secure) {
            this.secure = secure;
            return this;
        }

        /**
         * Sets a HTTP proxy.
         * @param proxy The endpoint of the HTTP proxy.
         * @return The current builder.
         * @throws IllegalArgumentException This will be thrown if the proxy endpoint is not a valid URI.
         */
        public Builder withProxy(final String proxy) throws IllegalArgumentException {
            try {
                final URI proxyUri;
                if(!proxy.startsWith("http")) {
                    throw new IllegalArgumentException("Invalid proxy format.  The web address must start with either http or https.");
                }
                proxyUri = new URI(proxy);

                this.proxy = proxyUri;
            } catch (final URISyntaxException e) {
                throw new IllegalArgumentException("Invalid proxy format.  Must be a web address.");
            }

            return this;
        }

        /**
         * Sets the number of retries the library will attempt to perform when it receives 307 redirects from a
         * DS3 appliance.  The default is 5.
         * @param retries The number of times the library should perform retries on 307.
         * @return The current builder.
         */
        public Builder withRedirectRetries(final int retries) {
            this.retries = retries;
            return this;
        }

        /**
         * Returns a new Ds3Client instance.
         */
        @Override
        public Ds3Client build() {
            final ConnectionDetailsImpl.Builder connBuilder = ConnectionDetailsImpl.builder(endpoint, credentials)
                .withProxy(proxy).withSecure(secure).withRedirectRetries(retries);

            final NetworkClient netClient = new NetworkClientImpl(connBuilder.build());
            return new Ds3Client(netClient);
        }
    }

    /**
     * Returns a Builder which is used to customize the behavior of the Ds3Client library.
     * @param endpoint The DS3 endpoint the library should connect to.
     * @param creds The {@link Credentials} used for connecting to a DS3 endpoint.
     * @return The Builder for the {@link Ds3Client} object.
     */
    public static Builder builder(final String endpoint, final Credentials creds) {
        return new Builder(endpoint, creds);
    }
    
    private final NetworkClient netClient;

    Ds3Client(final NetworkClient netClient) {
        this.netClient = netClient;
    }

    NetworkClient getNetClient() {
        return netClient;
    }

    /**
     * Gets the list of buckets.
     * @param request The Service Request object used to customize the HTTP request, {@link GetServiceRequest}
     * @return The response object contains the list of buckets that the user has access to.
     * @throws IOException
     * @throws SignatureException
     */
    public GetServiceResponse getService(final GetServiceRequest request) throws IOException, SignatureException {
        return new GetServiceResponse(netClient.getResponse(request));
    }

    /**
     * Gets the list of objects in a bucket.
     * @param request The Get Bucket Request object used to customize the HTTP request.  The bucket request object has
     *                several options for customizing the request.  See {@link GetBucketRequest} for the full list of options
     *                that can be configured.
     * @return The response object contains the list of objects that a bucket contains.  There is some additional
     *         information that is returned which is used for pagination, {@link GetBucketResponse} for the full
     *         list of properties
     * that are returned.
     * @throws IOException
     * @throws SignatureException
     */
    public GetBucketResponse getBucket(final GetBucketRequest request) throws IOException, SignatureException {
        return new GetBucketResponse(netClient.getResponse(request));
    }

    /**
     * Puts a new bucket to a DS3 endpoint
     * @param request The Put Bucket Request object used to customize the HTTP request.  The put bucket request object
     *                has some options for customizing the request.  See {@link PutBucketRequest} for the full list of
     *                options that can be configured.
     * @return The response object is returned primarily to be consistent with the rest of the API.  Additional data
     *         may be returned here in the future but nothing is currently.  See {@link PutBucketResponse} for the most
     *         up to date information on what is returned.
     * @throws IOException
     * @throws SignatureException
     */
    public PutBucketResponse putBucket(final PutBucketRequest request) throws IOException, SignatureException {
        return new PutBucketResponse(netClient.getResponse(request));
    }

    /**
     * Performs an HTTP HEAD for a bucket.  The HEAD will return information about if the bucket exists, or if the user
     * has access to that bucket.
     * @param request The Head Bucket Request object used to customize the HTTP request.  See {@link HeadBucketRequest}.
     * @return The response object is returned and contains the status of the bucket.  See {@link HeadBucketResponse} for
     *         the full list of status that a bucket can be in.
     * @throws IOException
     * @throws SignatureException
     */
    public HeadBucketResponse headBucket(final HeadBucketRequest request) throws IOException, SignatureException {
        return new HeadBucketResponse(netClient.getResponse(request));
    }

    /**
     * Deletes a bucket from a DS3 endpoint.  <b>Note:</b> all objects must be deleted first before deleteBucket will
     * succeed.
     * @param request The Delete Bucket Request object used to customize the HTTP request.  The delete bucket request object
     *                has some options for customizing the request.  See {@link DeleteBucketRequest} for the full list of
     *                options that can be configured.
     * @return The response object is returned primarily to be consistent with the rest of the API.  Additional data
     *         may be returned here in the future but nothing is currently.  See {@link DeleteBucketResponse} for the most
     *         up to date information on what is returned.
     * @throws IOException
     * @throws SignatureException
     */
    public DeleteBucketResponse deleteBucket(final DeleteBucketRequest request) throws IOException, SignatureException {<commit_msg>Fixed a minor spelling mistake.<commit_after>/*
 * ******************************************************************************
 *   Copyright 2014 Spectra Logic Corporation. 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://www.apache.org/licenses/LICENSE-2.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 com.spectralogic.ds3client;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.SignatureException;

import com.spectralogic.ds3client.commands.*;
import com.spectralogic.ds3client.models.Credentials;
import com.spectralogic.ds3client.networking.NetworkClient;

/**
 * The main class for communicating with a DS3 appliance.  All communication with a DS3 appliance should start with
 * this class.
 *
 * Here is an example showing how the Ds3Client class is used to get a list of buckets from a remote DS3 appliance.
 *
 * <pre>
 *     {@code
 *     final Ds3Client client = Ds3Client.builder("ds3Endpoint:8080",
 *                                  new Credentials("accessKey", "secretKey")).build();
 *
 *     final GetServiceResponse response = client.getService(new GetServiceRequest());
 *
 *     for(final Bucket bucket: response.getResult().getBuckets()) {
 *         System.out.println(bucket.getName());
 *     }
 *     }
 * </pre>
 */
public class Ds3Client {

    /**
     * A Builder class used to create a Ds3Client instance.
     */
    public static class Builder implements com.spectralogic.ds3client.utils.Builder<Ds3Client> {

        final private String endpoint;
        final private Credentials credentials;

        private boolean secure = true;
        private URI proxy = null;
        private int retries = 5;

        private Builder(final String endpoint, final Credentials credentials) throws IllegalArgumentException {
            if (endpoint == null || endpoint.isEmpty()) {
                throw new IllegalArgumentException("Endpoint must be non empty");
            }
            if(credentials == null || !credentials.isValid()) {
                throw new IllegalArgumentException("Credentials must be filled out.");
            }
            this.endpoint = endpoint;
            this.credentials = credentials;
        }

        /**
         * Specifies if the library should use HTTP or HTTPS.  The default is HTTP.
         * @param secure True will use HTTPS, false will use HTTP.
         * @return The current builder.
         */
        public Builder withHttpSecure(final boolean secure) {
            this.secure = secure;
            return this;
        }

        /**
         * Sets a HTTP proxy.
         * @param proxy The endpoint of the HTTP proxy.
         * @return The current builder.
         * @throws IllegalArgumentException This will be thrown if the proxy endpoint is not a valid URI.
         */
        public Builder withProxy(final String proxy) throws IllegalArgumentException {
            try {
                final URI proxyUri;
                if(!proxy.startsWith("http")) {
                    throw new IllegalArgumentException("Invalid proxy format.  The web address must start with either http or https.");
                }
                proxyUri = new URI(proxy);

                this.proxy = proxyUri;
            } catch (final URISyntaxException e) {
                throw new IllegalArgumentException("Invalid proxy format.  Must be a web address.");
            }

            return this;
        }

        /**
         * Sets the number of retries the library will attempt to perform when it receives 307 redirects from a
         * DS3 appliance.  The default is 5.
         * @param retries The number of times the library should perform retries on 307.
         * @return The current builder.
         */
        public Builder withRedirectRetries(final int retries) {
            this.retries = retries;
            return this;
        }

        /**
         * Returns a new Ds3Client instance.
         */
        @Override
        public Ds3Client build() {
            final ConnectionDetailsImpl.Builder connBuilder = ConnectionDetailsImpl.builder(endpoint, credentials)
                .withProxy(proxy).withSecure(secure).withRedirectRetries(retries);

            final NetworkClient netClient = new NetworkClientImpl(connBuilder.build());
            return new Ds3Client(netClient);
        }
    }

    /**
     * Returns a Builder which is used to customize the behavior of the Ds3Client library.
     * @param endpoint The DS3 endpoint the library should connect to.
     * @param creds The {@link Credentials} used for connecting to a DS3 endpoint.
     * @return The Builder for the {@link Ds3Client} object.
     */
    public static Builder builder(final String endpoint, final Credentials creds) {
        return new Builder(endpoint, creds);
    }
    
    private final NetworkClient netClient;

    Ds3Client(final NetworkClient netClient) {
        this.netClient = netClient;
    }

    NetworkClient getNetClient() {
        return netClient;
    }

    /**
     * Gets the list of buckets.
     * @param request The Service Request object used to customize the HTTP request, {@link GetServiceRequest}
     * @return The response object contains the list of buckets that the user has access to.
     * @throws IOException
     * @throws SignatureException
     */
    public GetServiceResponse getService(final GetServiceRequest request) throws IOException, SignatureException {
        return new GetServiceResponse(netClient.getResponse(request));
    }

    /**
     * Gets the list of objects in a bucket.
     * @param request The Get Bucket Request object used to customize the HTTP request.  The bucket request object has
     *                several options for customizing the request.  See {@link GetBucketRequest} for the full list of options
     *                that can be configured.
     * @return The response object contains the list of objects that a bucket contains.  There is some additional
     *         information that is returned which is used for pagination, {@link GetBucketResponse} for the full
     *         list of properties
     * that are returned.
     * @throws IOException
     * @throws SignatureException
     */
    public GetBucketResponse getBucket(final GetBucketRequest request) throws IOException, SignatureException {
        return new GetBucketResponse(netClient.getResponse(request));
    }

    /**
     * Puts a new bucket to a DS3 endpoint
     * @param request The Put Bucket Request object used to customize the HTTP request.  The put bucket request object
     *                has some options for customizing the request.  See {@link PutBucketRequest} for the full list of
     *                options that can be configured.
     * @return The response object is returned primarily to be consistent with the rest of the API.  Additional data
     *         may be returned here in the future but nothing is currently.  See {@link PutBucketResponse} for the most
     *         up to date information on what is returned.
     * @throws IOException
     * @throws SignatureException
     */
    public PutBucketResponse putBucket(final PutBucketRequest request) throws IOException, SignatureException {
        return new PutBucketResponse(netClient.getResponse(request));
    }

    /**
     * Performs a HTTP HEAD for a bucket.  The HEAD will return information about if the bucket exists, or if the user
     * has access to that bucket.
     * @param request The Head Bucket Request object used to customize the HTTP request.  See {@link HeadBucketRequest}.
     * @return The response object is returned and contains the status of the bucket.  See {@link HeadBucketResponse} for
     *         the full list of status that a bucket can be in.
     * @throws IOException
     * @throws SignatureException
     */
    public HeadBucketResponse headBucket(final HeadBucketRequest request) throws IOException, SignatureException {
        return new HeadBucketResponse(netClient.getResponse(request));
    }

    /**
     * Deletes a bucket from a DS3 endpoint.  <b>Note:</b> all objects must be deleted first before deleteBucket will
     * succeed.
     * @param request The Delete Bucket Request object used to customize the HTTP request.  The delete bucket request object
     *                has some options for customizing the request.  See {@link DeleteBucketRequest} for the full list of
     *                options that can be configured.
     * @return The response object is returned primarily to be consistent with the rest of the API.  Additional data
     *         may be returned here in the future but nothing is currently.  See {@link DeleteBucketResponse} for the most
     *         up to date information on what is returned.
     * @throws IOException
     * @throws SignatureException
     */
    public DeleteBucketResponse deleteBucket(final DeleteBucketRequest request) throws IOException, SignatureException {
{'fraction_non_alphanumeric': 0.08063070189316036, 'fraction_numerical': 0.0048515530243104996, 'mean_word_length': 2.5646616541353384, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3223549', 'n_tokens_mistral': 4530, 'n_tokens_neox': 4352, 'n_words': 1874}