Version 1.7.4
[gae.git] / java / src / main / com / google / appengine / api / search / checkers / QueryChecker.java
blobf17cfec5beed96edab9cc91e62ab243098ebbbac
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search.checkers;
5 import com.google.appengine.api.search.SearchQueryException;
6 import com.google.appengine.api.search.SearchServicePb.SearchParams;
7 import com.google.appengine.api.search.query.QueryTreeBuilder;
8 import com.google.apphosting.api.AppEngineInternal;
10 import org.antlr.runtime.RecognitionException;
12 import java.io.UnsupportedEncodingException;
14 /**
15 * Checks values of {@link com.google.appengine.api.search.Query}.
18 @AppEngineInternal
19 public final class QueryChecker {
20 /**
21 * The maximum length of a query string.
22 * @deprecated From 1.7.4, use {@link SearchApiLimits#MAXIMUM_QUERY_LENGTH}
24 @Deprecated public static final int MAXIMUM_QUERY_LENGTH = 2000;
26 /**
27 * Checks that query is not null and is parsable.
28 * @param query the query to check
29 * @return the checked query
30 * @throws SearchQueryException if the query is not parsable
31 * @throws IllegalArgumentException if the query is too long
33 public static String checkQuery(String query) {
34 checkQueryFast(query);
35 return checkQueryParses(query);
38 /**
39 * Checks if the given query parses. This method assumes that the
40 * other checks, such as length, non-null, etc., were already
41 * performed.
43 * @param query the query to check
44 * @return the checked query
45 * @throws SearchQueryException if the query is not parsable
47 private static String checkQueryParses(String query) {
48 try {
49 new QueryTreeBuilder().parse(query);
50 } catch (RecognitionException e) {
51 throw new SearchQueryException("Unable to parse query: " + query);
53 return query;
56 /**
57 * Checks the search specification is valid, specifically, has a valid
58 * index specification, a non-null query, a non-null number of documents
59 * to return specification, a valid cursor if present, valid sort
60 * specification list, a valid collection of field names for sorting,
61 * and a valid scorer specification.
63 * @param params the SearchParams to check
64 * @return this checked SearchParams
65 * @throws IllegalArgumentException if some part of the specification is
66 * invalid
67 * @throws SearchQueryException if the query is unparsable
69 public static SearchParams checkValid(SearchParams params) {
70 checkValidFast(params);
71 checkQueryParses(params.getQuery());
72 return params;
75 /**
76 * Performs a fast check of the search parameters. This method does not
77 * check if the query stored in the parameters parses.
79 * @param params the search parameters to check
80 * @return this checked SearchParams
81 * @throws IllegalArgumentException if parameters are not valid
83 public static SearchParams checkValidFast(SearchParams params) {
84 IndexChecker.checkName(params.getIndexSpec().getName());
85 checkQueryFast(params.getQuery());
86 QueryOptionsChecker.checkValid(params);
87 return params;
90 /**
91 * Checks if the query is not null, and not overly long. This method
92 * does not check if the query is parsable.
94 * @param query the query to check
95 * @return the checked query
97 private static String checkQueryFast(String query) {
98 Preconditions.checkNotNull(query, "query cannot be null");
99 int length;
100 try {
101 length = query.getBytes("UTF-8").length;
102 } catch (UnsupportedEncodingException e){
103 throw new IllegalArgumentException("Unsupported encoding UTF-8. Shouldn't happen, ever!");
105 Preconditions.checkArgument(
106 length <= SearchApiLimits.MAXIMUM_QUERY_LENGTH,
107 String.format("query string must not be longer than %d bytes, was %d", MAXIMUM_QUERY_LENGTH,
108 length));
109 return query;