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

Retrofit IOContext with ErrorReportConfiguration #1068

Merged
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 Builder builder() {
return new 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 Builder} initialized with settings of configuration
* instance
*/
public Builder rebuild() {
return new 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));
}
}

}
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
44 changes: 44 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/io/IOContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.core.io;

import com.fasterxml.jackson.core.ErrorReportConfiguration;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.StreamWriteConstraints;
Expand Down Expand Up @@ -68,6 +69,12 @@ public class IOContext
*/
protected final StreamReadConstraints _streamReadConstraints;

/**
* @see ErrorReportConfiguration
* @since 2.16
*/
protected final ErrorReportConfiguration _errorReportConfiguration;

/**
* @since 2.16
*/
Expand Down Expand Up @@ -129,6 +136,8 @@ public class IOContext
* @param managedResource Whether input source is managed (owned) by Jackson library
*
* @since 2.16
* @deprecated Since 2.16, use {@link #IOContext(StreamReadConstraints, StreamWriteConstraints, BufferRecycler,
* ContentReference, boolean, ErrorReportConfiguration)} instead.
*/
public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRecycler br,
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
ContentReference contentRef, boolean managedResource)
Expand All @@ -141,6 +150,30 @@ public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRe
_contentReference = contentRef;
_sourceRef = contentRef.getRawContent();
_managedResource = managedResource;
_errorReportConfiguration = ErrorReportConfiguration.defaults();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead call:

this(src, swc, br, contentRef, managedResource, ErrorReportConfiguration.defaults();

Copy link
Member Author

@JooHyukKim JooHyukKim Jul 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned above in Notes.1 as...

Use defaults() method and not use new constructor in old constructors

... to minimize changes (git revision), but maybe I have taken it too literally 😅.

Done. Thank you!

}

/**
* Main constructor to use.
*
* @param src constraints for streaming reads
* @param swc constraints for streaming writes
* @param br BufferRecycler to use, if any ({@code null} if none)
* @param contentRef Input source reference for location reporting
* @param managedResource Whether input source is managed (owned) by Jackson library
*
* @since 2.16
*/
public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRecycler br,
ContentReference contentRef, boolean managedResource, ErrorReportConfiguration erc)
{
_streamReadConstraints = src;
_streamWriteConstraints = swc;
_bufferRecycler = br;
_contentReference = contentRef;
_sourceRef = contentRef.getRawContent();
_managedResource = managedResource;
_errorReportConfiguration = erc;
}

/**
Expand All @@ -163,6 +196,7 @@ public IOContext(StreamReadConstraints src, BufferRecycler br,
_contentReference = contentRef;
_sourceRef = contentRef.getRawContent();
_managedResource = managedResource;
_errorReportConfiguration = ErrorReportConfiguration.defaults();
}

/**
Expand Down Expand Up @@ -199,6 +233,16 @@ public StreamWriteConstraints streamWriteConstraints() {
return _streamWriteConstraints;
}

/**
* Returns : {@link ErrorReportConfiguration}, container for configuration values used when
* handling errorneous token inputs.
*
* @since 2.16
*/
public ErrorReportConfiguration errorReportConfiguration() {
return _errorReportConfiguration;
pjfanning marked this conversation as resolved.
Show resolved Hide resolved
}

public void setEncoding(JsonEncoding enc) {
_encoding = enc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3033,7 +3033,7 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
}
++_inputPtr;
sb.append(c);
if (sb.length() >= MAX_ERROR_TOKEN_LENGTH) {
if (sb.length() >= _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
sb.append("...");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3691,7 +3691,7 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
break;
}
sb.append(c);
if (sb.length() >= MAX_ERROR_TOKEN_LENGTH) {
if (sb.length() >= _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
sb.append("...");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ protected JsonToken _finishErrorToken() throws IOException
// 11-Jan-2016, tatu: note: we will fully consume the character,
// included or not, so if recovery was possible, it'd be off-by-one...
_textBuffer.append(ch);
if (_textBuffer.size() < MAX_ERROR_TOKEN_LENGTH) {
if (_textBuffer.size() < _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
continue;
}
}
Expand Down
Loading