Search in sources :

Example 36 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project BIMserver by opensourceBIM.

the class StreamingModel method processDownload.

private void processDownload(Long topicId) throws UserException, ServerException, PublicInterfaceNotFoundException, IfcModelInterfaceException, IOException {
    InputStream inputStream = bimServerClient.getDownloadData(topicId);
    if (inputStream == null) {
        throw new IfcModelInterfaceException("No InputStream to read from for topicId " + topicId);
    }
    InputStream downloadData = new org.bimserver.utils.CountingInputStream(inputStream) {
    };
    try {
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(downloadData);
    // TODO implement
    } catch (Exception e) {
        throw new IfcModelInterfaceException(e);
    } finally {
        if (downloadData != null) {
            downloadData.close();
        }
    }
}
Also used : IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) IOException(java.io.IOException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 37 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project keycloak by keycloak.

the class JsonSerialization method createObjectNode.

/**
 * Creates an {@link ObjectNode} based on the given {@code pojo}, copying all its properties to the resulting {@link ObjectNode}.
 *
 * @param pojo a pojo which properties will be populates into the resulting a {@link ObjectNode}
 * @return a {@link ObjectNode} with all the properties from the given pojo
 * @throws IOException if the resulting a {@link ObjectNode} can not be created
 */
public static ObjectNode createObjectNode(Object pojo) throws IOException {
    if (pojo == null) {
        throw new IllegalArgumentException("Pojo can not be null.");
    }
    ObjectNode objectNode = createObjectNode();
    JsonParser jsonParser = mapper.getFactory().createParser(writeValueAsBytes(pojo));
    JsonNode jsonNode = jsonParser.readValueAsTree();
    if (!jsonNode.isObject()) {
        throw new RuntimeException("JsonNode [" + jsonNode + "] is not a object.");
    }
    objectNode.setAll((ObjectNode) jsonNode);
    return objectNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 38 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project keycloak by keycloak.

the class ImportUtils method importFederatedUsersFromStream.

// Assuming that it's invoked inside transaction
public static void importFederatedUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();
        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("federatedUsers".equals(parser.getText())) {
                parser.nextToken();
                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }
                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }
                importFederatedUsers(session, model, realmName, userReps);
                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
Also used : RealmProvider(org.keycloak.models.RealmProvider) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.fasterxml.jackson.core.JsonParser) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 39 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project keycloak by keycloak.

the class ImportUtils method getRealmsFromStream.

public static Map<String, RealmRepresentation> getRealmsFromStream(ObjectMapper mapper, InputStream is) throws IOException {
    Map<String, RealmRepresentation> result = new HashMap<String, RealmRepresentation>();
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(is);
    try {
        parser.nextToken();
        if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
            // Case with more realms in stream
            parser.nextToken();
            List<RealmRepresentation> realmReps = new ArrayList<RealmRepresentation>();
            while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                RealmRepresentation realmRep = parser.readValueAs(RealmRepresentation.class);
                parser.nextToken();
                // Ensure that master realm is imported first
                if (Config.getAdminRealm().equals(realmRep.getRealm())) {
                    realmReps.add(0, realmRep);
                } else {
                    realmReps.add(realmRep);
                }
            }
            for (RealmRepresentation realmRep : realmReps) {
                result.put(realmRep.getRealm(), realmRep);
            }
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            // Case with single realm in stream
            RealmRepresentation realmRep = parser.readValueAs(RealmRepresentation.class);
            result.put(realmRep.getRealm(), realmRep);
        }
    } finally {
        parser.close();
    }
    return result;
}
Also used : HashMap(java.util.HashMap) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 40 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project keycloak by keycloak.

the class ClaimInformationPointProviderTest method onBeforeClass.

