You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by yo...@apache.org on 2007/02/18 04:33:14 UTC
svn commit: r508859 - in /lucene/solr/trunk: ./
src/java/org/apache/solr/core/ src/webapp/WEB-INF/
src/webapp/src/org/apache/solr/servlet/
Author: yonik
Date: Sat Feb 17 19:33:13 2007
New Revision: 508859
URL: http://svn.apache.org/viewvc?view=rev&rev=508859
Log:
make solr more easily embeddable: SOLR-149
Modified:
lucene/solr/trunk/CHANGES.txt
lucene/solr/trunk/src/java/org/apache/solr/core/Config.java
lucene/solr/trunk/src/java/org/apache/solr/core/SolrCore.java
lucene/solr/trunk/src/webapp/WEB-INF/web.xml
lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java
lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java
Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Sat Feb 17 19:33:13 2007
@@ -103,6 +103,10 @@
configuration files loaded, including schema.xml and solrconfig.xml.
(Erik Hatcher with inspiration from Andrew Saar)
+12. SOLR-149: Changes to make Solr more easily embeddable, in addition
+ to logging which request handler handled each request.
+ (Ryan McKinley via yonik)
+
Changes in runtime behavior
1. Highlighting using DisMax will only pick up terms from the main
user query, not boost or filter queries (klaas).
Modified: lucene/solr/trunk/src/java/org/apache/solr/core/Config.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/core/Config.java?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/core/Config.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/core/Config.java Sat Feb 17 19:33:13 2007
@@ -248,6 +248,11 @@
}
return instanceDir;
}
+
+ public static boolean isInstanceDirInitalized()
+ {
+ return instanceDir != null;
+ }
// The directory where solr will look for config files by default.
// defaults to "./solr/conf/"
Modified: lucene/solr/trunk/src/java/org/apache/solr/core/SolrCore.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/core/SolrCore.java?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/core/SolrCore.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/core/SolrCore.java Sat Feb 17 19:33:13 2007
@@ -608,7 +608,8 @@
handler.handleRequest(req,rsp);
setResponseHeaderValues(responseHeader,req,rsp);
- log.info(req.getParamString()+ " 0 "+
+ log.info(req.getContext().get("path") + " "
+ + req.getParamString()+ " 0 "+
(int)(rsp.getEndTime() - req.getStartTime()));
}
Modified: lucene/solr/trunk/src/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/webapp/WEB-INF/web.xml?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/src/webapp/WEB-INF/web.xml (original)
+++ lucene/solr/trunk/src/webapp/WEB-INF/web.xml Sat Feb 17 19:33:13 2007
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
+<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
@@ -34,6 +34,16 @@
<filter>
<filter-name>SolrRequestFilter</filter-name>
<filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>
+ <!--
+ <init-param>
+ <param-name>handle-select</param-name>
+ <param-value>true</param-value>
+ </init-param>
+ <init-param>
+ <param-name>path-prefix</param-name>
+ <param-value>/xxx</param-value>
+ </init-param>
+ -->
</filter>
<filter-mapping>
<filter-name>SolrRequestFilter</filter-name>
Modified: lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java (original)
+++ lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java Sat Feb 17 19:33:13 2007
@@ -40,6 +40,7 @@
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException;
import org.apache.solr.request.QueryResponseWriter;
+import org.apache.solr.request.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.SolrRequestHandler;
@@ -53,31 +54,30 @@
protected SolrCore core;
protected SolrRequestParsers parsers;
+ protected boolean handleSelect = false;
+ protected String pathPrefix = null; // strip this from the begging of a path
public void init(FilterConfig config) throws ServletException
{
log.info("SolrDispatchFilter.init()");
- try {
- Context c = new InitialContext();
-
- /***
- System.out.println("Enumerating JNDI Context=" + c);
- NamingEnumeration<NameClassPair> en = c.list("java:comp/env");
- while (en.hasMore()) {
- NameClassPair ncp = en.next();
- System.out.println(" ENTRY:" + ncp);
+
+ // Only initalize the directory if it has not been done yet
+ if( !Config.isInstanceDirInitalized() ) {
+ try {
+ Context c = new InitialContext();
+ String home = (String)c.lookup("java:comp/env/solr/home");
+ if (home!=null) Config.setInstanceDir(home);
+ } catch (NoInitialContextException e) {
+ log.info("JNDI not configured for Solr (NoInitialContextEx)");
+ } catch (NamingException e) {
+ log.info("No /solr/home in JNDI");
}
- System.out.println("JNDI lookup=" + c.lookup("java:comp/env/solr/home"));
- ***/
-
- String home = (String)c.lookup("java:comp/env/solr/home");
- if (home!=null) Config.setInstanceDir(home);
- } catch (NoInitialContextException e) {
- log.info("JNDI not configured for Solr (NoInitialContextEx)");
- } catch (NamingException e) {
- log.info("No /solr/home in JNDI");
}
-
+
+ // web.xml configuration
+ this.pathPrefix = config.getInitParameter( "path-prefix" );
+ this.handleSelect = "true".equals( config.getInitParameter( "handle-select" ) );
+
log.info("user.dir=" + System.getProperty("user.dir"));
core = SolrCore.getSolrCore();
parsers = new SolrRequestParsers( core, SolrConfig.config );
@@ -99,17 +99,34 @@
// 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 );
}
+ SolrQueryRequest solrReq = null;
SolrRequestHandler 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 );
+ handler = core.getRequestHandler( qt );
+ if( handler == null ) {
+ throw new SolrException( 400, "unknown handler: "+qt);
+ }
+ }
+ }
if( handler != null ) {
- SolrQueryRequest solrReq = parsers.parse( path, req );
+ if( solrReq == null ) {
+ solrReq = parsers.parse( path, req );
+ }
SolrQueryResponse solrRsp = new SolrQueryResponse();
- core.execute( handler, solrReq, solrRsp );
+ this.execute( req, handler, solrReq, solrRsp );
if( solrRsp.getException() != null ) {
sendError( (HttpServletResponse)response, solrRsp.getException() );
return;
@@ -132,6 +149,12 @@
// 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 );
+ }
protected void sendError(HttpServletResponse res, Throwable ex) throws IOException
{
@@ -156,5 +179,50 @@
}
}
res.sendError( code, ex.getMessage() + trace );
+ }
+
+ //---------------------------------------------------------------------
+ //---------------------------------------------------------------------
+
+ /**
+ * 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
+ *
+ */
+ public boolean isHandleSelect() {
+ return handleSelect;
+ }
+
+ public void setHandleSelect(boolean handleSelect) {
+ this.handleSelect = handleSelect;
+ }
+
+ /**
+ * 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:
+ *
+ * <filter-mapping>
+ * <filter-name>SolrRequestFilter</filter-name>
+ * <url-pattern>/xxx/*</url-pattern>
+ * </filter-mapping>
+ *
+ * Make sure to set the PathPrefix to "/xxx" either with this function
+ * or in web.xml
+ *
+ * <init-param>
+ * <param-name>path-prefix</param-name>
+ * <param-value>/xxx</param-value>
+ * </init-param>
+ *
+ */
+ public void setPathPrefix(String pathPrefix) {
+ this.pathPrefix = pathPrefix;
+ }
+
+ public String getPathPrefix() {
+ return pathPrefix;
}
}
Modified: lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java?view=diff&rev=508859&r1=508858&r2=508859
==============================================================================
--- lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java (original)
+++ lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java Sat Feb 17 19:33:13 2007
@@ -109,11 +109,9 @@
SolrParams params = parser.parseParamsAndFillStreams( req, streams );
SolrQueryRequest sreq = buildRequestFrom( params, streams );
- // If there is some path left over, add it to the context
- int idx = req.getServletPath().indexOf( ':' );
- if( idx > 0 ) {
- sreq.getContext().put( "path", req.getServletPath().substring( idx+1 ) );
- }
+ // Handlers and loggin will want to know the path. If it contains a ':'
+ // the handler could use it for RESTfull URLs
+ sreq.getContext().put( "path", path );
return sreq;
}