Search in sources :

Example 1 with ParsingException

use of org.parboiled.errors.ParsingException in project ddf by codice.

the class OpenSearchQuery method addContextualFilter.

public void addContextualFilter(String searchTerm, String selectors) throws ParsingException {
    String methodName = "addContextualFilter";
    Filter filter = null;
    KeywordFilterGenerator keywordFilterGenerator = new KeywordFilterGenerator(filterBuilder);
    KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
    // translate the search terms into an abstract syntax tree
    ParsingResult<ASTNode> result = new RecoveringParseRunner(parser.inputPhrase()).run(searchTerm);
    // make sure it's a good result before using it
    if (result.matched && !result.hasErrors()) {
        filter = generateContextualFilter(selectors, keywordFilterGenerator, result);
    } else if (result.hasErrors()) {
        throw new ParsingException("Unable to parse keyword search phrase. " + generateParsingError(result));
    }
    if (filter != null) {
        filters.add(filter);
    }
}
Also used : KeywordFilterGenerator(org.codice.ddf.endpoints.KeywordFilterGenerator) SpatialFilter(ddf.catalog.impl.filter.SpatialFilter) TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) SpatialDistanceFilter(ddf.catalog.impl.filter.SpatialDistanceFilter) PolygonSpatialFilter(org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter) ParsingException(org.parboiled.errors.ParsingException) ASTNode(org.codice.ddf.endpoints.ASTNode) RecoveringParseRunner(org.parboiled.parserunners.RecoveringParseRunner) KeywordTextParser(org.codice.ddf.endpoints.KeywordTextParser)

Example 2 with ParsingException

use of org.parboiled.errors.ParsingException in project ddf by codice.

the class OpenSearchEndpoint method processQuery.

/**
     * @param searchTerms Space delimited list of search terms.
     * @param maxResults  Maximum # of results to return. If count is also specified, the count value will
     *                    take precedence over the maxResults value
     * @param sources     Comma delimited list of data sources to query (default: default sources selected).
     * @param maxTimeout  Maximum timeout (msec) for query to respond (default: mt=30000).
     * @param startIndex  Index of first result to return. Integer >= 0 (default: start=1).
     * @param count       Number of results to retrieve per page (default: count=10).
     * @param geometry    WKT Geometries (Support POINT and POLYGON).
     * @param bbox        Comma delimited list of lat/lon (deg) bounding box coordinates (geo format:
     *                    geo:bbox ~ West,South,East,North).
     * @param polygon     Comma delimited list of lat/lon (deg) pairs, in clockwise order around the
     *                    polygon, with the last point being the same as the first in order to close the
     *                    polygon.
     * @param lat         Latitude in decimal degrees (typical GPS receiver WGS84 coordinates).
     * @param lon         Longitude in decimal degrees (typical GPS receiver WGS84 coordinates).
     * @param radius      The radius (m) parameter, used with the lat and lon parameters, specifies the
     *                    search distance from this point (default: radius=5000).
     * @param dateStart   Specifies the beginning of the time slice of the search on the modified time field
     *                    (RFC-3339 - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Default value of
     *                    "1970-01-01T00:00:00Z" is used when dtend is indicated but dtstart is not
     *                    specified
     * @param dateEnd     Specifies the ending of the time slice of the search on the modified time field
     *                    (RFC-3339 - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Current GMT
     *                    date/time is used when dtstart is specified but not dtend.
     * @param dateOffset  Specifies an offset, backwards from the current time, to search on the modified
     *                    time field for entries. Defined in milliseconds.
     * @param sort        Specifies sort by field as sort=<sbfield>:<sborder>, where <sbfield> may be 'date'
     *                    or 'relevance' (default is 'relevance'). The conditional param <sborder> is
     *                    optional but has a value of 'asc' or 'desc' (default is 'desc'). When <sbfield> is
     *                    'relevance', <sborder> must be 'desc'.
     * @param format      Defines the format that the return type should be in. (example:atom, html)
     * @param selector    Defines a comma delimited list of XPath selectors to narrow the query.
     * @param type        Specifies the type of data to search for. (example: nitf)
     * @param versions    Specifies the versions in a comma delimited list.
     * @return
     */
