| Method from org.apache.solr.servlet.SolrDispatchFilter Detail: |
public void destroy() {
core.close();
}
|
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if( abortErrorMessage != null ) {
((HttpServletResponse)response).sendError( 500, abortErrorMessage );
return;
}
if( request instanceof HttpServletRequest) {
SolrQueryRequest solrReq = null;
HttpServletRequest req = (HttpServletRequest)request;
try {
String path = req.getServletPath();
if( req.getPathInfo() != null ) {
// this lets you handle /update/commit when /update is a servlet
path += req.getPathInfo();
}
if( pathPrefix != null && path.startsWith( pathPrefix ) ) {
path = path.substring( pathPrefix.length() );
}
int idx = path.indexOf( ':" );
if( idx > 0 ) {
// save the portion after the ':' for a 'handler' path parameter
path = path.substring( 0, idx );
}
SolrRequestHandler handler = null;
if( path.length() > 1 ) { // don't match "" or "/" as valid path
handler = core.getRequestHandler( path );
}
if( handler == null && handleSelect ) {
if( "/select".equals( path ) || "/select/".equals( path ) ) {
solrReq = parsers.parse( path, req );
String qt = solrReq.getParams().get( SolrParams.QT );
if( qt != null && qt.startsWith( "/" ) ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Invalid query type. Do not use /select to access: "+qt);
}
handler = core.getRequestHandler( qt );
if( handler == null ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+qt);
}
}
}
if( handler != null ) {
if( solrReq == null ) {
solrReq = parsers.parse( path, req );
}
SolrQueryResponse solrRsp = new SolrQueryResponse();
this.execute( req, handler, solrReq, solrRsp );
if( solrRsp.getException() != null ) {
sendError( (HttpServletResponse)response, solrRsp.getException() );
return;
}
// Now write it out
QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
response.setContentType(responseWriter.getContentType(solrReq, solrRsp));
PrintWriter out = response.getWriter();
responseWriter.write(out, solrReq, solrRsp);
return;
}
}
catch( Throwable ex ) {
sendError( (HttpServletResponse)response, ex );
return;
}
finally {
if( solrReq != null ) {
solrReq.close();
}
}
}
// Otherwise let the webapp handle the request
chain.doFilter(request, response);
}
|
protected void execute(HttpServletRequest req,
SolrRequestHandler handler,
SolrQueryRequest sreq,
SolrQueryResponse rsp) {
// a custom filter could add more stuff to the request before passing it on.
// for example: sreq.getContext().put( "HttpServletRequest", req );
core.execute( handler, sreq, rsp );
}
|
public String getPathPrefix() {
return pathPrefix;
}
|
public void init(FilterConfig config) throws ServletException {
log.info("SolrDispatchFilter.init()");
try {
// web.xml configuration
this.pathPrefix = config.getInitParameter( "path-prefix" );
// Let this filter take care of /select?xxx format
this.handleSelect =
SolrConfig.config.getBool( "requestDispatcher/@handleSelect", false );
log.info("user.dir=" + System.getProperty("user.dir"));
core = SolrCore.getSolrCore();
parsers = new SolrRequestParsers( core, SolrConfig.config );
}
catch( Throwable t ) {
// catch this so our filter still works
SolrConfig.severeErrors.add( t );
SolrCore.log( t );
}
// Optionally abort if we found a sever error
boolean abortOnConfigurationError = SolrConfig.config.getBool("abortOnConfigurationError",true);
if( abortOnConfigurationError && SolrConfig.severeErrors.size() > 0 ) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter( sw );
out.println( "Severe errors in solr configuration.\n" );
out.println( "Check your log files for more detailed infomation on what may be wrong.\n" );
out.println( "If you want solr to continue after configuration errors, change: \n");
out.println( " < abortOnConfigurationError >false< /abortOnConfigurationError >\n" );
out.println( "in solrconfig.xml\n" );
for( Throwable t : SolrConfig.severeErrors ) {
out.println( "-------------------------------------------------------------" );
t.printStackTrace( out );
}
out.flush();
// Servlet containers behave slightly differntly if you throw an exception durring
// initalization. Resin will display that error for every page, jetty prints it in
// the logs, but continues normally. (We will see a 404 rather then the real error)
// rather then leave the behavior undefined, lets cache the error and spit it out
// for every request.
abortErrorMessage = sw.toString();
//throw new ServletException( abortErrorMessage );
}
log.info("SolrDispatchFilter.init() done");
}
|
public boolean isHandleSelect() {
return handleSelect;
}
Should the filter handle /select even if it is not mapped in solrconfig.xml
This will use consistent error handling for /select?qt=xxx and /update/xml |
protected void sendError(HttpServletResponse res,
Throwable ex) throws IOException {
int code=500;
String trace = "";
if( ex instanceof SolrException ) {
code = ((SolrException)ex).code();
}
// For any regular code, don't include the stack trace
if( code == 500 || code < 100 ) {
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
trace = "\n\n"+sw.toString();
SolrException.logOnce(log,null,ex );
// non standard codes have undefined results with various servers
if( code < 100 ) {
log.warning( "invalid return code: "+code );
code = 500;
}
}
res.sendError( code, ex.getMessage() + trace );
}
|
public void setHandleSelect(boolean handleSelect) {
this.handleSelect = handleSelect;
}
|
public void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}
set the prefix for all paths. This is useful if you want to apply the
filter to something other then *.
For example, if web.xml specifies:
SolrRequestFilter
/xxx/*
Make sure to set the PathPrefix to "/xxx" either with this function
or in web.xml
path-prefix
/xxx
|