Search in sources :

Example 96 with ElasticsearchParseException

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();
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) UncheckedIOException(java.io.UncheckedIOException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 97 with ElasticsearchParseException

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));
}
Also used : State(io.crate.user.Privilege.State) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Privilege(io.crate.user.Privilege) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 98 with ElasticsearchParseException

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;
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) SecureString(org.elasticsearch.common.settings.SecureString) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 99 with ElasticsearchParseException

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;
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 100 with ElasticsearchParseException

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);
    }
}
Also used : InputStream(java.io.InputStream) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)106 XContentParser (org.elasticsearch.common.xcontent.XContentParser)44 HashMap (java.util.HashMap)28 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)13 Map (java.util.Map)11 List (java.util.List)7 GeoPoint (org.elasticsearch.common.geo.GeoPoint)7 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ParsingException (org.elasticsearch.common.ParsingException)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 Version (org.elasticsearch.Version)4 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 UncheckedIOException (java.io.UncheckedIOException)3 LinkedHashSet (java.util.LinkedHashSet)3 PutPipelineRequest (org.elasticsearch.action.ingest.PutPipelineRequest)3