@GET
public Response processQuery(@QueryParam(PHRASE) String searchTerms, @QueryParam(MAX_RESULTS) String maxResults, @QueryParam(SOURCES) String sources, @QueryParam(MAX_TIMEOUT) String maxTimeout, @QueryParam(START_INDEX) String startIndex, @QueryParam(COUNT) String count, @QueryParam(GEOMETRY) String geometry, @QueryParam(BBOX) String bbox, @QueryParam(POLYGON) String polygon, @QueryParam(LAT) String lat, @QueryParam(LON) String lon, @QueryParam(RADIUS) String radius, @QueryParam(DATE_START) String dateStart, @QueryParam(DATE_END) String dateEnd, @QueryParam(DATE_OFFSET) String dateOffset, @QueryParam(SORT) String sort, @QueryParam(FORMAT) String format, @QueryParam(SELECTOR) String selector, @Context UriInfo ui, @QueryParam(TYPE) String type, @QueryParam(VERSION) String versions, @Context HttpServletRequest request) {
    final String methodName = "processQuery";
    LOGGER.trace("ENTERING: {}", methodName);
    Response response;
    String localCount = count;
    LOGGER.debug("request url: {}", ui.getRequestUri());
    // honor maxResults if count is not specified
    if ((StringUtils.isEmpty(localCount)) && (!(StringUtils.isEmpty(maxResults)))) {
        LOGGER.debug("setting count to: {}", maxResults);
        localCount = maxResults;
    }
    try {
        String queryFormat = format;
        OpenSearchQuery query = createNewQuery(startIndex, localCount, sort, maxTimeout);
        if (!(StringUtils.isEmpty(sources))) {
            LOGGER.debug("Received site names from client.");
            Set<String> siteSet = new HashSet<String>(Arrays.asList(StringUtils.stripAll(sources.split(","))));
            // eventually remove support for it.
            if (siteSet.remove(LOCAL)) {
                LOGGER.debug("Found 'local' alias, replacing with {}.", SystemInfo.getSiteName());
                siteSet.add(SystemInfo.getSiteName());
            }
            if (siteSet.contains(framework.getId()) && siteSet.size() == 1) {
                LOGGER.debug("Only local site specified, saving overhead and just performing a local query on " + framework.getId() + ".");
            } else {
                LOGGER.debug("Querying site set: {}", siteSet);
                query.setSiteIds(siteSet);
            }
            query.setIsEnterprise(false);
        } else {
            LOGGER.debug("No sites found, defaulting to enterprise query.");
            query.setIsEnterprise(true);
        }
        // contextual
        if (searchTerms != null && !searchTerms.trim().isEmpty()) {
            try {
                query.addContextualFilter(searchTerms, selector);
            } catch (ParsingException e) {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
        // single temporal criterion per query
        if ((dateStart != null && !dateStart.trim().isEmpty()) || (dateEnd != null && !dateEnd.trim().isEmpty()) || (dateOffset != null && !dateOffset.trim().isEmpty())) {
            query.addTemporalFilter(dateStart, dateEnd, dateOffset);
        }
        // spatial
        // single spatial criterion per query
        addSpatialFilter(query, geometry, polygon, bbox, radius, lat, lon);
        if (type != null && !type.trim().isEmpty()) {
            query.addTypeFilter(type, versions);
        }
        Map<String, Serializable> properties = new HashMap<String, Serializable>();
        for (Object key : request.getParameterMap().keySet()) {
            if (key instanceof String) {
                Object value = request.getParameterMap().get(key);
                if (value instanceof Serializable) {
                    properties.put((String) key, ((String[]) value)[0]);
                }
            }
        }
        response = executeQuery(queryFormat, query, ui, properties);
    } catch (IllegalArgumentException iae) {
        LOGGER.info("Bad input found while executing a query", iae);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags(iae.getMessage())).build();
    } catch (RuntimeException re) {
        LOGGER.info("Exception while executing a query", re);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Exception while executing a query")).build();
    }
    LOGGER.trace("EXITING: {}", methodName);
    return response;
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) Serializable(java.io.Serializable) OpenSearchQuery(org.codice.ddf.opensearch.query.OpenSearchQuery) HashMap(java.util.HashMap) ParsingException(org.parboiled.errors.ParsingException) HashSet(java.util.HashSet) GET(javax.ws.rs.GET)

Example 3 with ParsingException

use of org.parboiled.errors.ParsingException in project ddf by codice.

the class OpenSearchEndpoint method processQuery.

/**
 * @param searchTerms Space-delimited list of search terms.
 * @param maxResults Maximum # of results to return. If count is also specified, the count value
 *     will take precedence over the maxResults value
 * @param sources Comma-delimited list of data sources to query (default: default sources
 *     selected).
 * @param maxTimeout Maximum timeout (msec) for query to respond (default: mt=30000).
 * @param startIndex Index of first result to return. Integer >= 0 (default: start=1).
 * @param count Number of results to retrieve per page (default: count=10).
 * @param geometry WKT Geometries.
 * @param bbox Comma-delimited list of lat/lon (deg) bounding box coordinates (geo format:
 *     geo:bbox ~ West,South,East,North).
 * @param polygon Comma-delimited list of lat/lon (deg) pairs, in clockwise order around the
 *     polygon, with the last point being the same as the first in order to close the polygon.
 * @param lat Latitude in decimal degrees (typical GPS receiver WGS84 coordinates).
 * @param lon Longitude in decimal degrees (typical GPS receiver WGS84 coordinates).
 * @param radius The radius (m) parameter, used with the lat and lon parameters, specifies the
 *     search distance from this point (default: radius=5000).
 * @param dateStart Specifies the beginning of the time slice of the search on the modified time
 *     field (RFC-3339 - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Default value of
 *     "1970-01-01T00:00:00Z" is used when dtend is indicated but dtstart is not specified
 * @param dateEnd Specifies the ending of the time slice of the search on the modified time field
 *     (RFC-3339 - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Current GMT date/time is used
 *     when dtstart is specified but not dtend.
 * @param dateOffset Specifies an offset, backwards from the current time, to search on the
 *     modified time field for entries. Defined in milliseconds.
 * @param sort Specifies sort by field as sort=<sbfield>:<sborder>, where <sbfield> may be 'date'
 *     or 'relevance' (default is 'relevance'). The conditional param <sborder> is optional but
 *     has a value of 'asc' or 'desc' (default is 'desc'). When <sbfield> is 'relevance',
 *     <sborder> must be 'desc'.
 * @param format Defines the format that the return type should be in. (example:atom, html)
 * @param selectors Defines a comma-delimited list of XPath selectors to narrow the query.
 * @param type Specifies the type of data to search for. (example: nitf)
 * @param versions Specifies the versions in a comma-delimited list.
 */
@Override
@GET
public Response processQuery(@QueryParam(OpenSearchConstants.SEARCH_TERMS) String searchTerms, @QueryParam(OpenSearchConstants.MAX_RESULTS) String maxResults, @QueryParam(OpenSearchConstants.SOURCES) String sources, @QueryParam(OpenSearchConstants.MAX_TIMEOUT) String maxTimeout, @QueryParam(OpenSearchConstants.START_INDEX) String startIndex, @QueryParam(OpenSearchConstants.COUNT) String count, @QueryParam(OpenSearchConstants.GEOMETRY) String geometry, @QueryParam(OpenSearchConstants.BBOX) String bbox, @QueryParam(OpenSearchConstants.POLYGON) String polygon, @QueryParam(OpenSearchConstants.LAT) String lat, @QueryParam(OpenSearchConstants.LON) String lon, @QueryParam(OpenSearchConstants.RADIUS) String radius, @QueryParam(OpenSearchConstants.DATE_START) String dateStart, @QueryParam(OpenSearchConstants.DATE_END) String dateEnd, @QueryParam(OpenSearchConstants.DATE_OFFSET) String dateOffset, @QueryParam(OpenSearchConstants.SORT) String sort, @QueryParam(OpenSearchConstants.FORMAT) String format, @QueryParam(OpenSearchConstants.SELECTORS) String selectors, @Context UriInfo ui, @QueryParam(OpenSearchConstants.TYPE) String type, @QueryParam(OpenSearchConstants.VERSIONS) String versions, @Context HttpServletRequest request) {
    Response response;
    LOGGER.trace("request url: {}", ui.getRequestUri());
    try {
        OpenSearchQuery query = createNewQuery(startIndex, count, maxResults, sort, maxTimeout);
        if (StringUtils.isNotEmpty(sources)) {
            LOGGER.trace("Received site names from client.");
            final Set<String> siteSet = SOURCES_PATTERN.splitAsStream(sources).collect(Collectors.toSet());
            // eventually remove support for it.
            if (siteSet.remove(OpenSearchConstants.LOCAL_SOURCE)) {
                LOGGER.trace("Found 'local' alias, replacing with {}.", SystemInfo.getSiteName());
                siteSet.add(SystemInfo.getSiteName());
            }
            if (siteSet.contains(framework.getId()) && siteSet.size() == 1) {
                LOGGER.trace("Only local site specified, saving overhead and just performing a local query on Id : {} .", framework.getId());
            } else {
                LOGGER.trace("Querying site set: {}", siteSet);
                query.setSiteIds(siteSet);
            }
            query.setIsEnterprise(false);
        } else {
            LOGGER.trace("No sites found, defaulting to enterprise query.");
            query.setIsEnterprise(true);
        }
        // contextual
        if (StringUtils.isNotBlank(searchTerms)) {
            query.addContextualFilter(searchTerms.trim(), selectors);
        }
        if (StringUtils.isNotBlank(dateStart) || StringUtils.isNotBlank(dateEnd)) {
            // If either start date OR end date is specified and non-empty, then a temporal filter can
            // be created
            query.addStartEndTemporalFilter(dateStart, dateEnd);
        } else if (StringUtils.isNotBlank(dateOffset)) {
            query.addOffsetTemporalFilter(dateOffset);
        }
        // spatial
        // single spatial criterion per query
        addSpatialFilter(query, geometry, polygon, bbox, radius, lat, lon);
        if (StringUtils.isNotBlank(type)) {
            query.addTypeFilter(type.trim(), versions);
        }
        Map<String, Serializable> properties = new HashMap<>();
        for (Object key : request.getParameterMap().keySet()) {
            if (key instanceof String) {
                Object value = request.getParameterMap().get(key);
                if (value != null) {
                    properties.put((String) key, ((String[]) value)[0]);
                }
            }
        }
        response = executeQuery(format, query, ui, properties);
    } catch (ParsingException e) {
        LOGGER.debug("Bad input found while executing a query", e);
        response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags(e.getMessage())).build();
    } catch (RuntimeException re) {
        LOGGER.debug("Exception while executing a query", re);
        response = Response.serverError().entity(wrapStringInPreformattedTags("Exception while executing a query")).build();
    }
    return response;
}
Also used : QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) Serializable(java.io.Serializable) OpenSearchQuery(org.codice.ddf.opensearch.endpoint.query.OpenSearchQuery) HashMap(java.util.HashMap) ParsingException(org.parboiled.errors.ParsingException) GET(javax.ws.rs.GET)

