Search in sources :

Example 56 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project uPortal by Jasig.

the class AnalyticsIncorporationComponent method serializePortletRenderExecutionEvents.

protected String serializePortletRenderExecutionEvents(final Set<PortalEvent> portalEvents) {
    // Filter to include just portlet render events
    final Map<String, PortletRenderExecutionEvent> renderEvents = new HashMap<>();
    for (final PortalEvent portalEvent : portalEvents) {
        if (portalEvent instanceof PortletRenderExecutionEvent) {
            final PortletRenderExecutionEvent portletRenderEvent = (PortletRenderExecutionEvent) portalEvent;
            // Don't write out info for minimized portlets
            if (!WindowState.MINIMIZED.equals(portletRenderEvent.getWindowState())) {
                final IPortletWindowId portletWindowId = portletRenderEvent.getPortletWindowId();
                final String eventKey = portletWindowId != null ? portletWindowId.getStringId() : portletRenderEvent.getFname();
                renderEvents.put(eventKey, portletRenderEvent);
            }
        }
    }
    try {
        return portletEventWriter.writeValueAsString(renderEvents);
    } catch (JsonParseException e) {
        logger.warn("Failed to convert this request's render events to JSON, no portlet level analytics will be included", e);
    } catch (JsonMappingException e) {
        logger.warn("Failed to convert this request's render events to JSON, no portlet level analytics will be included", e);
    } catch (IOException e) {
        logger.warn("Failed to convert this request's render events to JSON, no portlet level analytics will be included", e);
    }
    return "{}";
}
Also used : PortalEvent(org.apereo.portal.events.PortalEvent) HashMap(java.util.HashMap) PortletRenderExecutionEvent(org.apereo.portal.events.PortletRenderExecutionEvent) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IPortletWindowId(org.apereo.portal.portlet.om.IPortletWindowId)

Example 57 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project swagger-core by swagger-api.

the class SecuritySchemeDeserializer method deserialize.

@Override
public SecurityScheme deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectMapper mapper = null;
    if (openapi31) {
        mapper = Json31.mapper();
    } else {
        mapper = Json.mapper();
    }
    SecurityScheme result = null;
    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode inNode = node.get("type");
    if (inNode != null) {
        String type = inNode.asText();
        if (Arrays.stream(SecurityScheme.Type.values()).noneMatch(t -> t.toString().equals(type))) {
            // wrong type, throw exception
            throw new JsonParseException(jp, String.format("SecurityScheme type %s not allowed", type));
        }
        result = new SecurityScheme().description(getFieldText("description", node));
        if ("http".equals(type)) {
            result.type(SecurityScheme.Type.HTTP).scheme(getFieldText("scheme", node)).bearerFormat(getFieldText("bearerFormat", node));
        } else if ("apiKey".equals(type)) {
            result.type(SecurityScheme.Type.APIKEY).name(getFieldText("name", node)).in(getIn(getFieldText("in", node)));
        } else if ("openIdConnect".equals(type)) {
            result.type(SecurityScheme.Type.OPENIDCONNECT).openIdConnectUrl(getFieldText("openIdConnectUrl", node));
        } else if ("oauth2".equals(type)) {
            result.type(SecurityScheme.Type.OAUTH2).flows(mapper.convertValue(node.get("flows"), OAuthFlows.class));
        } else if ("mutualTLS".equals(type)) {
            result.type(SecurityScheme.Type.MUTUALTLS);
        }
    }
    return result;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 58 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project snow-owl by b2ihealthcare.

the class ExpandParser method parse.

public static Options parse(final String expand) {
    String jsonizedOptionPart = String.format("{%s}", expand.replace("(", ":{").replace(')', '}'));
    try {
        JsonParser parser = JSON_FACTORY.createParser(jsonizedOptionPart);
        parser.setCodec(new ObjectMapper());
        Map<String, Object> source = parser.<Map<String, Object>>readValueAs(new TypeReference<Map<String, Object>>() {
        });
        return OptionsBuilder.newBuilder().putAll(source).build();
    } catch (JsonParseException e) {
        throw new BadRequestException("Expansion parameter %s is malformed.", expand);
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Caught I/O exception while reading expansion parameters.", e);
    }
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 59 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project drools by kiegroup.

the class JsonUtils method convertFromStringToJSONNode.

/**
 * This method aim is to to evaluate if any possible String is a valid json or not.
 * Given a json in String format, it try to convert it in a <code>JsonNode</code>. In case of success, i.e.
 * the given string is a valid json, it put the <code>JsonNode</code> in a <code>Optional</code>. An empty
 * <code>Optional</code> is passed otherwise.
 * @param json
 * @return
 */
public static Optional<JsonNode> convertFromStringToJSONNode(String json) {
    if (json == null || json.isEmpty()) {
        return Optional.empty();
    }
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(json);
        return Optional.of(jsonNode);
    } catch (JsonParseException e) {
        return Optional.empty();
    } catch (IOException e) {
        throw new IllegalArgumentException("Generic error during json parsing: " + json, e);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 60 with JsonParseException

use of com.fasterxml.jackson.core.JsonParseException in project carbon-apimgt by wso2.

the class JSONAnalyzer method analyze.

/**
 * Analyze the JSON payload against limitations.
 * @param in input stream of the request payload.
 * @param apiContext request api context.
 * @throws APIMThreatAnalyzerException if defined limits for json payload exceeds
 */
@Override
public void analyze(InputStream in, String apiContext) throws APIMThreatAnalyzerException {
    try (JsonParser parser = factory.createParser(new InputStreamReader(in))) {
        int currentDepth = 0;
        int currentFieldCount = 0;
        JsonToken token;
        while ((token = parser.nextToken()) != null) {
            switch(token) {
                case START_OBJECT:
                    currentDepth += 1;
                    analyzeDepth(maxJsonDepth, currentDepth, apiContext);
                    break;
                case END_OBJECT:
                    currentDepth -= 1;
                    break;
                case FIELD_NAME:
                    currentFieldCount += 1;
                    String name = parser.getCurrentName();
                    analyzeField(name, maxFieldCount, currentFieldCount, maxFieldLength);
                    break;
                case VALUE_STRING:
                    String value = parser.getText();
                    analyzeString(value, maxStringLength);
                    break;
                case START_ARRAY:
                    analyzeArray(parser, maxArrayElementCount, maxStringLength);
            }
        }
    } catch (JsonParseException e) {
        throw new APIMThreatAnalyzerException("Error occurred while parsing the JSON payload", e);
    } catch (IOException e) {
        throw new APIMThreatAnalyzerException("Error occurred while reading the JSON payload.", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.gateway.threatprotection.APIMThreatAnalyzerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParseException (com.fasterxml.jackson.core.JsonParseException)143 IOException (java.io.IOException)73 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)58 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 JsonParser (com.fasterxml.jackson.core.JsonParser)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 Map (java.util.Map)18 JsonToken (com.fasterxml.jackson.core.JsonToken)15 InputStream (java.io.InputStream)15 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 File (java.io.File)11 HashMap (java.util.HashMap)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 JsonFactory (com.fasterxml.jackson.core.JsonFactory)7 JsonLocation (com.fasterxml.jackson.core.JsonLocation)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 GZIPInputStream (java.util.zip.GZIPInputStream)5