use of org.elasticsearch.ElasticsearchParseException in project crate by crate.
the class Settings method fromXContent.
private static Settings fromXContent(XContentParser parser, boolean allowNullValues, boolean validateEndOfStream) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
Builder innerBuilder = Settings.builder();
StringBuilder currentKeyBuilder = new StringBuilder();
fromXContent(parser, currentKeyBuilder, innerBuilder, allowNullValues);
if (validateEndOfStream) {
// ensure we reached the end of the stream
XContentParser.Token lastToken = null;
try {
while (!parser.isClosed() && (lastToken = parser.nextToken()) == null) ;
} catch (Exception e) {
throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", e, parser.getTokenLocation().lineNumber, parser.getTokenLocation().columnNumber);
}
if (lastToken != null) {
throw new ElasticsearchParseException("malformed, expected end of settings but encountered additional content starting at line number: [{}], " + "column number: [{}]", parser.getTokenLocation().lineNumber, parser.getTokenLocation().columnNumber);
}
}
return innerBuilder.build();
}
use of org.elasticsearch.ElasticsearchParseException in project crate by crate.
the class UsersPrivilegesMetadata method privilegeFromXContent.
private static void privilegeFromXContent(XContentParser parser, Set<Privilege> privileges) throws IOException {
XContentParser.Token currentToken;
State state = null;
Privilege.Type type = null;
Privilege.Clazz clazz = null;
String ident = null;
String grantor = null;
while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (currentToken == XContentParser.Token.FIELD_NAME) {
String currentFieldName = parser.currentName();
currentToken = parser.nextToken();
switch(currentFieldName) {
case "state":
if (currentToken != XContentParser.Token.VALUE_NUMBER) {
throw new ElasticsearchParseException("failed to parse privilege, 'state' value is not a number [{}]", currentToken);
}
state = State.values()[parser.intValue()];
break;
case "type":
if (currentToken != XContentParser.Token.VALUE_NUMBER) {
throw new ElasticsearchParseException("failed to parse privilege, 'type' value is not a number [{}]", currentToken);
}
type = Privilege.Type.values()[parser.intValue()];
break;
case "class":
if (currentToken != XContentParser.Token.VALUE_NUMBER) {
throw new ElasticsearchParseException("failed to parse privilege, 'class' value is not a number [{}]", currentToken);
}
clazz = Privilege.Clazz.values()[parser.intValue()];
break;
case "ident":
if (currentToken != XContentParser.Token.VALUE_STRING && currentToken != XContentParser.Token.VALUE_NULL) {
throw new ElasticsearchParseException("failed to parse privilege, 'ident' value is not a string or null [{}]", currentToken);
}
ident = parser.textOrNull();
break;
case "grantor":
if (currentToken != XContentParser.Token.VALUE_STRING) {
throw new ElasticsearchParseException("failed to parse privilege, 'grantor' value is not a string [{}]", currentToken);
}
grantor = parser.text();
break;
default:
throw new ElasticsearchParseException("failed to parse privilege");
}
} else if (currentToken == XContentParser.Token.END_ARRAY) {
// empty privileges set
return;
}
}
privileges.add(new Privilege(state, type, clazz, ident, grantor));
}
use of org.elasticsearch.ElasticsearchParseException in project crate by crate.
the class SecureHash method fromXContent.
public static SecureHash fromXContent(XContentParser parser) throws IOException {
XContentParser.Token currentToken;
int iterations = 0;
byte[] hash = new byte[0];
byte[] salt = new byte[0];
boolean hasPassword = false;
while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (currentToken == XContentParser.Token.FIELD_NAME) {
while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
// secure_hash
hasPassword = true;
if (currentToken == XContentParser.Token.FIELD_NAME) {
String currentFieldName = parser.currentName();
currentToken = parser.nextToken();
switch(currentFieldName) {
case X_CONTENT_KEY_ITERATIONS:
if (currentToken != XContentParser.Token.VALUE_NUMBER) {
throw new ElasticsearchParseException("failed to parse SecureHash, 'iterations' value is not a number [{}]", currentToken);
}
iterations = parser.intValue();
break;
case X_CONTENT_KEY_HASH:
if (currentToken.isValue() == false) {
throw new ElasticsearchParseException("failed to parse SecureHash, 'hash' does not contain any value [{}]", currentToken);
}
hash = parser.binaryValue();
break;
case X_CONTENT_KEY_SALT:
if (currentToken.isValue() == false) {
throw new ElasticsearchParseException("failed to parse SecureHash, 'salt' does not contain any value [{}]", currentToken);
}
salt = parser.binaryValue();
break;
default:
throw new ElasticsearchParseException("failed to parse secure_hash");
}
}
}
}
}
if (hasPassword) {
return SecureHash.of(iterations, salt, hash);
}
return null;
}
use of org.elasticsearch.ElasticsearchParseException in project crate by crate.
the class GeoJsonParser method parseGeometries.
/**
* Parse the geometries array of a GeometryCollection
*
* @param parser Parser that will be read from
* @return Geometry[] geometries of the GeometryCollection
* @throws IOException Thrown if an error occurs while reading from the XContentParser
*/
static GeometryCollectionBuilder parseGeometries(XContentParser parser, GeoShapeFieldMapper mapper) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
throw new ElasticsearchParseException("geometries must be an array of geojson objects");
}
XContentParser.Token token = parser.nextToken();
GeometryCollectionBuilder geometryCollection = new GeometryCollectionBuilder();
while (token != XContentParser.Token.END_ARRAY) {
ShapeBuilder shapeBuilder = ShapeParser.parse(parser);
geometryCollection.shape(shapeBuilder);
token = parser.nextToken();
}
return geometryCollection;
}
use of org.elasticsearch.ElasticsearchParseException in project crate by crate.
the class GeoUtils method parseGeoPoint.
/**
* Parses the value as a geopoint. The following types of values are supported:
* <p>
* Object: has to contain either lat and lon or geohash fields
* <p>
* String: expected to be in "latitude, longitude" format or a geohash
* <p>
* Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
*/
public static GeoPoint parseGeoPoint(Object value, final boolean ignoreZValue) throws ElasticsearchParseException {
try {
XContentBuilder content = JsonXContent.contentBuilder();
content.startObject();
content.field("null_value", value);
content.endObject();
try (InputStream stream = BytesReference.bytes(content).streamInput();
XContentParser parser = JsonXContent.JSON_XCONTENT.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
return parseGeoPoint(parser, new GeoPoint(), ignoreZValue);
}
} catch (IOException ex) {
throw new ElasticsearchParseException("error parsing geopoint", ex);
}
}
Aggregations