Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.exc.InputCoercionException;
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
import com.fasterxml.jackson.core.io.JsonEOFException;

import org.elasticsearch.core.CheckedSupplier;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.xcontent.XContentEOFException;
import org.elasticsearch.xcontent.XContentLocation;
Expand All @@ -26,6 +28,7 @@
import org.elasticsearch.xcontent.provider.XContentParserConfigurationImpl;
import org.elasticsearch.xcontent.support.AbstractXContentParser;

import java.io.CharConversionException;
import java.io.IOException;
import java.nio.CharBuffer;

Expand All @@ -48,30 +51,52 @@ public void allowDuplicateKeys(boolean allowDuplicateKeys) {
parser.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, allowDuplicateKeys == false);
}

private static XContentParseException newXContentParseException(JsonProcessingException e) {
private static XContentLocation getLocation(JsonProcessingException e) {
JsonLocation loc = e.getLocation();
throw new XContentParseException(new XContentLocation(loc.getLineNr(), loc.getColumnNr()), e.getMessage(), e);
if (loc != null) {
return new XContentLocation(loc.getLineNr(), loc.getColumnNr());
} else {
return null;
}
}

@Override
public Token nextToken() throws IOException {
private static XContentParseException newXContentParseException(JsonProcessingException e) {
return new XContentParseException(getLocation(e), e.getMessage(), e);
}

/**
* Handle parser exception depending on type.
* This converts known exceptions to XContentParseException and rethrows them.
*/
private static IOException handleParserException(IOException e) throws IOException {
switch (e) {
case JsonEOFException eof -> throw new XContentEOFException(getLocation(eof), "Unexpected end of file", e);
case JsonParseException pe -> throw newXContentParseException(pe);
case InputCoercionException ice -> throw newXContentParseException(ice);
case CharConversionException cce -> throw new XContentParseException(null, cce.getMessage(), cce);
case StreamConstraintsException sce -> throw newXContentParseException(sce);
default -> {
return e;
}
}
}

private static <T> T safeParse(CheckedSupplier<T, IOException> runnable) throws IOException {
try {
return convertToken(parser.nextToken());
} catch (JsonEOFException e) {
JsonLocation location = e.getLocation();
throw new XContentEOFException(new XContentLocation(location.getLineNr(), location.getColumnNr()), "Unexpected end of file", e);
} catch (JsonParseException e) {
throw newXContentParseException(e);
return runnable.get();
} catch (IOException e) {
throw handleParserException(e);
}
}

@Override
public Token nextToken() throws IOException {
return safeParse(() -> convertToken(parser.nextToken()));
}

@Override
public String nextFieldName() throws IOException {
try {
return parser.nextFieldName();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::nextFieldName);
}

@Override
Expand All @@ -96,23 +121,15 @@ public String currentName() throws IOException {

@Override
protected boolean doBooleanValue() throws IOException {
try {
return parser.getBooleanValue();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getBooleanValue);
}

@Override
public String text() throws IOException {
if (currentToken().isValue() == false) {
throwOnNoText();
}
try {
return parser.getText();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getText);
}

private void throwOnNoText() {
Expand All @@ -121,11 +138,7 @@ private void throwOnNoText() {

@Override
public CharBuffer charBuffer() throws IOException {
try {
return CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(() -> CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
}

@Override
Expand Down Expand Up @@ -171,92 +184,52 @@ public boolean hasTextCharacters() {

@Override
public char[] textCharacters() throws IOException {
try {
return parser.getTextCharacters();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getTextCharacters);
}

@Override
public int textLength() throws IOException {
try {
return parser.getTextLength();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getTextLength);
}

@Override
public int textOffset() throws IOException {
try {
return parser.getTextOffset();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getTextOffset);
}

@Override
public Number numberValue() throws IOException {
try {
return parser.getNumberValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getNumberValue);
}

@Override
public short doShortValue() throws IOException {
try {
return parser.getShortValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getShortValue);
}

@Override
public int doIntValue() throws IOException {
try {
return parser.getIntValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getIntValue);
}

@Override
public long doLongValue() throws IOException {
try {
return parser.getLongValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getLongValue);
}

@Override
public float doFloatValue() throws IOException {
try {
return parser.getFloatValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getFloatValue);
}

@Override
public double doDoubleValue() throws IOException {
try {
return parser.getDoubleValue();
} catch (InputCoercionException | JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getDoubleValue);
}

@Override
public byte[] binaryValue() throws IOException {
try {
return parser.getBinaryValue();
} catch (JsonParseException e) {
throw newXContentParseException(e);
}
return safeParse(parser::getBinaryValue);
}

@Override
Expand Down