Package io.jans.ca.plugin.adminui.utils
Class CommonUtils
- java.lang.Object
-
- io.jans.ca.plugin.adminui.utils.CommonUtils
-
public class CommonUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static DateTimeFormatterLS_DATE_FORMAT
-
Constructor Summary
Constructors Constructor Description CommonUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DateaddMinutes(Date date, int minutesToAdd)Return a new Date representing the given date plus the specified number of minutes.static StringconvertIsoToDateString(String isoDateTime)static StringconvertLocalDateToString(LocalDate date)static LocalDateconvertStringToLocalDate(String dateString)static GenericResponsecreateGenericResponse(boolean result, int responseCode, String responseMessage)static GenericResponsecreateGenericResponse(boolean result, int responseCode, String responseMessage, com.fasterxml.jackson.databind.JsonNode node)Create a GenericResponse populated with the provided status, code, message, and JSON response object.static GenericResponsecreateResponseWithByteArray(boolean result, int responseCode, String responseMessage, byte[] byteArray)Creates a GenericResponse populated with the provided success flag, response code, message, and byte array.static Stringdecode(String toDecode)static Stringencode(String str)static Map<String,Object>getClaims(io.jans.as.model.jwt.Jwt jwtObj)Extracts claims from a Jwt into a Map keyed by claim name.static StringgetFormattedDate()static booleanhasShortCode(String input)static booleanhasShortCode(Map<String,?> map)Checks whether any value in the provided map contains a placeholder of the form `${name}`.static booleanisValidJson(String json)static StringjoinAndUrlEncode(Collection<String> list)static Map<String,Object>jsonStringToMap(String jsonString)static RSAPrivateKeyloadPrivateKey(String privateKeyPEM)static StringmapToJsonString(Map<String,Object> mapObj)static StringreplacePlaceholders(String url, Map<String,Object> placeholderValues)static Map<String,Object>replacePlaceholders(Map<String,Object> map, Map<String,Object> placeholderValues)static com.fasterxml.jackson.databind.JsonNodetoJsonNode(Object object)Converts any Java object into a JsonNode representation.static booleantoPrimitiveOrDefaultFalse(Boolean booleanObject)Convert a Boolean reference to its primitive form, using a default value when the input is null.static StringtoUrlEncodedString(Map<String,String> params)Builds a UTF-8 URL-encoded query string from the provided parameters.
-
-
-
Field Detail
-
LS_DATE_FORMAT
public static final DateTimeFormatter LS_DATE_FORMAT
-
-
Method Detail
-
joinAndUrlEncode
public static String joinAndUrlEncode(Collection<String> list) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
-
encode
public static String encode(String str) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
-
getFormattedDate
public static String getFormattedDate()
-
loadPrivateKey
public static RSAPrivateKey loadPrivateKey(String privateKeyPEM) throws Exception
- Throws:
Exception
-
decode
public static String decode(String toDecode) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
-
createGenericResponse
public static GenericResponse createGenericResponse(boolean result, int responseCode, String responseMessage)
-
createGenericResponse
public static GenericResponse createGenericResponse(boolean result, int responseCode, String responseMessage, com.fasterxml.jackson.databind.JsonNode node)
Create a GenericResponse populated with the provided status, code, message, and JSON response object.- Parameters:
result- whether the operation was successfulresponseCode- numeric response coderesponseMessage- human-readable response messagenode- JSON node to use as the response object, may be null- Returns:
- a GenericResponse instance populated with the given values
-
createResponseWithByteArray
public static GenericResponse createResponseWithByteArray(boolean result, int responseCode, String responseMessage, byte[] byteArray)
Creates a GenericResponse populated with the provided success flag, response code, message, and byte array.- Parameters:
result- whether the response indicates successresponseCode- numeric response coderesponseMessage- human-readable response messagebyteArray- payload bytes to attach to the response- Returns:
- the populated GenericResponse with `responseCode`, `responseMessage`, `success`, and `responseBytes` set
-
hasShortCode
public static boolean hasShortCode(Map<String,?> map)
Checks whether any value in the provided map contains a placeholder of the form `${name}`.- Parameters:
map- the map whose values (converted to strings) will be scanned for placeholders- Returns:
- `true` if at least one value contains a placeholder matching `${name}`, `false` otherwise
-
hasShortCode
public static boolean hasShortCode(String input)
-
replacePlaceholders
public static Map<String,Object> replacePlaceholders(Map<String,Object> map, Map<String,Object> placeholderValues)
-
replacePlaceholders
public static String replacePlaceholders(String url, Map<String,Object> placeholderValues)
-
jsonStringToMap
public static Map<String,Object> jsonStringToMap(String jsonString) throws com.fasterxml.jackson.core.JsonProcessingException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
mapToJsonString
public static String mapToJsonString(Map<String,Object> mapObj) throws com.fasterxml.jackson.core.JsonProcessingException
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
isValidJson
public static boolean isValidJson(String json)
-
toJsonNode
public static com.fasterxml.jackson.databind.JsonNode toJsonNode(Object object) throws com.fasterxml.jackson.core.JsonProcessingException
Converts any Java object into a JsonNode representation.- Parameters:
object- the Java object to convert- Returns:
- JSON representation of the object
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-
toPrimitiveOrDefaultFalse
public static boolean toPrimitiveOrDefaultFalse(Boolean booleanObject)
Convert a Boolean reference to its primitive form, using a default value when the input is null.- Returns:
- `true` if the input is `Boolean.TRUE`, `false` if the input is `Boolean.FALSE` or `null`.
-
addMinutes
public static Date addMinutes(Date date, int minutesToAdd)
Return a new Date representing the given date plus the specified number of minutes.- Parameters:
date- the base date to adjustminutesToAdd- the number of minutes to add; may be negative to subtract minutes- Returns:
- the adjusted Date shifted by
minutesToAddminutes fromdate
-
getClaims
public static Map<String,Object> getClaims(io.jans.as.model.jwt.Jwt jwtObj)
Extracts claims from a Jwt into a Map keyed by claim name.The returned map contains claim values using appropriate Java types: Strings, Integer, Long, Boolean, List
for JSON arrays, or JSONObject for JSON objects. - Parameters:
jwtObj- the Jwt to extract claims from; may be null- Returns:
- a map of claim names to claim values, empty if
jwtObjis null
-
toUrlEncodedString
public static String toUrlEncodedString(Map<String,String> params)
Builds a UTF-8 URL-encoded query string from the provided parameters.- Parameters:
params- a map of parameter names to values; entries with a null value are omitted- Returns:
- a URL-encoded string of `key=value` pairs joined with `&`, encoded using UTF-8
-
-