Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MAX_ERROR_TOKEN_LENGTH configurable via ErrorReportConfiguration #1043

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/ErrorReportConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package com.fasterxml.jackson.core;

import java.io.Serializable;

/**
* Container for configuration values used when handling errorneous token inputs.
* For example, unquoted text segments.
* <p>
* Currently default settings are
* <ul>
* <li>Maximum length of token to include in error messages (see {@link #_maxErrorTokenLength})
* <li>Maximum length of raw content to include in error messages (see {@link #_maxRawContentLength})
* </ul>
*
* @since 2.16
*/
public class ErrorReportConfiguration
implements Serializable {
private static final long serialVersionUID = 1L;

/**
* Default value for {@link #_maxErrorTokenLength}.
*/
public static final int DEFAULT_MAX_ERROR_TOKEN_LENGTH = 256;

/**
* Previous was {@link com.fasterxml.jackson.core.io.ContentReference#DEFAULT_MAX_CONTENT_SNIPPET}.
* Default value for {@link #_maxRawContentLength}.
*/
public static final int DEFAULT_MAX_RAW_CONTENT_LENGTH = 500;

/**
* Maximum length of token to include in error messages
*
* @see Builder#maxErrorTokenLength(int)
*/
protected final int _maxErrorTokenLength;

/**
* Maximum length of raw content to include in error messages
*
* @see Builder#maxRawContentLength(int)
*/
protected final int _maxRawContentLength;

private static ErrorReportConfiguration DEFAULT =
new ErrorReportConfiguration(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH);

public static void overrideDefaultErrorReportConfiguration(final ErrorReportConfiguration errorReportConfiguration) {
if (errorReportConfiguration == null) {
DEFAULT = new ErrorReportConfiguration(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH);
} else {
DEFAULT = errorReportConfiguration;
}
}

/*
/**********************************************************************
/* Builder
/**********************************************************************
*/

public static final class Builder {
private int maxErrorTokenLength;
private int maxRawContentLength;

/**
* @param maxErrorTokenLength Constraints
* @return This factory instance (to allow call chaining)
* @throws IllegalArgumentException if {@code maxErrorTokenLength} is less than 0
*/
public Builder maxErrorTokenLength(final int maxErrorTokenLength) {
validateMaxErrorTokenLength(maxErrorTokenLength);
this.maxErrorTokenLength = maxErrorTokenLength;
return this;
}

/**
*
* @see ErrorReportConfiguration#_maxRawContentLength
* @return This factory instance (to allow call chaining)
*/
public Builder maxRawContentLength(final int maxRawContentLength) {
validateMaxRawContentLength(maxRawContentLength);
this.maxRawContentLength = maxRawContentLength;
return this;
}

Builder() {
this(DEFAULT_MAX_ERROR_TOKEN_LENGTH, DEFAULT_MAX_RAW_CONTENT_LENGTH);
}

Builder(final int maxErrorTokenLength, final int maxRawContentLength) {
this.maxErrorTokenLength = maxErrorTokenLength;
this.maxRawContentLength = maxRawContentLength;
}

Builder(ErrorReportConfiguration src) {
this.maxErrorTokenLength = src._maxErrorTokenLength;
this.maxRawContentLength = src._maxRawContentLength;
}

public ErrorReportConfiguration build() {
return new ErrorReportConfiguration(maxErrorTokenLength, maxRawContentLength);
}
}

/*
/**********************************************************************
/* Life-cycle
/**********************************************************************
*/

protected ErrorReportConfiguration(final int maxErrorTokenLength, final int maxRawContentLength) {
_maxErrorTokenLength = maxErrorTokenLength;
_maxRawContentLength = maxRawContentLength;
}

public static ErrorReportConfiguration.Builder builder() {
return new ErrorReportConfiguration.Builder();
}

/**
* @return the default {@link ErrorReportConfiguration} (when none is set on the {@link JsonFactory} explicitly)
* @see #overrideDefaultErrorReportConfiguration(ErrorReportConfiguration)
*/
public static ErrorReportConfiguration defaults() {
return DEFAULT;
}

/**
* @return New {@link ErrorReportConfiguration.Builder} initialized with settings of configuration
* instance
*/
public ErrorReportConfiguration.Builder rebuild() {
return new ErrorReportConfiguration.Builder(this);
}

/*
/**********************************************************************
/* Accessors
/**********************************************************************
*/

/**
* Accessor for {@link #_maxErrorTokenLength}
*
* @return Maximum length of token to include in error messages
* @see Builder#maxErrorTokenLength(int)
*/
public int getMaxErrorTokenLength() {
return _maxErrorTokenLength;
}

/**
* Accessor for {@link #_maxRawContentLength}
*
* @return Maximum length of token to include in error messages
* @see Builder#maxRawContentLength
*/
public int getMaxRawContentLength() {
return _maxRawContentLength;
}

/*
/**********************************************************************
/* Convenience methods for validation
/**********************************************************************
*/

/**
* Convenience method that can be used verify valid {@link #_maxErrorTokenLength}.
* If invalid value is passed in, {@link IllegalArgumentException} is thrown.
*
* @param maxErrorTokenLength Maximum length of token to include in error messages
*/
private static void validateMaxErrorTokenLength(int maxErrorTokenLength) throws IllegalArgumentException {
if (maxErrorTokenLength < 0) {
throw new IllegalArgumentException(
String.format("Value of maxErrorTokenLength (%d) cannot be negative", maxErrorTokenLength));
}
}

private static void validateMaxRawContentLength(int maxRawContentLength) {
if (maxRawContentLength < 0) {
throw new IllegalArgumentException(
String.format("Value of maxRawContentLength (%d) cannot be negative", maxRawContentLength));
}
}

}
44 changes: 38 additions & 6 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ public static int collectDefaults() {
*/
protected StreamReadConstraints _streamReadConstraints;

/**
* Container for configuration values used when handling errorneous token inputs.
*
* @see ErrorReportConfiguration
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/**
* Write constraints to use for {@link JsonGenerator}s constructed using
* this factory.
Expand Down Expand Up @@ -361,6 +369,7 @@ public JsonFactory(ObjectCodec oc) {
_quoteChar = DEFAULT_QUOTE_CHAR;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand All @@ -385,6 +394,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
_generatorDecorators = _copy(src._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(src._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(src._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(src._errorReportConfiguration);

// JSON-specific
_characterEscapes = src._characterEscapes;
Expand Down Expand Up @@ -412,6 +422,7 @@ public JsonFactory(JsonFactoryBuilder b) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

// JSON-specific
_characterEscapes = b._characterEscapes;
Expand Down Expand Up @@ -439,6 +450,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

// JSON-specific: need to assign even if not really used
_characterEscapes = null;
Expand Down Expand Up @@ -838,12 +850,32 @@ public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
return this;
}

/**
* Method for overriding {@link ErrorReportConfiguration} defined for
* this factory.
*<p>
* NOTE: the preferred way to set constraints is by using
* {@link JsonFactoryBuilder#errorReportConfiguration}: this method is only
* provided to support older non-builder-based construction.
* In Jackson 3.x this method will not be available.
*
* @param src Configuration
*
* @return This factory instance (to allow call chaining)
*
* @since 2.16
*/
public JsonFactory setErrorReportConfiguration(ErrorReportConfiguration src) {
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");;
return this;
}

/**
* Method for overriding {@link StreamWriteConstraints} defined for
* this factory.
*<p>
* NOTE: the preferred way to set constraints is by using
* {@link JsonFactoryBuilder#streamWriteConstraints}: this method is only
* {@link JsonFactoryBuilder#_streamWriteConstraints}: this method is only
* provided to support older non-builder-based construction.
* In Jackson 3.x this method will not be available.
*
Expand Down Expand Up @@ -2113,7 +2145,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
contentRef = ContentReference.unknown();
}
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
_getBufferRecycler(), contentRef, resourceManaged);
_getBufferRecycler(), contentRef, resourceManaged, _errorReportConfiguration);
}

/**
Expand All @@ -2131,7 +2163,7 @@ protected IOContext _createContext(Object rawContentRef, boolean resourceManaged
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
_getBufferRecycler(),
_createContentReference(rawContentRef),
resourceManaged);
resourceManaged, _errorReportConfiguration);
}

/**
Expand All @@ -2150,7 +2182,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
_getBufferRecycler(),
_createContentReference(srcRef),
false);
false, _errorReportConfiguration);
}

/**
Expand All @@ -2168,7 +2200,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
protected ContentReference _createContentReference(Object contentAccessor) {
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
// indicator of textual vs binary format:
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor);
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor, _errorReportConfiguration);
}

/**
Expand All @@ -2192,7 +2224,7 @@ protected ContentReference _createContentReference(Object contentAccessor,
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
// indicator of textual vs binary format:
return ContentReference.construct(!canHandleBinaryNatively(),
contentAccessor, offset, length);
contentAccessor, offset, length, _errorReportConfiguration);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*<p>
* Since Jackson 2.12 extends intermediate {@link JacksonException} type
* instead of directly extending {@link java.io.IOException}.
* <p>
* Since Jackson 2.16, truncates the exception message or the raw content after length specified with configured
* {@link com.fasterxml.jackson.core.ErrorReportConfiguration} or by its defaults.
*/
public class JsonProcessingException extends JacksonException
{
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public abstract class TSFBuilder<F extends JsonFactory,
*/
protected List<JsonGeneratorDecorator> _generatorDecorators;

/**
* Optional {@link ErrorReportConfiguration} to use.
*
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/*
/**********************************************************************
/* Construction
Expand All @@ -120,6 +127,7 @@ protected TSFBuilder(JsonFactory base)
_outputDecorator = base._outputDecorator;
_streamReadConstraints = base._streamReadConstraints;
_streamWriteConstraints = base._streamWriteConstraints;
_errorReportConfiguration = base._errorReportConfiguration;
_generatorDecorators = _copy(base._generatorDecorators);
}

Expand All @@ -134,6 +142,7 @@ protected TSFBuilder(int factoryFeatures,
_outputDecorator = null;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand Down Expand Up @@ -323,7 +332,18 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {
_streamReadConstraints = streamReadConstraints;
return _this();
}


/**
* Sets the configuration for error tokens.
*
* @param errorReportConfiguration configuration values used for handling errorneous token inputs.
* @return this factory
* @since 2.16
*/
public B errorReportConfiguration(ErrorReportConfiguration errorReportConfiguration) {
_errorReportConfiguration = errorReportConfiguration;
return _this();
}
/**
* Sets the constraints for streaming writes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public abstract class ParserMinimalBase extends JsonParser
* as part of error messages.
*
* @since 2.9
* @deprecated Since 2.16
* @see ErrorReportConfiguration#getMaxErrorTokenLength()
*/
@Deprecated
protected final static int MAX_ERROR_TOKEN_LENGTH = 256;

/*
Expand Down
Loading