Apache Solr open-source search software
SOLR-17069: HttpServletRequest; use eagerly to avoid errors (#3324)
David Smiley9ec8b72b1af7
diff --git a/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java b/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.javaindex f3b79c35837..911c24120b0 100644--- a/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java+++ b/solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java@@ -56,7 +56,7 @@ public class CancellableQueryTracker {}}- activeQueriesGenerated.put(queryID, req.getHttpSolrCall().getReq().getQueryString());+ activeQueriesGenerated.put(queryID, req.getParamString());return queryID;}diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.javaindex 7592eed86fc..d5cb3954a81 100644--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java@@ -209,7 +209,7 @@ public class HttpShardHandler extends ShardHandler {req.setMethod(SolrRequest.METHOD.POST);SolrRequestInfo requestInfo = SolrRequestInfo.getRequestInfo();if (requestInfo != null) {- req.setUserPrincipal(requestInfo.getReq().getUserPrincipal());+ req.setUserPrincipal(requestInfo.getUserPrincipal());}return httpShardHandlerFactory.newLBHttpSolrClientReq(req, urls);diff --git a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.javaindex c30d6f2c29e..ce0d8c139e2 100644--- a/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java+++ b/solr/core/src/java/org/apache/solr/request/SolrRequestInfo.java@@ -51,12 +51,12 @@ public class SolrRequestInfo {private SolrQueryRequest req;private SolrQueryResponse rsp;private Date now;- public HttpServletRequest httpRequest;private TimeZone tz;private ResponseBuilder rb;private List<AutoCloseable> closeHooks;private SolrDispatchFilter.Action action;private boolean useServerToken = false;+ private Principal principal;private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());@@ -151,6 +151,7 @@ public class SolrRequestInfo {public SolrRequestInfo(SolrQueryRequest req, SolrQueryResponse rsp) {this.req = req;this.rsp = rsp;+ this.principal = req != null ? req.getUserPrincipal() : null;}public SolrRequestInfo(@@ -160,8 +161,8 @@ public class SolrRequestInfo {}public SolrRequestInfo(HttpServletRequest httpReq, SolrQueryResponse rsp) {- this.httpRequest = httpReq;this.rsp = rsp;+ this.principal = httpReq != null ? httpReq.getUserPrincipal() : null;}public SolrRequestInfo(@@ -171,9 +172,7 @@ public class SolrRequestInfo {}public Principal getUserPrincipal() {- if (req != null) return req.getUserPrincipal();- if (httpRequest != null) return httpRequest.getUserPrincipal();- return null;+ return principal;}public Date getNOW() {diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.javaindex 492ad7e5118..7334a5bc47d 100644--- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java+++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java@@ -50,6 +50,7 @@ import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Objects;+import java.util.Optional;import java.util.Random;import java.util.Set;import java.util.concurrent.TimeUnit;@@ -145,6 +146,8 @@ public class HttpSolrCall {protected final HttpServletRequest req;protected final HttpServletResponse response;protected final boolean retry;+ private final SolrVersion userAgentSolrVersion; // of the client+ private final Span span;protected SolrCore core = null;protected SolrQueryRequest solrReq = null;private boolean mustClearSolrRequestInfo = false;@@ -176,18 +179,30 @@ public class HttpSolrCall {this.response = response;this.retry = retry;this.requestType = RequestType.UNKNOWN;+ this.userAgentSolrVersion = parseUserAgentSolrVersion();+ this.span = Optional.ofNullable(TraceUtils.getSpan(req)).orElse(Span.getInvalid());+ this.path = ServletUtils.getPathAfterContext(req);+req.setAttribute(HttpSolrCall.class.getName(), this);// set a request timer which can be reused by requests if neededreq.setAttribute(SolrRequestParsers.REQUEST_TIMER_SERVLET_ATTRIBUTE, new RTimerTree());// put the core container in request attributereq.setAttribute("org.apache.solr.CoreContainer", cores);- path = ServletUtils.getPathAfterContext(req);}public String getPath() {return path;}+ /**+ * WARNING: This method returns a non-null {@link HttpServletRequest}, but calling certain methods+ * on it — such as {@link HttpServletRequest#getAttribute(String)} — may throw {@link+ * NullPointerException} if accessed outside the original servlet thread (e.g., in asynchronous+ * tasks).+ *+ * <p>Always cache required request data early during request handling if it needs to be accessed+ * later.+ */public HttpServletRequest getReq() {return req;}@@ -639,12 +654,7 @@ public class HttpSolrCall {/** Get the Span for this request. Not null. */public Span getSpan() {- var s = TraceUtils.getSpan(req);- if (s != null) {- return s;- } else {- return Span.getInvalid();- }+ return span;}// called after init().@@ -1174,6 +1184,10 @@ public class HttpSolrCall {* clients prior to 9.9 present themselves as 1.0 or 2.0.*/public SolrVersion getUserAgentSolrVersion() {+ return userAgentSolrVersion;+ }++ private SolrVersion parseUserAgentSolrVersion() {String header = req.getHeader("User-Agent");if (header == null || !header.startsWith("Solr")) {return null;diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java b/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.javaindex 8902acf51a3..d208106c874 100644--- a/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java+++ b/solr/core/src/java/org/apache/solr/servlet/SolrRequestParsers.java@@ -169,7 +169,7 @@ public class SolrRequestParsers {SolrParams params = parser.parseParamsAndFillStreams(req, streams);SolrQueryRequest sreq =- buildRequestFrom(core, params, streams, getRequestTimer(req), req, null);+ buildRequestFrom(core, params, streams, getRequestTimer(req), req, req.getUserPrincipal());// Handlers and login will want to know the path. If it contains a ':'// the handler could use it for RESTful URLs@@ -200,7 +200,7 @@ public class SolrRequestParsers {Collection<ContentStream> streams, // might be added to but caller shouldn't depend on itRTimerTree requestTimer,final HttpServletRequest req,- final Principal principal)+ final Principal principal) // from req, if req was provided, otherwise from elsewherethrows Exception {// ensure streams is non-null and mutable so we can easily add to itif (streams == null) {@@ -267,11 +267,7 @@ public class SolrRequestParsers {new SolrQueryRequestBase(core, params, requestTimer) {@Overridepublic Principal getUserPrincipal() {- if (principal != null) {- return principal;- } else {- return req == null ? null : req.getUserPrincipal();- }+ return principal;}@Overridediff --git a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.javaindex e899c6fbb20..a8c3f7ffe6f 100644--- a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java+++ b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java@@ -308,7 +308,7 @@ public class SolrCmdDistributor implements Closeable {// Copy user principal from the original request to the new update request, for later// authentication interceptor useif (SolrRequestInfo.getRequestInfo() != null) {- req.uReq.setUserPrincipal(SolrRequestInfo.getRequestInfo().getReq().getUserPrincipal());+ req.uReq.setUserPrincipal(SolrRequestInfo.getRequestInfo().getUserPrincipal());}if (req.synchronous) {