Package com.styra.opa.openapi.utils
Class Utils
- java.lang.Object
-
- com.styra.opa.openapi.utils.Utils
-
public final class Utils extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Utils.Function<S,T>
static class
Utils.HttpResponseCached
static class
Utils.JsonShape
static class
Utils.TypeReferenceWithShape
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
allowIntrospection(java.lang.Class<?> cls)
static <T> T
asType(EventStreamMessage x, com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
static java.lang.String
bytesToLowerCaseHex(byte[] bytes)
static Utils.HttpResponseCached
cache(java.net.http.HttpResponse<java.io.InputStream> response)
Fully reads the body of the given response and caches it in memory.static void
checkArgument(boolean expression, java.lang.String message)
static <T> T
checkNotNull(T object, java.lang.String name)
static HTTPRequest
configureSecurity(HTTPRequest request, java.lang.Object security)
static boolean
contentTypeMatches(java.lang.String contentType, java.lang.String pattern)
static java.lang.Object
convertToShape(java.lang.Object o, Utils.JsonShape shape, com.fasterxml.jackson.core.type.TypeReference<?> typeReference)
static com.fasterxml.jackson.databind.JavaType
convertToStringShape(com.fasterxml.jackson.databind.type.TypeFactory f, com.fasterxml.jackson.databind.JavaType a)
static java.net.http.HttpRequest.Builder
copy(java.net.http.HttpRequest request)
Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.static java.net.http.HttpRequest.Builder
copy(java.net.http.HttpRequest request, java.util.function.BiPredicate<java.lang.String,java.lang.String> filter)
Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.static java.lang.String
discriminatorToString(java.lang.Object o)
static <K,V>
java.util.Map<K,V>emptyMapIfNull(java.util.Map<K,V> map)
static byte[]
extractByteArrayFromBody(java.net.http.HttpResponse<java.io.InputStream> response)
static <T> java.lang.String
generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, java.util.Optional<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static <T> java.lang.String
generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static <T> java.lang.String
generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, T params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static java.lang.String
generateURL(java.lang.String baseURL, java.lang.String path)
static java.util.Map<java.lang.String,java.util.List<java.lang.String>>
getHeadersFromMetadata(java.lang.Object headers, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static <T> java.util.List<org.apache.http.NameValuePair>
getQueryParams(java.lang.Class<T> type, java.util.Optional<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static <T> java.util.List<org.apache.http.NameValuePair>
getQueryParams(java.lang.Class<T> type, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static <T> java.util.List<org.apache.http.NameValuePair>
getQueryParams(java.lang.Class<T> type, T params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static boolean
isPresentAndNotNull(java.util.Optional<?> x)
static boolean
isPresentAndNotNull(org.openapitools.jackson.nullable.JsonNullable<?> x)
static java.lang.String
json(EventStreamMessage m, com.fasterxml.jackson.databind.ObjectMapper mapper, boolean dataIsPlainText)
static com.fasterxml.jackson.databind.ObjectMapper
mapper()
static java.lang.Object
populateGlobal(java.lang.Object value, java.lang.String fieldName, java.lang.String paramType, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
static java.lang.String
prefixBearer(java.lang.String authHeaderValue)
static <T> T
readDefaultOrConstValue(java.lang.String name, java.lang.String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
static void
recordTest(java.lang.String id)
static boolean
referenceEquals(java.lang.Object a, java.lang.Object b)
static java.lang.Object
resolveOptionals(java.lang.Object o)
static SerializedBody
serializeRequestBody(java.lang.Object request, java.lang.String requestField, java.lang.String serializationMethod, boolean nullable)
static java.lang.String
sortQueryParameters(java.lang.String url)
Returns an equivalent url with query parameters sorted by name.static java.lang.Object
sortSerializedMaps(java.lang.Object input, java.lang.String regex, java.lang.String delim)
static boolean
statusCodeMatches(int statusCode, java.lang.String... expectedStatusCodes)
static boolean
statusCodeMatchesOne(int statusCode, java.lang.String expectedStatusCode)
static <T> java.util.stream.Stream<T>
stream(java.util.concurrent.Callable<java.util.Optional<T>> first, Utils.Function<T,java.util.Optional<T>> next)
static java.lang.String
templateUrl(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> params)
static byte[]
toByteArrayAndClose(java.io.InputStream in)
static java.util.List<?>
toList(java.lang.Object o)
static java.lang.String
toString(java.lang.Class<?> cls, java.lang.Object... items)
static java.lang.String
toUtf8AndClose(java.io.InputStream in)
static java.lang.String
urlEncode(java.lang.String s)
static java.lang.String
valToString(java.lang.Object value)
-
-
-
Method Detail
-
referenceEquals
public static boolean referenceEquals(java.lang.Object a, java.lang.Object b)
-
generateURL
public static java.lang.String generateURL(java.lang.String baseURL, java.lang.String path) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
- Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
generateURL
public static <T> java.lang.String generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws com.fasterxml.jackson.core.JsonProcessingException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
generateURL
public static <T> java.lang.String generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, java.util.Optional<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws com.fasterxml.jackson.core.JsonProcessingException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
generateURL
public static <T> java.lang.String generateURL(java.lang.Class<T> type, java.lang.String baseURL, java.lang.String path, T params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException, com.fasterxml.jackson.core.JsonProcessingException
- Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
com.fasterxml.jackson.core.JsonProcessingException
-
contentTypeMatches
public static boolean contentTypeMatches(java.lang.String contentType, java.lang.String pattern)
-
allowIntrospection
public static boolean allowIntrospection(java.lang.Class<?> cls)
-
serializeRequestBody
public static SerializedBody serializeRequestBody(java.lang.Object request, java.lang.String requestField, java.lang.String serializationMethod, boolean nullable) throws java.lang.NoSuchFieldException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.UnsupportedOperationException, java.io.IOException
- Throws:
java.lang.NoSuchFieldException
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
java.lang.UnsupportedOperationException
java.io.IOException
-
getQueryParams
public static <T> java.util.List<org.apache.http.NameValuePair> getQueryParams(java.lang.Class<T> type, java.util.Optional<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getQueryParams
public static <T> java.util.List<org.apache.http.NameValuePair> getQueryParams(java.lang.Class<T> type, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws java.lang.Exception
- Throws:
java.lang.Exception
-
getQueryParams
public static <T> java.util.List<org.apache.http.NameValuePair> getQueryParams(java.lang.Class<T> type, T params, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws java.lang.Exception
- Throws:
java.lang.Exception
-
configureSecurity
public static HTTPRequest configureSecurity(HTTPRequest request, java.lang.Object security) throws java.lang.Exception
- Throws:
java.lang.Exception
-
templateUrl
public static java.lang.String templateUrl(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> params)
-
getHeadersFromMetadata
public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeadersFromMetadata(java.lang.Object headers, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals) throws java.lang.Exception
- Throws:
java.lang.Exception
-
valToString
public static java.lang.String valToString(java.lang.Object value)
-
prefixBearer
public static java.lang.String prefixBearer(java.lang.String authHeaderValue)
-
populateGlobal
public static java.lang.Object populateGlobal(java.lang.Object value, java.lang.String fieldName, java.lang.String paramType, java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.Object>>> globals)
-
checkNotNull
public static <T> T checkNotNull(T object, java.lang.String name)
-
checkArgument
public static void checkArgument(boolean expression, java.lang.String message)
-
emptyMapIfNull
public static <K,V> java.util.Map<K,V> emptyMapIfNull(java.util.Map<K,V> map)
-
toString
public static java.lang.String toString(java.lang.Class<?> cls, java.lang.Object... items)
-
resolveOptionals
public static java.lang.Object resolveOptionals(java.lang.Object o)
-
toList
public static java.util.List<?> toList(java.lang.Object o)
-
readDefaultOrConstValue
public static <T> T readDefaultOrConstValue(java.lang.String name, java.lang.String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
-
extractByteArrayFromBody
public static byte[] extractByteArrayFromBody(java.net.http.HttpResponse<java.io.InputStream> response) throws java.io.IOException
- Throws:
java.io.IOException
-
toByteArrayAndClose
public static byte[] toByteArrayAndClose(java.io.InputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
toUtf8AndClose
public static java.lang.String toUtf8AndClose(java.io.InputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
convertToShape
public static java.lang.Object convertToShape(java.lang.Object o, Utils.JsonShape shape, com.fasterxml.jackson.core.type.TypeReference<?> typeReference)
-
convertToStringShape
public static com.fasterxml.jackson.databind.JavaType convertToStringShape(com.fasterxml.jackson.databind.type.TypeFactory f, com.fasterxml.jackson.databind.JavaType a)
-
stream
public static <T> java.util.stream.Stream<T> stream(java.util.concurrent.Callable<java.util.Optional<T>> first, Utils.Function<T,java.util.Optional<T>> next)
-
statusCodeMatches
public static boolean statusCodeMatches(int statusCode, java.lang.String... expectedStatusCodes)
-
statusCodeMatchesOne
public static boolean statusCodeMatchesOne(int statusCode, java.lang.String expectedStatusCode)
-
copy
public static java.net.http.HttpRequest.Builder copy(java.net.http.HttpRequest request)
Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.- Parameters:
request
- request to copy- Returns:
- a builder initialized with values from
request
-
copy
public static java.net.http.HttpRequest.Builder copy(java.net.http.HttpRequest request, java.util.function.BiPredicate<java.lang.String,java.lang.String> filter)
Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.- Parameters:
request
- request to copyfilter
- selects which header key-values to include in the copied request- Returns:
- a builder initialized with values from
request
-
mapper
public static com.fasterxml.jackson.databind.ObjectMapper mapper()
-
asType
public static <T> T asType(EventStreamMessage x, com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
-
json
public static java.lang.String json(EventStreamMessage m, com.fasterxml.jackson.databind.ObjectMapper mapper, boolean dataIsPlainText) throws com.fasterxml.jackson.core.JsonProcessingException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
cache
public static Utils.HttpResponseCached cache(java.net.http.HttpResponse<java.io.InputStream> response) throws java.io.IOException
Fully reads the body of the given response and caches it in memory. The returned response has utility methods to view the body (bodyAsUtf8(), bodyAsBytes()
and thebody()
method can be called multiple times, each returning a freshInputStream
that will read from the cached byte array.This method is most likely to be used in a diagnostic/logging situtation so that the contents of a response can be viewed without affecting processing. Using this method with a very large body may be problematic in terms of memory use.
- Parameters:
response
- response to cache- Returns:
- response with a cached body
- Throws:
java.io.IOException
-
bytesToLowerCaseHex
public static java.lang.String bytesToLowerCaseHex(byte[] bytes)
-
discriminatorToString
public static java.lang.String discriminatorToString(java.lang.Object o)
-
recordTest
public static void recordTest(java.lang.String id)
-
sortQueryParameters
public static java.lang.String sortQueryParameters(java.lang.String url)
Returns an equivalent url with query parameters sorted by name. Sort is stable in that parameters with the same name will not be reordered.- Parameters:
url
- input- Returns:
- url with query parameters sorted by name
-
sortSerializedMaps
public static java.lang.Object sortSerializedMaps(java.lang.Object input, java.lang.String regex, java.lang.String delim)
-
isPresentAndNotNull
public static boolean isPresentAndNotNull(java.util.Optional<?> x)
-
isPresentAndNotNull
public static boolean isPresentAndNotNull(org.openapitools.jackson.nullable.JsonNullable<?> x)
-
urlEncode
public static java.lang.String urlEncode(java.lang.String s)
-
-