Package com.styra.opa.openapi.utils
Class Utils
java.lang.Object
com.styra.opa.openapi.utils.Utils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
static final class
static enum
static final class
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
allowIntrospection
(Class<?> cls) static <T> T
asType
(EventStreamMessage x, com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) static Utils.HttpResponseCached
cache
(HttpResponse<InputStream> response) Fully reads the body of the given response and caches it in memory.static void
checkArgument
(boolean expression, String message) static <T> T
checkNotNull
(T object, String name) static HTTPRequest
configureSecurity
(HTTPRequest request, Object security) static boolean
contentTypeMatches
(String contentType, String pattern) static Object
convertToShape
(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 HttpRequest.Builder
copy
(HttpRequest request) Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.static HttpRequest.Builder
copy
(HttpRequest request, BiPredicate<String, String> filter) Returns anHttpRequest.Builder
which is initialized with the state of the givenHttpRequest
.static HTTPClient
createTestHTTPClient
(SpeakeasyHTTPClient client, String testName) static HTTPClient
createTestHTTPClient
(String testName) static String
static <K,
V> Map<K, V> emptyMapIfNull
(Map<K, V> map) static String
environmentVariable
(String name, String defaultValue) Internal use.static byte[]
extractByteArrayFromBody
(HttpResponse<InputStream> response) static <T> String
generateURL
(Class<T> type, String baseURL, String path, Optional<? extends T> params, Map<String, Map<String, Map<String, Object>>> globals) static <T> String
generateURL
(Class<T> type, String baseURL, String path, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Map<String, Map<String, Map<String, Object>>> globals) static <T> String
generateURL
(Class<T> type, String baseURL, String path, T params, Map<String, Map<String, Map<String, Object>>> globals) static String
generateURL
(String baseURL, String path) static <T> List<QueryParameter>
getQueryParams
(Class<T> type, Optional<? extends T> params, Map<String, Map<String, Map<String, Object>>> globals) static <T> List<QueryParameter>
getQueryParams
(Class<T> type, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Map<String, Map<String, Map<String, Object>>> globals) static <T> List<QueryParameter>
static boolean
isPresentAndNotNull
(Optional<?> x) static boolean
isPresentAndNotNull
(org.openapitools.jackson.nullable.JsonNullable<?> x) static String
json
(EventStreamMessage m, com.fasterxml.jackson.databind.ObjectMapper mapper, boolean dataIsPlainText) static com.fasterxml.jackson.databind.ObjectMapper
mapper()
static Object
populateGlobal
(Object value, String fieldName, String paramType, Map<String, Map<String, Map<String, Object>>> globals) static String
prefixBearer
(String authHeaderValue) static byte[]
static byte[]
readBytes
(InputStream in) static byte[]
static byte[]
static <T> T
readDefaultOrConstValue
(String name, String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) static void
recordTest
(String id) static boolean
referenceEquals
(Object a, Object b) static Object
static SerializedBody
serializeRequestBody
(Object request, String requestField, String serializationMethod, boolean nullable) static String
sessionKey
(String... items) static void
setSseSentinel
(Object o, String value) static String
sortJSONObjectKeys
(String json, String... fields) static String
Returns an equivalent url with query parameters sorted by name.static Object
sortSerializedMaps
(Object input, String regex, String delim) static boolean
statusCodeMatches
(int statusCode, String... expectedStatusCodes) static boolean
statusCodeMatchesOne
(int statusCode, String expectedStatusCode) static <T> Stream<T>
stream
(Callable<Optional<T>> first, Utils.Function<T, Optional<T>> next) static String
templateUrl
(String url, Map<String, String> params) static byte[]
static String
toHex
(byte[] bytes) static List<?>
static <T> Optional<T>
toOptional
(org.openapitools.jackson.nullable.JsonNullable<T> a) static String
static String
static String
valToString
(Object value) static <T> T
valueOrElse
(Optional<T> value, T valueIfNotPresent) static <T> T
valueOrElse
(org.openapitools.jackson.nullable.JsonNullable<T> value, T valueIfNotPresent) static <T> T
valueOrElse
(T value, T valueIfNotPresent) static <T> T
valueOrNull
(Optional<T> value) static <T> T
valueOrNull
(org.openapitools.jackson.nullable.JsonNullable<T> value) static <T> T
valueOrNull
(T value)
-
Method Details
-
referenceEquals
-
generateURL
public static String generateURL(String baseURL, String path) throws IllegalArgumentException, IllegalAccessException -
generateURL
public static <T> String generateURL(Class<T> type, String baseURL, String path, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Map<String, Map<String, throws com.fasterxml.jackson.core.JsonProcessingException, IllegalArgumentException, IllegalAccessExceptionMap<String, Object>>> globals) - Throws:
com.fasterxml.jackson.core.JsonProcessingException
IllegalArgumentException
IllegalAccessException
-
generateURL
public static <T> String generateURL(Class<T> type, String baseURL, String path, Optional<? extends T> params, Map<String, Map<String, throws com.fasterxml.jackson.core.JsonProcessingException, IllegalArgumentException, IllegalAccessExceptionMap<String, Object>>> globals) - Throws:
com.fasterxml.jackson.core.JsonProcessingException
IllegalArgumentException
IllegalAccessException
-
generateURL
public static <T> String generateURL(Class<T> type, String baseURL, String path, T params, Map<String, Map<String, throws IllegalArgumentException, IllegalAccessException, com.fasterxml.jackson.core.JsonProcessingExceptionMap<String, Object>>> globals) - Throws:
IllegalArgumentException
IllegalAccessException
com.fasterxml.jackson.core.JsonProcessingException
-
contentTypeMatches
-
allowIntrospection
-
serializeRequestBody
public static SerializedBody serializeRequestBody(Object request, String requestField, String serializationMethod, boolean nullable) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException, UnsupportedOperationException, IOException -
getQueryParams
public static <T> List<QueryParameter> getQueryParams(Class<T> type, Optional<? extends T> params, Map<String, Map<String, throws ExceptionMap<String, Object>>> globals) - Throws:
Exception
-
getQueryParams
public static <T> List<QueryParameter> getQueryParams(Class<T> type, org.openapitools.jackson.nullable.JsonNullable<? extends T> params, Map<String, Map<String, throws ExceptionMap<String, Object>>> globals) - Throws:
Exception
-
getQueryParams
public static <T> List<QueryParameter> getQueryParams(Class<T> type, T params, Map<String, Map<String, throws ExceptionMap<String, Object>>> globals) - Throws:
Exception
-
configureSecurity
- Throws:
Exception
-
templateUrl
-
getHeadersFromMetadata
public static Map<String,List<String>> getHeadersFromMetadata(Object headers, Map<String, Map<String, throws ExceptionMap<String, Object>>> globals) - Throws:
Exception
-
valToString
-
prefixBearer
-
populateGlobal
-
checkNotNull
-
checkArgument
-
emptyMapIfNull
-
toString
-
resolveOptionals
-
toList
-
readDefaultOrConstValue
-
extractByteArrayFromBody
public static byte[] extractByteArrayFromBody(HttpResponse<InputStream> response) throws IOException - Throws:
IOException
-
toByteArrayAndClose
- Throws:
IOException
-
toUtf8AndClose
- Throws:
IOException
-
convertToShape
public static Object convertToShape(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
-
statusCodeMatches
-
statusCodeMatchesOne
-
copy
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
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 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
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:
IOException
-
readBytes
-
readBytes
-
readBytesAndClose
-
readBytes
-
toHex
-
discriminatorToString
-
recordTest
-
sortQueryParameters
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
-
isPresentAndNotNull
-
isPresentAndNotNull
public static boolean isPresentAndNotNull(org.openapitools.jackson.nullable.JsonNullable<?> x) -
setSseSentinel
-
sessionKey
-
createTestHTTPClient
-
createTestHTTPClient
-
environmentVariable
Internal use. Returns the system property withkey = "env." + name
and if doesn't exist returns the value of the environment variable with the given name of if it doesn't exist returnsdefaultValue
.- Parameters:
name
- variable namedefaultValue
- default value if system property and environment variable don't exist- Returns:
- system property with name prepended with ".env" or environment variable of given name or default value
-
toOptional
-
sortJSONObjectKeys
-
valueOrElse
public static <T> T valueOrElse(T value, T valueIfNotPresent) -
valueOrElse
-
valueOrElse
public static <T> T valueOrElse(org.openapitools.jackson.nullable.JsonNullable<T> value, T valueIfNotPresent) -
valueOrNull
public static <T> T valueOrNull(T value) -
valueOrNull
-
valueOrNull
public static <T> T valueOrNull(org.openapitools.jackson.nullable.JsonNullable<T> value)
-