Apache Lucene open-source search software
SOLR-7547: Short circuit SolrDispatchFilter for static content request. Right now it creates a new HttpSolrCall object and tries to process it.
Anshum Guptad0251d9b1064
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex c0ee6ec1e0a..4040a0704e6 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -270,6 +270,9 @@ Optimizations* SOLR-7324: IndexFetcher does not need to call isIndexStale if full copy is already needed(Stephan Lagraulet via Varun Thacker)+* SOLR-7547: Short circuit SolrDisptachFilter for static content request. Right now it creates+ a new HttpSolrCall object and tries to process it. (Anshum Gupta)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.javaindex 022dd55f014..00e6b102f34 100644--- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java+++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java@@ -400,13 +400,7 @@ public class HttpSolrCall {1. Authorization is enabled, and2. The requested resource is not a known static file*/- // TODO: There should be a better way to ignore the static files.- if (cores.getAuthorizationPlugin() != null &&- !(req.getRequestURI().endsWith(".html")- || req.getRequestURI().endsWith(".png")- || req.getRequestURI().endsWith(".ico")- || req.getRequestURI().endsWith(".css")- )) {+ if (cores.getAuthorizationPlugin() != null) {AuthorizationContext context = getAuthCtx();log.info(context.toString());AuthorizationResponse authResponse = cores.getAuthorizationPlugin().authorize(context);diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.javaindex 03b3b191357..9aeea9667bc 100644--- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java+++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java@@ -17,10 +17,6 @@package org.apache.solr.servlet;-import java.io.ByteArrayInputStream;-import java.io.IOException;-import java.util.Properties;-import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;@@ -28,6 +24,12 @@ import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;+import java.io.ByteArrayInputStream;+import java.io.IOException;+import java.util.ArrayList;+import java.util.Properties;+import java.util.regex.Matcher;+import java.util.regex.Pattern;import org.apache.commons.io.IOUtils;import org.apache.commons.lang.StringUtils;@@ -57,6 +59,7 @@ public class SolrDispatchFilter extends BaseSolrFilter {protected String abortErrorMessage = null;protected final CloseableHttpClient httpClient = HttpClientUtil.createClient(new ModifiableSolrParams());+ private ArrayList<Pattern> excludePatterns;/*** Enum to define action that needs to be processed.@@ -81,7 +84,14 @@ public class SolrDispatchFilter extends BaseSolrFilter {public void init(FilterConfig config) throws ServletException{log.info("SolrDispatchFilter.init()" + this.getClass().getClassLoader());-+ String exclude = config.getInitParameter("excludePatterns");+ if(exclude != null) {+ String[] excludeArray = exclude.split(",");+ excludePatterns = new ArrayList();+ for (String element : excludeArray) {+ excludePatterns.add(Pattern.compile(element));+ }+ }try {Properties extraProperties = (Properties) config.getServletContext().getAttribute(PROPERTIES_ATTRIBUTE);if (extraProperties == null)@@ -170,6 +180,19 @@ public class SolrDispatchFilter extends BaseSolrFilter {public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain, boolean retry) throws IOException, ServletException {if (!(request instanceof HttpServletRequest)) return;++ // No need to even create the HttpSolrCall object if this path is excluded.+ if(excludePatterns != null) {+ String servletPath = ((HttpServletRequest) request).getServletPath().toString();+ for (Pattern p : excludePatterns) {+ Matcher matcher = p.matcher(servletPath);+ if (matcher.lookingAt()) {+ chain.doFilter(request, response);+ return;+ }+ }+ }+HttpSolrCall call = new HttpSolrCall(this, cores, (HttpServletRequest) request, (HttpServletResponse) response, retry);try {Action result = call.call();diff --git a/solr/webapp/web/WEB-INF/web.xml b/solr/webapp/web/WEB-INF/web.xmlindex 841aed3bbac..0a11fff8f0e 100644--- a/solr/webapp/web/WEB-INF/web.xml+++ b/solr/webapp/web/WEB-INF/web.xml@@ -49,27 +49,15 @@<filter><filter-name>SolrRequestFilter</filter-name><filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>- <!-- If you are wiring Solr into a larger web application which controls- the web context root, you will probably want to mount Solr under- a path prefix (app.war with /app/solr mounted into it, for example).- You will need to put this prefix in front of the SolrDispatchFilter- url-pattern mapping too (/solr/*), and also on any paths for- legacy Solr servlet mappings you may be using.- For the Admin UI to work properly in a path-prefixed configuration,- the admin folder containing the resources needs to be under the app context root- named to match the path-prefix. For example:-- .war- xxx- js- main.js- --><!--+ Exclude patterns is a list of directories that would be short circuited by the+ SolrDispatchFilter. It includes all Admin UI related static content.+ NOTE: It is NOT a pattern but only matches the start of the HTTP ServletPath.+ --><init-param>- <param-name>path-prefix</param-name>- <param-value>/xxx</param-value>+ <param-name>excludePatterns</param-name>+ <param-value>/css/*,/js/*,/img/*,/tpl/*</param-value></init-param>- --></filter><filter-mapping>