Search in sources :

Example 21 with ArrayNode

use of org.codehaus.jackson.node.ArrayNode in project pinot by linkedin.

the class JsonResponseUtil method buildResponseJSON.

public static ObjectNode buildResponseJSON(List<? extends Object> list) {
    ObjectNode rootNode = MAPPER.getNodeFactory().objectNode();
    ArrayNode resultArrayNode = MAPPER.createArrayNode();
    rootNode.put("Result", "OK");
    for (Object obj : list) {
        JsonNode node = MAPPER.convertValue(obj, JsonNode.class);
        resultArrayNode.add(node);
    }
    rootNode.put("Records", resultArrayNode);
    return rootNode;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) JsonNode(org.codehaus.jackson.JsonNode) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 22 with ArrayNode

use of org.codehaus.jackson.node.ArrayNode in project neo4j by neo4j.

the class AbstractRESTInteraction method getValue.

private Object getValue(JsonNode valueNode) {
    Object value;
    if (valueNode instanceof TextNode) {
        value = valueNode.asText();
    } else if (valueNode instanceof ObjectNode) {
        value = mapValue(valueNode.getFieldNames(), valueNode);
    } else if (valueNode instanceof ArrayNode) {
        ArrayNode aNode = (ArrayNode) valueNode;
        ArrayList<String> listValue = new ArrayList<>(aNode.size());
        for (int j = 0; j < aNode.size(); j++) {
            listValue.add(aNode.get(j).asText());
        }
        value = listValue;
    } else if (valueNode instanceof IntNode) {
        value = valueNode.getIntValue();
    } else if (valueNode instanceof LongNode) {
        value = valueNode.getLongValue();
    } else if (valueNode.isNull()) {
        return null;
    } else {
        throw new RuntimeException(String.format("Unhandled REST value type '%s'. Need String (TextNode), List (ArrayNode), Object (ObjectNode), long (LongNode), or int (IntNode).", valueNode.getClass()));
    }
    return value;
}
Also used : IntNode(org.codehaus.jackson.node.IntNode) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) TextNode(org.codehaus.jackson.node.TextNode) ArrayNode(org.codehaus.jackson.node.ArrayNode) Matchers.containsString(org.hamcrest.Matchers.containsString) LongNode(org.codehaus.jackson.node.LongNode)

Example 23 with ArrayNode

use of org.codehaus.jackson.node.ArrayNode in project neo4j by neo4j.

the class EnterpriseAuthenticationIT method shouldHavePredefinedRoles.

@Test
public void shouldHavePredefinedRoles() throws Exception {
    // Given
    startServerWithConfiguredUser();
    // When
    String method = "POST";
    String path = "db/data/transaction/commit";
    HTTP.RawPayload payload = HTTP.RawPayload.quotedJson("{'statements':[{'statement':'CALL dbms.security.listRoles()'}]}");
    HTTP.Response response = HTTP.withHeaders(HttpHeaders.AUTHORIZATION, challengeResponse("neo4j", "secret")).request(method, server.baseUri().resolve(path).toString(), payload);
    // Then
    assertThat(response.status(), equalTo(200));
    ArrayNode errors = (ArrayNode) response.get("errors");
    assertThat("Should have no errors", errors.size(), equalTo(0));
    ArrayNode results = (ArrayNode) response.get("results");
    ArrayNode data = (ArrayNode) results.get(0).get("data");
    assertThat("Should have 4 predefined roles", data.size(), equalTo(4));
    Stream<String> values = data.findValues("row").stream().map(row -> row.get(0).asText());
    assertThat("Expected specific roles", values.collect(Collectors.toList()), hasItems("admin", "architect", "publisher", "reader"));
}
Also used : HTTP(org.neo4j.test.server.HTTP) ArrayNode(org.codehaus.jackson.node.ArrayNode) Test(org.junit.Test)

Example 24 with ArrayNode

use of org.codehaus.jackson.node.ArrayNode in project oxTrust by GluuFederation.

the class SchemaTypeGroupSerializer method serialize.

