Search in sources :

Example 61 with JsonParser

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

the class JsonRecordSupport method jsonStreamToRecords.

public static void jsonStreamToRecords(Set<String> indexes, String dbPath, InputStream is, Consumer<JsonRecord> consumer) throws IOException {
    try (JsonParser jp = new JsonFactory().createParser(is)) {
        jsonStreamToRecords(indexes, jp, dbPath, consumer);
        JsonToken jsonToken = jp.nextToken();
        if (jsonToken != null) {
            throw new JsonParseException(jp, "Document did not terminate as expected.");
        }
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 62 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project build-info by JFrogDev.

the class ArtifactoryHttpClient method getVersion.

public ArtifactoryVersion getVersion() throws IOException {
    String versionUrl = artifactoryUrl + VERSION_INFO_URL;
    PreemptiveHttpClient client = getHttpClient();
    HttpGet httpGet = new HttpGet(versionUrl);
    HttpResponse response = client.execute(httpGet);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_NOT_FOUND) {
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null) {
            EntityUtils.consume(httpEntity);
        }
        return ArtifactoryVersion.NOT_FOUND;
    }
    if (statusCode != HttpStatus.SC_OK) {
        if (response.getEntity() != null) {
            EntityUtils.consume(response.getEntity());
        }
        throw new IOException(response.getStatusLine().getReasonPhrase());
    }
    HttpEntity httpEntity = response.getEntity();
    if (httpEntity != null) {
        InputStream content = httpEntity.getContent();
        JsonParser parser;
        try {
            parser = createJsonParser(content);
            EntityUtils.consume(httpEntity);
            JsonNode result = parser.readValueAsTree();
            log.debug("Version result: " + result);
            String version = result.get("version").asText();
            JsonNode addonsNode = result.get("addons");
            boolean hasAddons = (addonsNode != null) && addonsNode.iterator().hasNext();
            return new ArtifactoryVersion(version, hasAddons);
        } finally {
            if (content != null) {
                content.close();
            }
        }
    }
    return ArtifactoryVersion.NOT_FOUND;
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 63 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project build-info by JFrogDev.

the class ArtifactoryHttpClient method execute.

public ArtifactoryUploadResponse execute(HttpPut httpPut) throws IOException {
    HttpResponse response = getHttpClient().execute(httpPut);
    ArtifactoryUploadResponse artifactoryResponse = null;
    if (response.getEntity() != null && response.getEntity().getContent() != null) {
        InputStream in = response.getEntity().getContent();
        String content = IOUtils.toString(in, "UTF-8");
        if (StringUtils.isNotEmpty(content)) {
            try {
                JsonParser parser = createJsonParser(content);
                artifactoryResponse = parser.readValueAs(ArtifactoryUploadResponse.class);
            } catch (Exception e) {
                // Displays the response received from the client and the stacktrace in case an Exception caught.
                log.info("Response received: \n\n" + content + "\n\n");
                log.error("Failed while reading the response from: " + httpPut, e);
            } finally {
                in.close();
            }
        }
    }
    if (artifactoryResponse == null) {
        artifactoryResponse = new ArtifactoryUploadResponse();
    }
    StatusLine statusLine = response.getStatusLine();
    artifactoryResponse.setStatusLine(statusLine);
    return artifactoryResponse;
}
Also used : StatusLine(org.apache.http.StatusLine) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 64 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method readResponse.

/**
 * Reads HTTP response and converts it to object of the type specified.
 *
 * @param response     response to read
 * @param valueType    response object type
 * @param errorMessage error message to throw in case of error
 * @param <T>          response object type
 * @return response object converted from HTTP Json reponse to the type specified.
 * @throws java.io.IOException if reading or converting response fails.
 */
private <T> T readResponse(HttpResponse response, TypeReference<T> valueType, String errorMessage, boolean ignorMissingFields) throws IOException {
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            return null;
        }
        InputStream content = null;
        try {
            content = entity.getContent();
            JsonParser parser = httpClient.createJsonParser(content);
            if (ignorMissingFields) {
                ((ObjectMapper) parser.getCodec()).configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
            }
            // http://wiki.fasterxml.com/JacksonDataBinding
            return parser.readValueAs(valueType);
        } finally {
            if (content != null) {
                IOUtils.closeQuietly(content);
            }
        }
    } else {
        HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null) {
            IOUtils.closeQuietly(httpEntity.getContent());
        }
        throw new IOException(errorMessage + ": " + response.getStatusLine());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 65 with JsonParser

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser in project build-info by JFrogDev.

the class BuildInfoExtractorUtils method jsonStringToGeneric.

public static <T extends Serializable> T jsonStringToGeneric(String json, Class<T> clazz) throws IOException {
    JsonFactory jsonFactory = createJsonFactory();
    JsonParser parser = jsonFactory.createJsonParser(new StringReader(json));
    return jsonFactory.getCodec().readValue(parser, clazz);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

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)39 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