Example usage for com.fasterxml.jackson.core JsonParser disable

List of usage examples for com.fasterxml.jackson.core JsonParser disable

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonParser disable.

Prototype

public JsonParser disable(Feature f) 

Source Link

Document

Method for disabling specified feature (check Feature for list of features)

Usage

From source file:com.proofpoint.jaxrs.SmileMapper.java

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;//from  w ww  .j  a  v a2 s  .co m
    try {
        JsonParser jsonParser = new SmileFactory().createParser(inputStream);

        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

        object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // we want to return a 400 for bad JSON but not for a real IO exception
        if (e instanceof IOException && !(e instanceof JsonProcessingException)
                && !(e instanceof EOFException)) {
            throw (IOException) e;
        }

        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);

        // Invalid json request. Throwing exception so the response code can be overridden using a mapper.
        throw new JsonMapperParsingException(type, e);
    }

    // validate object using the bean validation framework
    validateObject(genericType, object);

    return object;
}

From source file:com.proofpoint.http.client.SmileResponseHandler.java

@Override
public T handle(Request request, Response response) {
    if (!successfulResponseCodes.contains(response.getStatusCode())) {
        throw new UnexpectedResponseException(String.format("Expected response code to be %s, but was %d: %s",
                successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request,
                response);/*  w w  w.  ja v a 2 s .co  m*/
    }
    String contentType = response.getHeader(CONTENT_TYPE);
    if (contentType == null) {
        throw new UnexpectedResponseException("Content-Type is not set for response", request, response);
    }
    if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) {
        throw new UnexpectedResponseException(
                "Expected application/x-jackson-smile response from server but got " + contentType, request,
                response);
    }
    try {
        JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream());
        ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get();

        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

        return objectMapper.readValue(jsonParser,
                objectMapper.getTypeFactory().constructType(jsonCodec.getType()));
    } catch (InvalidFormatException e) {
        throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response",
                e);
    } catch (IOException e) {
        throw new RuntimeException("Error reading SMILE response from server", e);
    }
}

From source file:io.airlift.jaxrs.SmileMapper.java

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;//from  ww w  .j av  a  2s  .c  o  m
    try {
        JsonParser jsonParser = new SmileFactory().createParser(inputStream);

        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

        object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // we want to return a 400 for bad JSON but not for a real IO exception
        if (e instanceof IOException && !(e instanceof JsonProcessingException)
                && !(e instanceof EOFException)) {
            throw (IOException) e;
        }

        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);

        // invalid json request
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
                .entity("Invalid json for Java type " + type).build());
    }

    // validate object using the bean validation framework
    Set<ConstraintViolation<Object>> violations = VALIDATOR.validate(object);
    if (!violations.isEmpty()) {
        throw new WebApplicationException(
                Response.status(Response.Status.BAD_REQUEST).entity(messagesFor(violations)).build());
    }

    return object;
}

From source file:com.proofpoint.jaxrs.JsonMapper.java

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;//from  w w w . j  a va  2  s  .  c  o m
    try {
        JsonParser jsonParser = objectMapper.getFactory().createParser(inputStream);

        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

        object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // We want to handle parsing exceptions differently than regular IOExceptions so just rethrow IOExceptions
        if (e instanceof IOException && !(e instanceof JsonProcessingException)
                && !(e instanceof EOFException)) {
            throw e;
        }

        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);

        // Invalid json request. Throwing exception so the response code can be overridden using a mapper.
        throw new JsonMapperParsingException(type, e);
    }

    // validate object using the bean validation framework
    validateObject(genericType, object);

    return object;
}

From source file:io.airlift.jaxrs.JsonMapper.java

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    Object object;/*from  ww w. j  a  va2 s  .c  o  m*/
    try {
        JsonParser jsonParser = objectMapper.getFactory().createJsonParser(inputStream);

        // Important: we are NOT to close the underlying stream after
        // mapping, so we need to instruct parser:
        jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

        object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
    } catch (Exception e) {
        // We want to handle parsing exceptions differently than regular IOExceptions so just rethrow IOExceptions
        if (e instanceof IOException && !(e instanceof JsonProcessingException)
                && !(e instanceof EOFException)) {
            throw e;
        }

        // log the exception at debug so it can be viewed during development
        // Note: we are not logging at a higher level because this could cause a denial of service
        log.debug(e, "Invalid json for Java type %s", type);

        // Invalid json request. Throwing exception so the response code can be overridden using a mapper.
        throw new JsonMapperParsingException(type, e);
    }

    // validate object using the bean validation framework
    Set<ConstraintViolation<Object>> violations = VALIDATOR.validate(object);
    if (!violations.isEmpty()) {
        throw new BeanValidationException(violations);
    }

    return object;
}