Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.ImmutableListMultimap; 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.httpd.restapi.RestApiServlet; 21 : import com.google.gerrit.server.documentation.QueryDocumentationExecutor; 22 : import com.google.gerrit.server.documentation.QueryDocumentationExecutor.DocQueryException; 23 : import com.google.gerrit.server.documentation.QueryDocumentationExecutor.DocResult; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Singleton; 26 : import java.io.IOException; 27 : import java.util.List; 28 : import javax.servlet.Filter; 29 : import javax.servlet.FilterChain; 30 : import javax.servlet.FilterConfig; 31 : import javax.servlet.ServletException; 32 : import javax.servlet.ServletRequest; 33 : import javax.servlet.ServletResponse; 34 : import javax.servlet.http.HttpServletRequest; 35 : import javax.servlet.http.HttpServletResponse; 36 : 37 : @Singleton 38 : public class QueryDocumentationFilter implements Filter { 39 99 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 40 : 41 : private final QueryDocumentationExecutor searcher; 42 : 43 : @Inject 44 99 : QueryDocumentationFilter(QueryDocumentationExecutor searcher) { 45 99 : this.searcher = searcher; 46 99 : } 47 : 48 : @Override 49 99 : public void init(FilterConfig filterConfig) {} 50 : 51 : @Override 52 99 : public void destroy() {} 53 : 54 : @Override 55 : public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 56 : throws IOException, ServletException { 57 0 : HttpServletRequest req = (HttpServletRequest) request; 58 0 : if ("GET".equals(req.getMethod()) && !Strings.isNullOrEmpty(req.getParameter("q"))) { 59 0 : HttpServletResponse rsp = (HttpServletResponse) response; 60 : try { 61 0 : List<DocResult> result = searcher.doQuery(request.getParameter("q")); 62 0 : RestApiServlet.replyJson(req, rsp, false, ImmutableListMultimap.of(), result); 63 0 : } catch (DocQueryException e) { 64 0 : logger.atSevere().withCause(e).log("Doc search failed"); 65 0 : rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 66 0 : } 67 0 : } else { 68 0 : chain.doFilter(request, response); 69 : } 70 0 : } 71 : }