
com.nutanix.sec.java.client.ApiClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of security-java-client Show documentation
Show all versions of security-java-client Show documentation
Manage security features, such as encryption, certificates, or platform hardening.
The newest version!
package com.nutanix.sec.java.client;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.nutanix.json.deserializers.ObjectTypeTypedObject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.RequestEntity.BodyBuilder;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.time.Duration;
import java.util.*;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.lang.reflect.Method;
import com.nutanix.sec.java.client.auth.Authentication;
import com.nutanix.sec.java.client.auth.HttpBasicAuth;
import com.nutanix.sec.java.client.auth.ApiKeyAuth;
import com.nutanix.sec.java.client.auth.OAuth;
import javax.net.ssl.SSLContext;
@Slf4j
@javax.annotation.Generated(value = "com.nutanix.swagger.codegen.generators.JavaClientSDKGenerator", date = "2024-12-05T00:55:59.712Z[Etc/UTC]")@Component("com.nutanix.sec.java.client.ApiClient")
public class ApiClient {
public enum CollectionFormat {
CSV(","), TSV("\t"), SSV(" "), PIPES("|"), MULTI(null);
private final String separator;
private CollectionFormat(String separator) {
this.separator = separator;
}
private String collectionToString(Collection extends CharSequence> collection) {
return collection == null ? "" : StringUtils.join(collection, separator);
}
}
private final int MAX_RETRY = 5;
private final int RETRY_DELAY = 3000;
private final long DEFAULT_READ_TIMEOUT = 30000;
private final long DEFAULT_CONNECT_TIMEOUT = 30000;
private final long MAX_DEFAULT_TIMEOUT = 10800000;
private boolean debugging = false;
private HttpHeaders defaultHeaders = new HttpHeaders();
private String cookie;
private boolean refreshCookie = true;
private String scheme = "https";
private String host = "localhost";
private int port = 9440;
private RestTemplate restTemplate;
private RetryTemplate retryTemplate;
private int maxRetryAttempts = MAX_RETRY;
private int retryInterval = RETRY_DELAY;
private long readTimeout = DEFAULT_READ_TIMEOUT;
private long connectTimeout = DEFAULT_CONNECT_TIMEOUT;
private Map authentications;
private HttpStatus statusCode;
private MultiValueMap responseHeaders;
private DateFormat dateFormat;
private String downloadDirectory = Paths.get("").toAbsolutePath().toString();
private final Set binaryMediaTypes = new HashSet<>(Arrays.asList
("application/octet-stream", "application/pdf", "application/zip"));
private final Set textMediaTypes = new HashSet<>(Arrays.asList
("text/event-stream", "text/html", "text/xml", "text/csv", "text/javascript", "text/markdown", "text/vcard"));
private boolean verifySsl = true;
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public ApiClient() {
this.restTemplate = buildRestTemplate();
this.retryTemplate = buildRetryTemplate();
init();
}
@Autowired
public ApiClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
this.retryTemplate = buildRetryTemplate();
init();
}
private void init() {
// Use RFC3339 format for date and datetime.
// See http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14
this.dateFormat = new RFC3339DateFormat();
// Use UTC as the default time zone.
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// Set default User-Agent.
addDefaultHeader("User-Agent", "Nutanix-security-java-client/4.0.1-beta-1");
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap();
authentications.put("apiKeyAuthScheme", (Authentication) new ApiKeyAuth("header", "X-ntnx-api-key"));
authentications.put("basicAuthScheme", (Authentication) new HttpBasicAuth());
// Prevent the authentications from being modified.
authentications = Collections.unmodifiableMap(authentications);
}
/**
* Enable/Disable SSL Verification
* @param verifySsl flag
* @throws KeyStoreException if key is not found.
* @throws NoSuchAlgorithmException if requested cryptographic algorithm is not found.
* @return ApiClient this client
*/
public ApiClient setVerifySsl(boolean verifySsl) throws KeyStoreException, NoSuchAlgorithmException {
this.verifySsl = verifySsl;
this.restTemplate = buildRestTemplate();
return this;
}
/**
* Get the current URL scheme for making a connection to the cluster
* @return String URL scheme
*/
public String getScheme() {
return scheme;
}
/**
* Set a URI scheme for connecting to the cluster (HTTP or HTTPS using SSL/TLS)
* @param scheme URL scheme
* @return ApiClient this client
*/
public ApiClient setScheme(String scheme) {
this.scheme = scheme;
return this;
}
/**
* Get the current hostname or base URL
* @return String the hostname
*/
public String getHost() {
return host;
}
/**
* Set the hostname for base URL
* @param host the hostname
* @return ApiClient this client
*/
public ApiClient setHost(String host) {
this.host = host;
return this;
}
/**
* Get the current response read timeout
* @return long the response read timeout
*/
public long getReadTimeout() {
return readTimeout;
}
/**
* Set the response read timeout
* @param readTimeout the response read timeout
* @return ApiClient this client
*/
public ApiClient setReadTimeout(long readTimeout) {
this.readTimeout = readTimeout;
this.restTemplate = buildRestTemplate();
return this;
}
/**
* Get the current connection timeout
* @return long the connection timeout
*/
public long getConnectTimeout() {
return connectTimeout;
}
/**
* Set the connection timeout
* @param connectTimeout the connection timeout
* @return ApiClient this client
*/
public ApiClient setConnectTimeout(long connectTimeout) {
this.connectTimeout = connectTimeout;
this.restTemplate = buildRestTemplate();
return this;
}
private long getValidTimeout(long timeout, long defaultTimeout) {
if (timeout <= 0) {
timeout = defaultTimeout;
} else if (timeout > MAX_DEFAULT_TIMEOUT) {
timeout = MAX_DEFAULT_TIMEOUT;
}
return timeout;
}
/**
* Get the current port for base URL
* @return int the port
*/
public int getPort() {
return port;
}
/**
* Set the port for base URL, which should exclude the semicolon (Default port: 9440)
* @param port the port
* @return ApiClient this client
*/
public ApiClient setPort(int port) {
this.port = port;
return this;
}
/**
* Get the number of max retry attempts
* @return int number of max retry attempts
*/
public int getMaxRetryAttempts() {
return maxRetryAttempts;
}
/**
* Set the number of max retry attempts
* @param maxRetryAttempts number of max retry attempts
* @return ApiClient this client
*/
public ApiClient setMaxRetryAttempts(int maxRetryAttempts) {
this.maxRetryAttempts = maxRetryAttempts;
this.retryTemplate = buildRetryTemplate();
return this;
}
/**
* Get the delay between each retry attempt
* @return int back off period in milliseconds
*/
public int getRetryInterval() {
return retryInterval;
}
/**
* Set the delay between each retry attempt
* @param retryInterval delay between each retry attempt in milliseconds
* @return ApiClient this client
*/
public ApiClient setRetryInterval(int retryInterval) {
this.retryInterval = retryInterval;
this.retryTemplate = buildRetryTemplate();
return this;
}
/**
* Directory path to store downloaded files
* @param path the path of the directory
*/
public void setDownloadDirectory(String path) {
this.downloadDirectory = path;
}
/**
* Gets the status code of the previous request
* @return HttpStatus the status code
*/
public HttpStatus getStatusCode() {
return statusCode;
}
/**
* Gets the response headers of the previous request
* @return MultiValueMap a map of response headers
*/
public MultiValueMap getResponseHeaders() {
return responseHeaders;
}
/**
* Get authentications (key: authentication name, value: authentication).
* @return Map the currently configured authentication types
*/
public Map getAuthentications() {
return authentications;
}
/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}
/**
* Helper method to set API key value for the first API key authentication.
* @param apiKey the API key
*/
public void setApiKey(String apiKey) {
for (Authentication auth : authentications.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKey(apiKey);
return;
}
}
throw new RuntimeException("No API key authentication is configured!");
}
/**
* Helper method to set API key prefix for the first API key authentication.
* @param apiKeyPrefix the API key prefix
*/
public void setApiKeyPrefix(String apiKeyPrefix) {
for (Authentication auth : authentications.values()) {
if (auth instanceof ApiKeyAuth) {
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
return;
}
}
throw new RuntimeException("No API key authentication configured!");
}
/**
* Helper method to set username for the first HTTP basic authentication.
* @param username the username
*/
public void setUsername(String username) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setUsername(username);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Helper method to set password for the first HTTP basic authentication.
* @param password the password
*/
public void setPassword(String password) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBasicAuth) {
((HttpBasicAuth) auth).setPassword(password);
return;
}
}
throw new RuntimeException("No HTTP basic authentication configured!");
}
/**
* Add a default header.
*
* @param name The header's name
* @param value The header's value
* @return ApiClient this client
*/
public ApiClient addDefaultHeader(String name, String value) {
if ("Authorization".equals(name)) {
this.cookie = null;
}
defaultHeaders.add(name, value);
if (!"Authorization".equals(name)) {
log.info("Default header {}:{} added to the api client", name, value);
}
return this;
}
/**
* Enable or disable debugging
* @param debugging debug value true or false
*/
public void setDebugging(boolean debugging) {
List currentInterceptors = this.restTemplate.getInterceptors();
if(debugging) {
if (currentInterceptors == null) {
currentInterceptors = new ArrayList();
}
ClientHttpRequestInterceptor interceptor = new ApiClientHttpRequestInterceptor();
currentInterceptors.add(interceptor);
this.restTemplate.setInterceptors(currentInterceptors);
}
else {
if (currentInterceptors != null && !currentInterceptors.isEmpty()) {
Iterator iter = currentInterceptors.iterator();
while (iter.hasNext()) {
ClientHttpRequestInterceptor interceptor = iter.next();
if (interceptor instanceof ApiClientHttpRequestInterceptor) {
iter.remove();
}
}
this.restTemplate.setInterceptors(currentInterceptors);
}
}
this.debugging = debugging;
}
/**
* Check that whether debugging is enabled for this API client.
* @return boolean true if this client is enabled for debugging, false otherwise
*/
public boolean isDebugging() {
return debugging;
}
/**
* Get the date format used to parse/format date parameters.
* @return DateFormat format
*/
public DateFormat getDateFormat() {
return dateFormat;
}
/**
* Set the date format used to parse/format date parameters.
* @param dateFormat Date format
* @return ApiClient
*/
public ApiClient setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
return this;
}
/**
* Parse the given string into Date object.
* @param str date to parse
* @return Date parsed date
*/
public Date parseDate(String str) {
try {
return dateFormat.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* Format the given Date object into string.
* @param date date to format
* @return String formatted date
*/
public String formatDate(Date date) {
return dateFormat.format(date);
}
/**
* Format the given OffsetDateTime object into string.
* @param OffsetDateTime dateTime to format
* @return String formatted dateTime
*/
public String formatOffsetDateTime(OffsetDateTime dateTime) {
DateTimeFormatter ISO_8601_FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.withZone(ZoneId.of("UTC"));
if(dateTime.getOffset().equals(ZoneOffset.UTC)) {
return ISO_8601_FORMATTER.format(dateTime);
}
return dateTime.toString();
}
/**
* Format the given parameter object into string.
* @param param the object to convert
* @return String the parameter represented as a String
*/
public String parameterToString(Object param) {
if (param == null) {
return "";
}
else if (param instanceof OffsetDateTime) {
return formatOffsetDateTime( (OffsetDateTime) param);
}
else if (param instanceof Date) {
return formatDate( (Date) param);
}
else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for(Object o : (Collection>) param) {
if(b.length() > 0) {
b.append(",");
}
b.append(String.valueOf(o));
}
return b.toString();
}
else {
return String.valueOf(param);
}
}
/**
* Converts a parameter to a {@link MultiValueMap} for use in REST requests
* @param collectionFormat The format to convert to
* @param name The name of the parameter
* @param value The parameter's value
* @return a Map containing the String value(s) of the input parameter
*/
public MultiValueMap parameterToMultiValueMap(CollectionFormat collectionFormat, String name, Object value) {
final MultiValueMap params = new LinkedMultiValueMap();
if (name == null || name.isEmpty() || value == null) {
return params;
}
if(collectionFormat == null) {
collectionFormat = CollectionFormat.CSV;
}
Collection> valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection>) value;
}
else {
params.add(name, parameterToString(value));
return params;
}
if (valueCollection.isEmpty()){
return params;
}
if (collectionFormat.equals(CollectionFormat.MULTI)) {
for (Object item : valueCollection) {
params.add(name, parameterToString(item));
}
return params;
}
List values = new ArrayList();
for(Object o : valueCollection) {
values.add(parameterToString(o));
}
params.add(name, collectionFormat.collectionToString(values));
return params;
}
/*
* Check if the given {@code String} is a JSON MIME.
* @param mediaType the input MediaType
* @return boolean true if the MediaType represents JSON, false otherwise
*/
public boolean isJsonMime(String mediaType) {
// "* / *" is default to JSON
if ("*/*".equals(mediaType)) {
return true;
}
try {
return isJsonMime(MediaType.parseMediaType(mediaType));
} catch (InvalidMediaTypeException e) {
}
return false;
}
/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* @param mediaType the input MediaType
* @return boolean true if the MediaType represents JSON, false otherwise
*/
public boolean isJsonMime(MediaType mediaType) {
return mediaType != null && (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || mediaType.getSubtype().matches("^.*\\+json[;]?\\s*$"));
}
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
* otherwise use all of them (joining into a string)
*
* @param accepts The accepts array to select from
* @return List The list of MediaTypes to use for the Accept header
*/
public List selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) {
return null;
}
return MediaType.parseMediaTypes(StringUtils.join(accepts, ","));
}
/**
* Select the Content-Type header's value from the given array:
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used.
*/
public MediaType selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
return MediaType.APPLICATION_JSON;
}
for (String contentType : contentTypes) {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (isJsonMime(mediaType)) {
return mediaType;
}
}
return MediaType.parseMediaType(contentTypes[0]);
}
/**
* Select the body to use for the request
* @param obj the body object
* @param formParams the form parameters
* @param contentType the content type of the request
* @return Object the selected body
*/
protected Object selectBody(Object obj, MultiValueMap formParams, MediaType contentType) {
boolean isForm = MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType) || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType);
return isForm ? formParams : obj;
}
/**
* Invoke API by sending HTTP request with the given options.
*
* @param the return type to use
* @param path The sub-path of the HTTP URL
* @param method The request method
* @param queryParams The query parameters
* @param body The request body object
* @param headerParams The header parameters
* @param formParams The form parameters
* @param accept The request's Accept header
* @param contentType The request's Content-Type header
* @param authNames The authentications to apply
* @param returnType The return type into which to deserialize the response
* @return The response body in chosen type
* @throws org.springframework.web.client.RestClientException
*/
public T invokeAPI(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException {
try {
return invokeAPIInternal(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames, returnType);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
log.error("Server responded with 401 status due to {message}.", e.getMessage());
this.cookie = null;
refreshCookie = true;
log.debug("Retrying request with basic header as server responded with 401 status due to cookie expiration.");
return invokeAPIInternal(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames, returnType);
} else {
log.error("Unable to make a successful request", e);
throw e;
}
} catch (Exception e) {
log.error("Unable to make a successful request", e);
throw e;
}
}
private T invokeAPIInternal(String path, HttpMethod method, MultiValueMap queryParams, Object body, HttpHeaders headerParams, MultiValueMap formParams, List accept, MediaType contentType, String[] authNames, ParameterizedTypeReference returnType) throws RestClientException {
final UriComponentsBuilder builder = UriComponentsBuilder.newInstance().scheme(scheme).host(host).port(port).path(path);
if (queryParams != null) {
builder.queryParams(queryParams);
}
final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
if(accept != null && !headerParams.containsKey(HttpHeaders.ACCEPT)) {
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
}
if(contentType != null && !headerParams.containsKey(HttpHeaders.CONTENT_TYPE)) {
requestBuilder.contentType(contentType);
}
if(defaultHeaders.getOrDefault("NTNX-Request-Id", null) == null
&& headerParams.getOrDefault("NTNX-Request-Id", null) == null) {
UUID generatedID = UUID.randomUUID();
headerParams.add("NTNX-Request-Id", String.valueOf(generatedID));
}
if(!headerParams.containsKey(HttpHeaders.IF_MATCH)) {
addEtagReferenceToHeader(body, requestBuilder);
}
headerParams.putAll(defaultHeaders);
if(cookie != null) {
requestBuilder.header(HttpHeaders.COOKIE, cookie);
if (headerParams.containsKey(HttpHeaders.AUTHORIZATION)) {
headerParams.remove(HttpHeaders.AUTHORIZATION);
}
}
else {
updateParamsForAuth(authNames, queryParams, headerParams);
}
addHeadersToRequest(headerParams, requestBuilder);
RequestEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy