com.adobe.platform.operation.ClientConfig Maven / Gradle / Ivy
/*
* 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.platform.operation;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.adobe.platform.operation.internal.InternalClientConfig;
import com.adobe.platform.operation.internal.util.JsonUtil;
import com.adobe.platform.operation.internal.util.StringUtil;
/**
* Encapsulates the API request configurations
*/
public class ClientConfig {
private static String V2_SERVICES_KEY = "v2Services";
private static String V2_SERVICES_PREDICT_KEY = "v2PredictUri";
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 v2ServicesPredictUri;
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 v2ServicesConfig = clientConfig.get(ClientConfig.V2_SERVICES_KEY);
if (v2ServicesConfig != null) {
JsonNode v2ServicesPredictUriNode = v2ServicesConfig.get(ClientConfig.V2_SERVICES_PREDICT_KEY);
if (v2ServicesPredictUriNode != null) {
this.v2ServicesPredictUri = v2ServicesPredictUriNode.asText();
}
}
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.v2ServicesPredictUri, this.cpfOpsCreateUri);
}
}
}