001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Salem Aouana
018 */
019
020package org.nuxeo.elasticsearch.hint;
021
022import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
023import static org.elasticsearch.common.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION;
024
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import java.util.Arrays;
028
029import org.elasticsearch.ElasticsearchParseException;
030import org.elasticsearch.common.geo.GeoPoint;
031import org.elasticsearch.common.geo.GeoUtils;
032import org.elasticsearch.common.xcontent.NamedXContentRegistry;
033import org.elasticsearch.common.xcontent.XContentBuilder;
034import org.elasticsearch.common.xcontent.XContentParser;
035import org.elasticsearch.common.xcontent.json.JsonXContent;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.elasticsearch.api.ESHintQueryBuilder;
038
039/**
040 * Abstract implementation of {@link ESHintQueryBuilder} that manages the geo queries.
041 *
042 * @since 11.1
043 */
044public abstract class AbstractGeoESHintQueryBuilder implements ESHintQueryBuilder {
045
046    protected String[] validate(Object value, int arrayLength, String message) {
047        if (!(value instanceof Object[])) {
048            throw new NuxeoException(String.format("Expected an array, found %s", value.getClass()), SC_BAD_REQUEST);
049        }
050
051        Object[] values = (Object[]) value;
052        if (values.length != arrayLength) {
053            throw new NuxeoException(String.format("Hints: %s requires %s parameters: %s", getClass().getSimpleName(),
054                    arrayLength, message), SC_BAD_REQUEST);
055        }
056
057        return Arrays.asList(values).toArray(new String[0]);
058    }
059
060    protected GeoPoint parseGeoPointString(String value) {
061        try {
062            XContentBuilder content = JsonXContent.contentBuilder();
063            content.value(value);
064            content.flush();
065            content.close();
066            try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
067                    THROW_UNSUPPORTED_OPERATION, ((ByteArrayOutputStream) content.getOutputStream()).toByteArray())) {
068                parser.nextToken();
069                return GeoUtils.parseGeoPoint(parser);
070            }
071        } catch (IOException | ElasticsearchParseException e) {
072            throw new NuxeoException(String.format("Invalid value for Geo-point: %s", value), e, SC_BAD_REQUEST);
073        }
074    }
075}