Search in sources :

Example 31 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project spring-security by spring-projects.

the class UnmodifiableSetDeserializer method deserialize.

@Override
public Set deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode node = mapper.readTree(jp);
    Set<Object> resultSet = new HashSet<Object>();
    if (node != null) {
        if (node instanceof ArrayNode) {
            ArrayNode arrayNode = (ArrayNode) node;
            Iterator<JsonNode> nodeIterator = arrayNode.iterator();
            while (nodeIterator.hasNext()) {
                JsonNode elementNode = nodeIterator.next();
                resultSet.add(mapper.readValue(elementNode.toString(), Object.class));
            }
        } else {
            resultSet.add(mapper.readValue(node.toString(), Object.class));
        }
    }
    return Collections.unmodifiableSet(resultSet);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 32 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project spring-security by spring-projects.

the class UsernamePasswordAuthenticationTokenDeserializer method deserialize.

/**
	 * This method construct {@link UsernamePasswordAuthenticationToken} object from serialized json.
	 * @param jp the JsonParser
	 * @param ctxt the DeserializationContext
	 * @return the user
	 * @throws IOException if a exception during IO occurs
	 * @throws JsonProcessingException if an error during JSON processing occurs
	 */
@Override
public UsernamePasswordAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    UsernamePasswordAuthenticationToken token = null;
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode jsonNode = mapper.readTree(jp);
    Boolean authenticated = readJsonNode(jsonNode, "authenticated").asBoolean();
    JsonNode principalNode = readJsonNode(jsonNode, "principal");
    Object principal = null;
    if (principalNode.isObject()) {
        principal = mapper.readValue(principalNode.toString(), new TypeReference<User>() {
        });
    } else {
        principal = principalNode.asText();
    }
    Object credentials = readJsonNode(jsonNode, "credentials").asText();
    List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").toString(), new TypeReference<List<GrantedAuthority>>() {
    });
    if (authenticated) {
        token = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
    } else {
        token = new UsernamePasswordAuthenticationToken(principal, credentials);
    }
    token.setDetails(readJsonNode(jsonNode, "details"));
    return token;
}
Also used : GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) JsonNode(com.fasterxml.jackson.databind.JsonNode) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 33 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project spring-security by spring-projects.

the class AbstractMixinTests method setup.

@Before
public void setup() {
    mapper = new ObjectMapper();
    ClassLoader loader = getClass().getClassLoader();
    mapper.registerModules(SecurityJackson2Modules.getModules(loader));
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 34 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project keywhiz by square.

the class Printing method printSanitizedSecretWithDetails.

public void printSanitizedSecretWithDetails(SanitizedSecret secret) {
    System.out.println(SanitizedSecret.displayName(secret));
    SecretDetailResponse secretDetails;
    try {
        secretDetails = keywhizClient.secretDetailsForId(secret.id());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    System.out.println("\tGroups:");
    secretDetails.groups.stream().sorted(Comparator.comparing(Group::getName)).forEach(g -> System.out.println(INDENT + g.getName()));
    System.out.println("\tClients:");
    secretDetails.clients.stream().sorted(Comparator.comparing(Client::getName)).forEach(c -> System.out.println(INDENT + c.getName()));
    System.out.println("\tMetadata:");
    if (!secret.metadata().isEmpty()) {
        String metadata;
        try {
            metadata = new ObjectMapper().writeValueAsString(secret.metadata());
        } catch (JsonProcessingException e) {
            throw Throwables.propagate(e);
        }
        System.out.println(INDENT + metadata);
    }
    if (secret.expiry() > 0) {
        System.out.println("\tExpiry:");
        Date d = new Date(secret.expiry() * 1000);
        System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
    }
    if (!secret.description().isEmpty()) {
        System.out.println("\tDescription:");
        System.out.println(INDENT + secret.description());
    }
    if (!secret.createdBy().isEmpty()) {
        System.out.println("\tCreated by:");
        System.out.println(INDENT + secret.createdBy());
    }
    System.out.println("\tCreated at:");
    Date d = new Date(secret.createdAt().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
    if (!secret.updatedBy().isEmpty()) {
        System.out.println("\tUpdated by:");
        System.out.println(INDENT + secret.updatedBy());
    }
    System.out.println("\tUpdated at:");
    d = new Date(secret.updatedAt().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
}
Also used : Group(keywhiz.api.model.Group) SecretDetailResponse(keywhiz.api.SecretDetailResponse) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date)

Example 35 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project keywhiz by square.

the class Printing method printGroupWithDetails.

public void printGroupWithDetails(Group group) {
    System.out.println(group.getName());
    GroupDetailResponse groupDetails;
    try {
        groupDetails = keywhizClient.groupDetailsForId(group.getId());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    System.out.println("\tClients:");
    groupDetails.getClients().stream().sorted(Comparator.comparing(Client::getName)).forEach(c -> System.out.println(INDENT + c.getName()));
    System.out.println("\tSecrets:");
    groupDetails.getSecrets().stream().sorted(Comparator.comparing(SanitizedSecret::name)).forEach(s -> System.out.println(INDENT + SanitizedSecret.displayName(s)));
    System.out.println("\tMetadata:");
    if (!groupDetails.getMetadata().isEmpty()) {
        String metadata;
        try {
            metadata = new ObjectMapper().writeValueAsString(groupDetails.getMetadata());
        } catch (JsonProcessingException e) {
            throw Throwables.propagate(e);
        }
        System.out.println(INDENT + metadata);
    }
    if (!groupDetails.getDescription().isEmpty()) {
        System.out.println("\tDescription:");
        System.out.println(INDENT + groupDetails.getDescription());
    }
    if (!groupDetails.getCreatedBy().isEmpty()) {
        System.out.println("\tCreated by:");
        System.out.println(INDENT + groupDetails.getCreatedBy());
    }
    System.out.println("\tCreated at:");
    Date d = new Date(groupDetails.getCreationDate().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
    if (!groupDetails.getUpdatedBy().isEmpty()) {
        System.out.println("\tUpdated by:");
        System.out.println(INDENT + groupDetails.getUpdatedBy());
    }
    System.out.println("\tUpdated at:");
    d = new Date(groupDetails.getUpdateDate().toEpochSecond() * 1000);
    System.out.println(INDENT + DateFormat.getDateTimeInstance().format(d));
}
Also used : SanitizedSecret(keywhiz.api.model.SanitizedSecret) GroupDetailResponse(keywhiz.api.GroupDetailResponse) IOException(java.io.IOException) Client(keywhiz.api.model.Client) KeywhizClient(keywhiz.client.KeywhizClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1706 Test (org.junit.Test)641 JsonNode (com.fasterxml.jackson.databind.JsonNode)270 IOException (java.io.IOException)238 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)183 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)124 HashMap (java.util.HashMap)119 ArrayList (java.util.ArrayList)81 File (java.io.File)72 InputStream (java.io.InputStream)65 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)55 JCodeModel (com.sun.codemodel.JCodeModel)52 List (java.util.List)49 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)47 JPackage (com.sun.codemodel.JPackage)44 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)41 JsonException (jmri.server.json.JsonException)41 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)37