SOLR-2079: Add option to pass HttpServletRequest in the SolrQueryRequest context map

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1470614 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-04-22 16:44:16 +00:00
parent a24f93e728
commit 89b08baab7
5 changed files with 62 additions and 2 deletions

View File

@ -20,6 +20,8 @@ javax.servlet.ServletRequest#getParameterMap()
javax.servlet.ServletRequest#getParameterNames() javax.servlet.ServletRequest#getParameterNames()
javax.servlet.ServletRequest#getParameterValues(java.lang.String) javax.servlet.ServletRequest#getParameterValues(java.lang.String)
javax.servlet.http.HttpServletRequest#getSession() @ Servlet API getter has side effect of creating sessions
@defaultMessage Servlet API method is broken and slow in some environments (e.g., Jetty's UTF-8 readers) @defaultMessage Servlet API method is broken and slow in some environments (e.g., Jetty's UTF-8 readers)
javax.servlet.ServletRequest#getReader() javax.servlet.ServletRequest#getReader()

View File

@ -73,6 +73,9 @@ Other Changes
* SOLR-4737: Update Guava to 14.0.1 (Mark Miller) * SOLR-4737: Update Guava to 14.0.1 (Mark Miller)
* SOLR-2079: Add option to pass HttpServletRequest in the SolrQueryRequest context map.
(Tomás Fernández Löbbe via Robert Muir)
================== 4.3.0 ================== ================== 4.3.0 ==================
Versions of Major Components Versions of Major Components

View File

@ -73,6 +73,7 @@ public class SolrRequestParsers
private final boolean enableRemoteStreams; private final boolean enableRemoteStreams;
private StandardRequestParser standard; private StandardRequestParser standard;
private boolean handleSelect = true; private boolean handleSelect = true;
private boolean addHttpRequestToContext;
/** Default instance for e.g. admin requests. Limits to 2 MB uploads and does not allow remote streams. */ /** Default instance for e.g. admin requests. Limits to 2 MB uploads and does not allow remote streams. */
public static final SolrRequestParsers DEFAULT = new SolrRequestParsers(); public static final SolrRequestParsers DEFAULT = new SolrRequestParsers();
@ -87,6 +88,7 @@ public class SolrRequestParsers
multipartUploadLimitKB = formUploadLimitKB = Integer.MAX_VALUE; multipartUploadLimitKB = formUploadLimitKB = Integer.MAX_VALUE;
enableRemoteStreams = true; enableRemoteStreams = true;
handleSelect = true; handleSelect = true;
addHttpRequestToContext = false;
} else { } else {
multipartUploadLimitKB = globalConfig.getInt( multipartUploadLimitKB = globalConfig.getInt(
"requestDispatcher/requestParsers/@multipartUploadLimitInKB", 2048 ); "requestDispatcher/requestParsers/@multipartUploadLimitInKB", 2048 );
@ -100,6 +102,9 @@ public class SolrRequestParsers
// Let this filter take care of /select?xxx format // Let this filter take care of /select?xxx format
handleSelect = globalConfig.getBool( handleSelect = globalConfig.getBool(
"requestDispatcher/@handleSelect", true ); "requestDispatcher/@handleSelect", true );
addHttpRequestToContext = globalConfig.getBool(
"requestDispatcher/requestParsers/@addHttpRequestToContext", false );
} }
init(multipartUploadLimitKB, formUploadLimitKB); init(multipartUploadLimitKB, formUploadLimitKB);
} }
@ -107,6 +112,7 @@ public class SolrRequestParsers
private SolrRequestParsers() { private SolrRequestParsers() {
enableRemoteStreams = false; enableRemoteStreams = false;
handleSelect = false; handleSelect = false;
addHttpRequestToContext = false;
init(2048, 2048); init(2048, 2048);
} }
@ -140,6 +146,10 @@ public class SolrRequestParsers
// Handlers and login will want to know the path. If it contains a ':' // Handlers and login will want to know the path. If it contains a ':'
// the handler could use it for RESTful URLs // the handler could use it for RESTful URLs
sreq.getContext().put( "path", path ); sreq.getContext().put( "path", path );
if(addHttpRequestToContext) {
sreq.getContext().put("httpRequest", req);
}
return sreq; return sreq;
} }
@ -343,6 +353,14 @@ public class SolrRequestParsers
public void setHandleSelect(boolean handleSelect) { public void setHandleSelect(boolean handleSelect) {
this.handleSelect = handleSelect; this.handleSelect = handleSelect;
} }
public boolean isAddRequestHeadersToContext() {
return addHttpRequestToContext;
}
public void setAddRequestHeadersToContext(boolean addRequestHeadersToContext) {
this.addHttpRequestToContext = addRequestHeadersToContext;
}
} }
//----------------------------------------------------------------- //-----------------------------------------------------------------

View File

@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -314,4 +315,32 @@ public class SolrRequestParserTest extends SolrTestCaseJ4 {
assertEquals(500, solre.code()); assertEquals(500, solre.code());
} }
} }
@Test
public void testAddHttpRequestToContext() throws Exception {
HttpServletRequest request = createMock(HttpServletRequest.class);
expect(request.getMethod()).andReturn("GET").anyTimes();
expect(request.getContentType()).andReturn( "application/x-www-form-urlencoded" ).anyTimes();
expect(request.getQueryString()).andReturn("q=title:solr").anyTimes();
Map<String, String> headers = new HashMap<String,String>();
headers.put("X-Forwarded-For", "10.0.0.1");
expect(request.getHeaderNames()).andReturn(new Vector<String>(headers.keySet()).elements()).anyTimes();
for(Map.Entry<String,String> entry:headers.entrySet()) {
Vector<String> v = new Vector<String>();
v.add(entry.getValue());
expect(request.getHeaders(entry.getKey())).andReturn(v.elements()).anyTimes();
}
replay(request);
SolrRequestParsers parsers = new SolrRequestParsers(h.getCore().getSolrConfig());
assertFalse(parsers.isAddRequestHeadersToContext());
SolrQueryRequest solrReq = parsers.parse(h.getCore(), "/select", request);
assertFalse(solrReq.getContext().containsKey("httpRequest"));
parsers.setAddRequestHeadersToContext(true);
solrReq = parsers.parse(h.getCore(), "/select", request);
assertEquals(request, solrReq.getContext().get("httpRequest"));
assertEquals("10.0.0.1", ((HttpServletRequest)solrReq.getContext().get("httpRequest")).getHeaders("X-Forwarded-For").nextElement());
}
} }

View File

@ -700,6 +700,13 @@
POST. You can use POST to pass request parameters not POST. You can use POST to pass request parameters not
fitting into the URL. fitting into the URL.
addHttpRequestToContext - if set to true, it will instruct
the requestParsers to include the original HttpServletRequest
object in the context map of the SolrQueryRequest under the
key "httpRequest". It will not be used by any of the existing
Solr components, but may be useful when developing custom
plugins.
*** WARNING *** *** WARNING ***
The settings below authorize Solr to fetch remote files, You The settings below authorize Solr to fetch remote files, You
should make sure your system has some authentication before should make sure your system has some authentication before
@ -708,7 +715,8 @@
--> -->
<requestParsers enableRemoteStreaming="true" <requestParsers enableRemoteStreaming="true"
multipartUploadLimitInKB="2048000" multipartUploadLimitInKB="2048000"
formdataUploadLimitInKB="2048"/> formdataUploadLimitInKB="2048"
addHttpRequestToContext="false"/>
<!-- HTTP Caching <!-- HTTP Caching