@Override
public void serialize(Group group, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    log.info(" serialize() ");
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        JsonNode rootNode = mapper.convertValue(group, JsonNode.class);
        Iterator<Map.Entry<String, JsonNode>> iterator = rootNode.getFields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> rootNodeEntry = iterator.next();
            if (rootNodeEntry.getValue() instanceof ObjectNode) {
            // Definitely maybe in the near future
            } else if (rootNodeEntry.getValue() instanceof ArrayNode) {
                AttributeHolder arrayNodeAttributeHolder = new AttributeHolder();
                arrayNodeAttributeHolder.setName(rootNodeEntry.getKey());
                if (rootNodeEntry.getKey().equalsIgnoreCase("members")) {
                    arrayNodeAttributeHolder.setDescription(rootNodeEntry.getKey() + " list; using sub-attributes in a query filter is not supported (cross-querying)");
                    arrayNodeAttributeHolder.setCaseExact(Boolean.TRUE);
                    List<String> referenceTypes = new ArrayList<String>();
                    referenceTypes.add("User");
                    arrayNodeAttributeHolder.setReferenceTypes(referenceTypes);
                } else {
                    arrayNodeAttributeHolder.setDescription(rootNodeEntry.getKey() + " list");
                    arrayNodeAttributeHolder.setCaseExact(Boolean.FALSE);
                }
                arrayNodeAttributeHolder.setRequired(Boolean.FALSE);
                arrayNodeAttributeHolder.setMultiValued(Boolean.TRUE);
                if (rootNodeEntry.getKey().equalsIgnoreCase("schemas")) {
                    arrayNodeAttributeHolder.setUniqueness("server");
                    arrayNodeAttributeHolder.setType("string");
                    arrayNodeAttributeHolder.setCaseExact(Boolean.TRUE);
                    arrayNodeAttributeHolder.setMutability("readOnly");
                    arrayNodeAttributeHolder.setReturned("always");
                } else {
                    arrayNodeAttributeHolder.setType("complex");
                }
                List<AttributeHolder> arrayNodeMapAttributeHolders = new ArrayList<AttributeHolder>();
                Iterator<JsonNode> arrayNodeIterator = rootNodeEntry.getValue().getElements();
                while (arrayNodeIterator.hasNext()) {
                    JsonNode jsonNode = arrayNodeIterator.next();
                    Iterator<Map.Entry<String, JsonNode>> arrayNodeMapIterator = jsonNode.getFields();
                    while (arrayNodeMapIterator.hasNext()) {
                        Map.Entry<String, JsonNode> arrayNodeMapRootNodeEntry = arrayNodeMapIterator.next();
                        AttributeHolder arrayNodeMapAttributeHolder = new AttributeHolder();
                        if (rootNodeEntry.getKey().equalsIgnoreCase("members") && arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("reference")) {
                            arrayNodeMapAttributeHolder.setName("$ref");
                        } else {
                            arrayNodeMapAttributeHolder.setName(arrayNodeMapRootNodeEntry.getKey());
                        }
                        arrayNodeMapAttributeHolder.setType("string");
                        arrayNodeMapAttributeHolder.setDescription(arrayNodeMapRootNodeEntry.getKey());
                        if (arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("value") || arrayNodeMapRootNodeEntry.getKey().equalsIgnoreCase("type")) {
                            arrayNodeMapAttributeHolder.setRequired(Boolean.TRUE);
                        } else {
                            arrayNodeMapAttributeHolder.setRequired(Boolean.FALSE);
                        }
                        arrayNodeMapAttributeHolders.add(arrayNodeMapAttributeHolder);
                    }
                    arrayNodeAttributeHolder.setSubAttributes(arrayNodeMapAttributeHolders);
                    attributeHolders.add(arrayNodeAttributeHolder);
                }
            } else {
                if (!rootNodeEntry.getKey().equalsIgnoreCase("externalId")) {
                    AttributeHolder attributeHolder = new AttributeHolder();
                    attributeHolder.setName(rootNodeEntry.getKey());
                    if (rootNodeEntry.getValue().isBoolean()) {
                        attributeHolder.setType("boolean");
                    } else {
                        attributeHolder.setType("string");
                    }
                    attributeHolder.setDescription(rootNodeEntry.getKey());
                    attributeHolder.setRequired(Boolean.FALSE);
                    if (rootNodeEntry.getKey().equalsIgnoreCase("id")) {
                        attributeHolder.setUniqueness("server");
                        attributeHolder.setCaseExact(Boolean.TRUE);
                        attributeHolder.setMutability("readOnly");
                        attributeHolder.setReturned("always");
                    }
                    if (rootNodeEntry.getKey().equalsIgnoreCase("displayName")) {
                        attributeHolder.setReturned("always");
                    }
                    attributeHolders.add(attributeHolder);
                }
            }
        }
        GroupCoreSchema groupCoreSchema = (GroupCoreSchema) schemaType;
        groupCoreSchema.setAttributeHolders(attributeHolders);
        schemaType = groupCoreSchema;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Unexpected processing error; please check the Group class structure.");
    }
}
Also used : AttributeHolder(org.gluu.oxtrust.model.scim2.schema.AttributeHolder) ObjectNode(org.codehaus.jackson.node.ObjectNode) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) GroupCoreSchema(org.gluu.oxtrust.model.scim2.schema.core.GroupCoreSchema) IOException(java.io.IOException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(org.codehaus.jackson.node.ArrayNode) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 25 with ArrayNode

use of org.codehaus.jackson.node.ArrayNode in project oxTrust by GluuFederation.

the class ExtensionDeserializer method deserialize.

@Override
public Extension deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    log.info(" deserialize() ");
    try {
        if (id == null || id.isEmpty()) {
            throw new IllegalArgumentException("The URN cannot be null or empty");
        }
        JsonNode rootNode = jsonParser.readValueAsTree();
        if (!rootNode.isObject()) {
            throw new IllegalArgumentException("Extension is of wrong JSON type");
        }
        Extension.Builder extensionBuilder = new Extension.Builder(id);
        Iterator<Map.Entry<String, JsonNode>> fieldIterator = rootNode.getFields();
        while (fieldIterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = fieldIterator.next();
            GluuAttribute gluuAttribute = attributeService.getAttributeByName(entry.getKey());
            if (gluuAttribute != null) {
                if (!(gluuAttribute.getOxSCIMCustomAttribute() != null && gluuAttribute.getOxSCIMCustomAttribute().equals(ScimCustomAtribute.TRUE))) {
                    log.info(" NOT A CUSTOM ATTRIBUTE: " + gluuAttribute.getName());
                    throw new IllegalArgumentException("NOT A CUSTOM ATTRIBUTE: " + gluuAttribute.getName());
                }
                GluuAttributeDataType attributeDataType = gluuAttribute.getDataType();
                if ((gluuAttribute.getOxMultivaluedAttribute() != null) && gluuAttribute.getOxMultivaluedAttribute().equals(OxMultivalued.TRUE)) {
                    if (entry.getValue() instanceof ArrayNode) {
                        ArrayNode arrayNode = (ArrayNode) entry.getValue();
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
                        if (attributeDataType.equals(GluuAttributeDataType.STRING) || attributeDataType.equals(GluuAttributeDataType.PHOTO)) {
                            List<String> stringList = Arrays.asList(mapper.readValue(arrayNode, String[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), stringList);
                        } else if (attributeDataType.equals(GluuAttributeDataType.DATE)) {
                            // For validation
                            List<Date> dateList = Arrays.asList(mapper.readValue(arrayNode, Date[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), Arrays.asList(mapper.readValue(arrayNode, String[].class)));
                        } else if (attributeDataType.equals(GluuAttributeDataType.NUMERIC)) {
                            List<BigDecimal> numberList = Arrays.asList(mapper.readValue(arrayNode, BigDecimal[].class));
                            extensionBuilder.setFieldAsList(entry.getKey(), numberList);
                        } else {
                            log.info(" NO MATCH: attributeDataType.getDisplayName() = " + attributeDataType.getDisplayName());
                            throw new IllegalArgumentException("JSON type not supported: " + entry.getValue().toString());
                        }
                    } else {
                        throw new IllegalArgumentException("Attribute \"" + entry.getKey() + "\" is multi-valued but passed value is not of array type.");
                    }
                } else {
                    if (entry.getValue() instanceof ArrayNode) {
                        throw new IllegalArgumentException("Attribute \"" + entry.getKey() + "\" is not multi-valued but passed value is of array type.");
                    } else {
                        if (attributeDataType.equals(GluuAttributeDataType.STRING) || attributeDataType.equals(GluuAttributeDataType.PHOTO)) {
                            handleString(extensionBuilder, entry);
                        } else if (attributeDataType.equals(GluuAttributeDataType.DATE)) {
                            handleDateTime(extensionBuilder, entry);
                        } else if (attributeDataType.equals(GluuAttributeDataType.NUMERIC)) {
                            handleNumber(extensionBuilder, entry);
                        } else {
                            log.info(" NO MATCH: attributeDataType.getDisplayName() = " + attributeDataType.getDisplayName());
                            throw new IllegalArgumentException("JSON type not supported: " + entry.getValue().toString());
                        }
                    }
                }
            } else {
                throw new IllegalArgumentException("NOT FOUND: custom attribute = " + entry.getKey());
            }
        }
        return extensionBuilder.build();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) GluuAttributeDataType(org.xdi.model.GluuAttributeDataType) IOException(java.io.IOException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) IOException(java.io.IOException) GluuAttribute(org.xdi.model.GluuAttribute) Extension(org.gluu.oxtrust.model.scim2.Extension) List(java.util.List) ArrayNode(org.codehaus.jackson.node.ArrayNode) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

ArrayNode (org.codehaus.jackson.node.ArrayNode)44 ObjectNode (org.codehaus.jackson.node.ObjectNode)29 JsonNode (org.codehaus.jackson.JsonNode)17 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)11 GET (javax.ws.rs.GET)10 Test (org.junit.Test)10 Path (javax.ws.rs.Path)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Date (java.util.Date)4 List (java.util.List)4 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 Produces (javax.ws.rs.Produces)3 RegressionTestHelper (org.openmrs.module.htmlformentry.RegressionTestHelper)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2