@BeforeClass
public static void onBeforeClass() {
    httpService = Undertow.builder().addHttpListener(8989, "localhost").setHandler(exchange -> {
        if (exchange.isInIoThread()) {
            try {
                if (exchange.getRelativePath().equals("/post-claim-information-provider")) {
                    FormParserFactory parserFactory = FormParserFactory.builder().build();
                    FormDataParser parser = parserFactory.createParser(exchange);
                    FormData formData = parser.parseBlocking();
                    if (!"Bearer tokenString".equals(exchange.getRequestHeaders().getFirst("Authorization")) || !"post".equalsIgnoreCase(exchange.getRequestMethod().toString()) || !"application/x-www-form-urlencoded".equals(exchange.getRequestHeaders().getFirst("Content-Type")) || !exchange.getRequestHeaders().get("header-b").contains("header-b-value1") || !exchange.getRequestHeaders().get("header-b").contains("header-b-value2") || !formData.get("param-a").getFirst().getValue().equals("param-a-value1") || !formData.get("param-a").getLast().getValue().equals("param-a-value2") || !formData.get("param-subject").getFirst().getValue().equals("sub") || !formData.get("param-user-name").getFirst().getValue().equals("username") || !formData.get("param-other-claims").getFirst().getValue().equals("param-other-claims-value1") || !formData.get("param-other-claims").getLast().getValue().equals("param-other-claims-value2")) {
                        exchange.setStatusCode(400);
                        return;
                    }
                    exchange.setStatusCode(200);
                } else if (exchange.getRelativePath().equals("/get-claim-information-provider")) {
                    if (!"Bearer idTokenString".equals(exchange.getRequestHeaders().getFirst("Authorization")) || !"get".equalsIgnoreCase(exchange.getRequestMethod().toString()) || !exchange.getRequestHeaders().get("header-b").contains("header-b-value1") || !exchange.getRequestHeaders().get("header-b").contains("header-b-value2") || !exchange.getQueryParameters().get("param-a").contains("param-a-value1") || !exchange.getQueryParameters().get("param-a").contains("param-a-value2") || !exchange.getQueryParameters().get("param-subject").contains("sub") || !exchange.getQueryParameters().get("param-user-name").contains("username")) {
                        exchange.setStatusCode(400);
                        return;
                    }
                    exchange.setStatusCode(200);
                } else {
                    exchange.setStatusCode(404);
                }
            } finally {
                if (exchange.getStatusCode() == 200) {
                    try {
                        ObjectMapper mapper = JsonSerialization.mapper;
                        JsonParser jsonParser = mapper.getFactory().createParser("{\"a\": \"a-value1\", \"b\": \"b-value1\", \"d\": [\"d-value1\", \"d-value2\"]}");
                        TreeNode treeNode = mapper.readTree(jsonParser);
                        exchange.getResponseSender().send(treeNode.toString());
                    } catch (Exception ignore) {
                        ignore.printStackTrace();
                    }
                }
                exchange.endExchange();
            }
        }
    }).build();
    httpService.start();
}
Also used : FormData(io.undertow.server.handlers.form.FormData) FormParserFactory(io.undertow.server.handlers.form.FormParserFactory) TreeNode(com.fasterxml.jackson.core.TreeNode) FormDataParser(io.undertow.server.handlers.form.FormDataParser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) BeforeClass(org.junit.BeforeClass)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)587 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)258 JacksonWrapperParser (com.abubusoft.kripton.persistence.JacksonWrapperParser)258 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 ArrayList (java.util.ArrayList)171 IOException (java.io.IOException)126 JsonFactory (com.fasterxml.jackson.core.JsonFactory)76 Test (org.junit.Test)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)51 JsonNode (com.fasterxml.jackson.databind.JsonNode)46 HashSet (java.util.HashSet)43 JsonToken (com.fasterxml.jackson.core.JsonToken)41 LinkedHashSet (java.util.LinkedHashSet)38 HashMap (java.util.HashMap)35 LinkedList (java.util.LinkedList)26 JsonParseException (com.fasterxml.jackson.core.JsonParseException)23 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)21 List (java.util.List)21 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)20 InputStream (java.io.InputStream)20