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

com.adobe.pdfservices.operation.ClientConfig Maven / Gradle / Ivy

Go to download

Adobe PDF Services SDK allows you to access RESTful APIs to create, convert, and manipulate PDFs within your applications.

There is a newer version: 4.1.0
Show newest version
/*
 * Copyright 2019 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */

package com.adobe.pdfservices.operation;

import java.io.File;
import java.io.IOException;

import com.adobe.pdfservices.operation.internal.InternalClientConfig;
import com.adobe.pdfservices.operation.internal.util.JsonUtil;
import com.adobe.pdfservices.operation.internal.util.StringUtil;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * Encapsulates the API request configurations
 */
public class ClientConfig {

    private static String CPF_SERVICES_KEY = "cpfServices";
    private static String CPF_SERVICES_OPS_CREATE_KEY = "cpfOpsCreateUri";
    private static String CONNECT_TIMEOUT_KEY = "connectTimeout";
    private static String SOCKET_TIMEOUT_KEY = "socketTimeout";

    /**
     * Creates a new {@code ClientConfig} builder.
     *
     * @return a {@code ClientConfig.Builder} instance
     */
    public static ClientConfig.Builder builder(){
        return new ClientConfig.Builder();
    }

    /**
     * Builds a {@link ClientConfig} instance.
     */
    public static class Builder {
        private Integer connectTimeout;
        private Integer socketTimeout;
        private String cpfOpsCreateUri;

        /**
         * Constructs a {@code Builder} instance.
         */
        public Builder(){
        }

        /**
         * Sets the connect timeout. It should be greater than zero.
         *
         * @param connectTimeout determines the timeout in milliseconds until a connection is established in the API calls. Default value is 10000 milliseconds
         * @return this Builder instance to add any additional parameters
         */
        public ClientConfig.Builder withConnectTimeout(Integer connectTimeout){
            this.connectTimeout = connectTimeout;
            return this;
        }

        /**
         * Sets the socket timeout. It should be greater than zero.
         *
         * @param socketTimeout Defines the socket timeout in milliseconds, which is the timeout for waiting for data or,
         *                      put differently, a maximum period inactivity between two consecutive data packets).
         *                      Default value is 2000 milliseconds
         * @return this Builder instance to add any additional parameters
         */
        public ClientConfig.Builder withSocketTimeout(Integer socketTimeout){
            this.socketTimeout = socketTimeout;
            return this;
        }

        /**
         * Sets the connect timeout and socket timeout using the JSON client config file path.
         * All the keys in the JSON structure are optional.
         * 

* JSON structure: *

{
         *   "connectTimeout": "4000",
         *   "socketTimeout": "20000"
         * }
* * @return this Builder instance to add any additional parameters */ public ClientConfig.Builder fromFile(String clientConfigFilePath) { try { JsonNode clientConfig = JsonUtil.readJsonTreeFromFile(new File(clientConfigFilePath)); JsonNode cpfServicesConfig = clientConfig.get(ClientConfig.CPF_SERVICES_KEY); if (cpfServicesConfig != null) { JsonNode cpfServicesOpsCreateUriNode = cpfServicesConfig.get(ClientConfig.CPF_SERVICES_OPS_CREATE_KEY); if (cpfServicesOpsCreateUriNode != null) { this.cpfOpsCreateUri = cpfServicesOpsCreateUriNode.asText(); } } JsonNode connectTimeoutNode = clientConfig.get(ClientConfig.CONNECT_TIMEOUT_KEY); if (connectTimeoutNode != null) { if (StringUtil.isPositiveInteger(connectTimeoutNode.asText())) { this.connectTimeout = connectTimeoutNode.asInt(); } else { throw new IllegalArgumentException(String.format("Invalid value for connect timeout %s Must be valid integer greater than 0", connectTimeoutNode.asText())); } } JsonNode socketTimeoutNode = clientConfig.get(ClientConfig.SOCKET_TIMEOUT_KEY); if (socketTimeoutNode != null) { if (StringUtil.isPositiveInteger(socketTimeoutNode.asText())) { this.socketTimeout = socketTimeoutNode.asInt(); } else { throw new IllegalArgumentException(String.format("Invalid value for socket timeout %s. Must be valid integer greater than 0", socketTimeoutNode.asText())); } } return this; } catch (IOException ioException) { throw new IllegalArgumentException("Invalid Client config file provided."); } } /** * Returns a new {@link ClientConfig} instance built from the current state of this builder. * * @return a new {@code ClientConfig} instance */ public ClientConfig build(){ return new InternalClientConfig(this.connectTimeout, this.socketTimeout, this.cpfOpsCreateUri); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy