de.cidaas.jwt.impl.JsonNodeClaim Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cidaas-interceptor-spring-security Show documentation
Show all versions of cidaas-interceptor-spring-security Show documentation
Interceptor for Cidaas Java Spring Clients
package de.cidaas.jwt.impl;
import de.cidaas.jwt.exceptions.JWTDecodeException;
import de.cidaas.jwt.interfaces.Claim;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* The JsonNodeClaim retrieves a claim value from a JsonNode object.
*/
class JsonNodeClaim implements Claim {
private final ObjectReader objectReader;
private final JsonNode data;
private JsonNodeClaim(JsonNode node, ObjectReader objectReader) {
this.data = node;
this.objectReader = objectReader;
}
@Override
public Boolean asBoolean() {
return !data.isBoolean() ? null : data.asBoolean();
}
@Override
public Integer asInt() {
return !data.isNumber() ? null : data.asInt();
}
@Override
public Long asLong() {
return !data.isNumber() ? null : data.asLong();
}
@Override
public Double asDouble() {
return !data.isNumber() ? null : data.asDouble();
}
@Override
public String asString() {
return !data.isTextual() ? null : data.asText();
}
@Override
public Date asDate() {
if (!data.canConvertToLong()) {
return null;
}
long seconds = data.asLong();
return new Date(seconds * 1000);
}
@Override
@SuppressWarnings("unchecked")
public T[] asArray(Class tClazz) throws JWTDecodeException {
if (!data.isArray()) {
return null;
}
T[] arr = (T[]) Array.newInstance(tClazz, data.size());
for (int i = 0; i < data.size(); i++) {
try {
arr[i] = objectReader.treeToValue(data.get(i), tClazz);
} catch (JsonProcessingException e) {
throw new JWTDecodeException("Couldn't map the Claim's array contents to " + tClazz.getSimpleName(), e);
}
}
return arr;
}
@Override
public List asList(Class tClazz) throws JWTDecodeException {
if (!data.isArray()) {
return null;
}
List list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
try {
list.add(objectReader.treeToValue(data.get(i), tClazz));
} catch (JsonProcessingException e) {
throw new JWTDecodeException("Couldn't map the Claim's array contents to " + tClazz.getSimpleName(), e);
}
}
return list;
}
@Override
public Map asMap() throws JWTDecodeException {
if (!data.isObject()) {
return null;
}
try {
TypeReference