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#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)
javax.servlet.ServletRequest#getReader()

View File

@ -73,6 +73,9 @@ Other Changes
* 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 ==================
Versions of Major Components

View File

@ -73,7 +73,8 @@ public class SolrRequestParsers
private final boolean enableRemoteStreams;
private StandardRequestParser standard;
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. */
public static final SolrRequestParsers DEFAULT = new SolrRequestParsers();
@ -87,6 +88,7 @@ public class SolrRequestParsers
multipartUploadLimitKB = formUploadLimitKB = Integer.MAX_VALUE;
enableRemoteStreams = true;
handleSelect = true;
addHttpRequestToContext = false;
} else {
multipartUploadLimitKB = globalConfig.getInt(
"requestDispatcher/requestParsers/@multipartUploadLimitInKB", 2048 );
@ -100,6 +102,9 @@ public class SolrRequestParsers
// Let this filter take care of /select?xxx format
handleSelect = globalConfig.getBool(
"requestDispatcher/@handleSelect", true );
addHttpRequestToContext = globalConfig.getBool(
"requestDispatcher/requestParsers/@addHttpRequestToContext", false );
}
init(multipartUploadLimitKB, formUploadLimitKB);
}
@ -107,6 +112,7 @@ public class SolrRequestParsers
private SolrRequestParsers() {
enableRemoteStreams = false;
handleSelect = false;
addHttpRequestToContext = false;
init(2048, 2048);
}
@ -140,6 +146,10 @@ public class SolrRequestParsers
// Handlers and login will want to know the path. If it contains a ':'
// the handler could use it for RESTful URLs
sreq.getContext().put( "path", path );
if(addHttpRequestToContext) {
sreq.getContext().put("httpRequest", req);
}
return sreq;
}
@ -343,6 +353,14 @@ public class SolrRequestParsers
public void setHandleSelect(boolean 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.List;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
@ -314,4 +315,32 @@ public class SolrRequestParserTest extends SolrTestCaseJ4 {
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
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 ***
The settings below authorize Solr to fetch remote files, You
should make sure your system has some authentication before
@ -708,7 +715,8 @@
-->
<requestParsers enableRemoteStreaming="true"
multipartUploadLimitInKB="2048000"
formdataUploadLimitInKB="2048"/>
formdataUploadLimitInKB="2048"
addHttpRequestToContext="false"/>
<!-- HTTP Caching