Example 4 with ParsingException

use of org.parboiled.errors.ParsingException in project ddf by codice.

the class OpenSearchQuery method addContextualFilter.

public void addContextualFilter(String searchTerms, String selectors) throws ParsingException {
    Filter filter = null;
    KeywordFilterGenerator keywordFilterGenerator = new KeywordFilterGenerator(filterBuilder);
    KeywordTextParser parser = Parboiled.createParser(KeywordTextParser.class);
    // translate the search terms into an abstract syntax tree
    ParsingResult<ASTNode> result = new RecoveringParseRunner(parser.inputPhrase()).run(searchTerms);
    // make sure it's a good result before using it
    if (result.matched && !result.hasErrors()) {
        filter = generateContextualFilter(selectors, keywordFilterGenerator, result);
    } else if (result.hasErrors()) {
        throw new ParsingException("Unable to parse keyword search phrase. " + generateParsingError(result));
    }
    if (filter != null) {
        filters.add(filter);
    }
}
Also used : KeywordFilterGenerator(org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator) PolygonSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.PolygonSpatialFilter) SpatialFilter(ddf.catalog.impl.filter.SpatialFilter) TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) SpatialDistanceFilter(ddf.catalog.impl.filter.SpatialDistanceFilter) Filter(org.opengis.filter.Filter) BBoxSpatialFilter(org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter) ParsingException(org.parboiled.errors.ParsingException) ASTNode(org.codice.ddf.opensearch.endpoint.ASTNode) RecoveringParseRunner(org.parboiled.parserunners.RecoveringParseRunner) KeywordTextParser(org.codice.ddf.opensearch.endpoint.KeywordTextParser)

Aggregations

ParsingException (org.parboiled.errors.ParsingException)4 SpatialDistanceFilter (ddf.catalog.impl.filter.SpatialDistanceFilter)2 SpatialFilter (ddf.catalog.impl.filter.SpatialFilter)2 TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Response (javax.ws.rs.core.Response)2 Filter (org.opengis.filter.Filter)2 RecoveringParseRunner (org.parboiled.parserunners.RecoveringParseRunner)2 HashSet (java.util.HashSet)1 ASTNode (org.codice.ddf.endpoints.ASTNode)1 KeywordFilterGenerator (org.codice.ddf.endpoints.KeywordFilterGenerator)1 KeywordTextParser (org.codice.ddf.endpoints.KeywordTextParser)1 ASTNode (org.codice.ddf.opensearch.endpoint.ASTNode)1 KeywordFilterGenerator (org.codice.ddf.opensearch.endpoint.KeywordFilterGenerator)1 KeywordTextParser (org.codice.ddf.opensearch.endpoint.KeywordTextParser)1 OpenSearchQuery (org.codice.ddf.opensearch.endpoint.query.OpenSearchQuery)1 BBoxSpatialFilter (org.codice.ddf.opensearch.endpoint.query.filter.BBoxSpatialFilter)1