Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / checkers / QueryChecker.java
blob87fe3961ed15464fd7d98af6afa95899a3b4c819
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 {
21 /**
22 * Checks that query is not null and is parsable.
23 * @param query the query to check
24 * @return the checked query
25 * @throws SearchQueryException if the query is not parsable
26 * @throws IllegalArgumentException if the query is too long
28 public static String checkQuery(String query) {
29 checkQueryFast(query);
30 return checkQueryParses(query);
33 /**
34 * Checks if the given query parses. This method assumes that the
35 * other checks, such as length, non-null, etc., were already
36 * performed.
38 * @param query the query to check
39 * @return the checked query
40 * @throws SearchQueryException if the query is not parsable
42 private static String checkQueryParses(String query) {
43 try {
44 new QueryTreeBuilder().parse(query);
45 } catch (RecognitionException e) {
46 throw new SearchQueryException("Unable to parse query: " + query);
48 return query;
51 /**
52 * Checks the search specification is valid, specifically, has a valid
53 * index specification, a non-null query, a non-null number of documents
54 * to return specification, a valid cursor if present, valid sort
55 * specification list, a valid collection of field names for sorting,
56 * and a valid scorer specification.
58 * @param params the SearchParams to check
59 * @return this checked SearchParams
60 * @throws IllegalArgumentException if some part of the specification is
61 * invalid
62 * @throws SearchQueryException if the query is unparsable
64 public static SearchParams checkValid(SearchParams params) {
65 checkValidFast(params);
66 checkQueryParses(params.getQuery());
67 return params;
70 /**
71 * Performs a fast check of the search parameters. This method does not
72 * check if the query stored in the parameters parses.
74 * @param params the search parameters to check
75 * @return this checked SearchParams
76 * @throws IllegalArgumentException if parameters are not valid
78 public static SearchParams checkValidFast(SearchParams params) {
79 IndexChecker.checkName(params.getIndexSpec().getName());
80 checkQueryFast(params.getQuery());
81 QueryOptionsChecker.checkValid(params);
82 return params;
85 /**
86 * Checks if the query is not null, and not overly long. This method
87 * does not check if the query is parsable.
89 * @param query the query to check
90 * @return the checked query
92 private static String checkQueryFast(String query) {
93 Preconditions.checkNotNull(query, "query cannot be null");
94 int length;
95 try {
96 length = query.getBytes("UTF-8").length;
97 } catch (UnsupportedEncodingException e){
98 throw new IllegalArgumentException("Unsupported encoding UTF-8. Shouldn't happen, ever!");
100 Preconditions.checkArgument(
101 length <= SearchApiLimits.MAXIMUM_QUERY_LENGTH,
102 String.format("query string must not be longer than %d bytes, was %d",
103 SearchApiLimits.MAXIMUM_QUERY_LENGTH, length));
104 return query;