change SolrServlet from Filter to Servlet

(as no multicore required)
this allows to simplify context/servlet initialization in Jetty init.
pull/1/head
reger 11 years ago
parent 14c977dd26
commit 19c1a7a5ca

@ -32,19 +32,15 @@ import java.net.InetSocketAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.SocketException; import java.net.SocketException;
import java.security.KeyStore; import java.security.KeyStore;
import java.util.EnumSet;
import java.util.Enumeration; import java.util.Enumeration;
import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.servlet.DispatcherType;
import net.yacy.cora.util.ConcurrentLog; import net.yacy.cora.util.ConcurrentLog;
import net.yacy.http.servlets.GSAsearchServlet; import net.yacy.http.servlets.GSAsearchServlet;
import net.yacy.http.servlets.SolrServlet; import net.yacy.http.servlets.SolrServlet;
import net.yacy.http.servlets.YaCyDefaultServlet; import net.yacy.http.servlets.YaCyDefaultServlet;
import net.yacy.http.servlets.YaCyProxyServlet; import net.yacy.http.servlets.YaCyProxyServlet;
import net.yacy.http.servlets.SolrServlet.Servlet404;
import net.yacy.search.Switchboard; import net.yacy.search.Switchboard;
import net.yacy.utils.PKCS12Tool; import net.yacy.utils.PKCS12Tool;
@ -57,7 +53,6 @@ import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
@ -101,13 +96,6 @@ public class Jetty8HttpServerImpl implements YaCyHttpServer {
YacyDomainHandler domainHandler = new YacyDomainHandler(); YacyDomainHandler domainHandler = new YacyDomainHandler();
domainHandler.setAlternativeResolver(sb.peers); domainHandler.setAlternativeResolver(sb.peers);
//add SolrServlet
ServletContextHandler solrContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
solrContext.setContextPath("/solr");
solrContext.addServlet(new ServletHolder(Servlet404.class),"/*");
solrContext.addFilter(new FilterHolder(SolrServlet.class), "/*", EnumSet.of(DispatcherType.REQUEST));
// configure root context // configure root context
ServletContextHandler htrootContext = new ServletContextHandler(ServletContextHandler.SESSIONS); ServletContextHandler htrootContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
@ -117,13 +105,14 @@ public class Jetty8HttpServerImpl implements YaCyHttpServer {
//sholder.setInitParameter("welcomeFile", "index.html"); // default is index.html, welcome.html //sholder.setInitParameter("welcomeFile", "index.html"); // default is index.html, welcome.html
htrootContext.addServlet(sholder,"/*"); htrootContext.addServlet(sholder,"/*");
//add SolrServlet
htrootContext.addServlet(SolrServlet.class,"/solr/select");
// add proxy?url= servlet // add proxy?url= servlet
ServletHolder proxyholder= new ServletHolder(YaCyProxyServlet.class); htrootContext.addServlet(YaCyProxyServlet.class,"/proxy.html");
htrootContext.addServlet(proxyholder,"/proxy.html");
// add GSA servlet // add GSA servlet
ServletHolder gsaholder = new ServletHolder (GSAsearchServlet.class); htrootContext.addServlet(GSAsearchServlet.class,"/gsa/search");
htrootContext.addServlet(gsaholder,"/gsa/search");
// define list of YaCy specific general handlers // define list of YaCy specific general handlers
HandlerList handlers = new HandlerList(); HandlerList handlers = new HandlerList();
@ -139,7 +128,6 @@ public class Jetty8HttpServerImpl implements YaCyHttpServer {
// logic: 1. YaCy handlers are called if request not handled (e.g. proxy) then servlets handle it // logic: 1. YaCy handlers are called if request not handled (e.g. proxy) then servlets handle it
ContextHandlerCollection allrequesthandlers = new ContextHandlerCollection(); ContextHandlerCollection allrequesthandlers = new ContextHandlerCollection();
allrequesthandlers.addHandler(context); allrequesthandlers.addHandler(context);
allrequesthandlers.addHandler(solrContext);
allrequesthandlers.addHandler(htrootContext); allrequesthandlers.addHandler(htrootContext);
allrequesthandlers.addHandler(new DefaultHandler()); // if not handled by other handler allrequesthandlers.addHandler(new DefaultHandler()); // if not handled by other handler

@ -31,9 +31,6 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
@ -81,8 +78,10 @@ import org.apache.solr.util.FastWriter;
/* /*
* taken from the Solr 3.6.0 code, which is now deprecated; * taken from the Solr 3.6.0 code, which is now deprecated;
* this is now done in Solr 4.x.x with org.apache.solr.servlet.SolrDispatchFilter * this is now done in Solr 4.x.x with org.apache.solr.servlet.SolrDispatchFilter
* implemented as servlet (we don't use multicore)
*/ */
public class SolrServlet implements Filter { public class SolrServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public final static Map<String, QueryResponseWriter> RESPONSE_WRITER = new HashMap<String, QueryResponseWriter>(); public final static Map<String, QueryResponseWriter> RESPONSE_WRITER = new HashMap<String, QueryResponseWriter>();
static { static {
@ -102,50 +101,14 @@ public class SolrServlet implements Filter {
RESPONSE_WRITER.put("gsa", new GSAResponseWriter()); RESPONSE_WRITER.put("gsa", new GSAResponseWriter());
} }
public SolrServlet() {
}
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override @Override
public void destroy() { public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!(request instanceof HttpServletRequest)) {
if (chain != null) chain.doFilter(request, response);
return;
}
HttpServletRequest hrequest = (HttpServletRequest) request; HttpServletRequest hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = (HttpServletResponse) response; HttpServletResponse hresponse = (HttpServletResponse) response;
SolrQueryRequest req = null; SolrQueryRequest req = null;
// check if this servlet was called correctly
String pathInfo = hrequest.getPathInfo();
String path = pathInfo == null ? hrequest.getServletPath() : hrequest.getServletPath() + pathInfo; // should be "/select" after this
if (!EmbeddedSolrConnector.SELECT.equals(path)) {
// this is not for this servlet
if (chain != null) chain.doFilter(request, response);
return;
}
if (!EmbeddedSolrConnector.CONTEXT.equals(hrequest.getContextPath())) {
// this is not for this servlet
if (chain != null) chain.doFilter(request, response);
return;
}
// reject POST which is not supported here
final Method reqMethod = Method.getMethod(hrequest.getMethod()); final Method reqMethod = Method.getMethod(hrequest.getMethod());
if (reqMethod == null || (reqMethod != Method.GET && reqMethod != Method.HEAD)) {
throw new ServletException("Unsupported method: " + hrequest.getMethod());
}
Writer out = null; Writer out = null;
try { try {
@ -263,15 +226,15 @@ public class SolrServlet implements Filter {
if (Method.HEAD == reqMethod) { if (Method.HEAD == reqMethod) {
return; return;
} }
// write response body // write response body
if (responseWriter instanceof BinaryResponseWriter) { if (responseWriter instanceof BinaryResponseWriter) {
((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp); ((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp);
} else { } else {
out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset)); out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset));
responseWriter.write(out, req, rsp); responseWriter.write(out, req, rsp);
out.flush(); out.flush();
} }
} catch (final Throwable ex) { } catch (final Throwable ex) {
sendError(hresponse, ex); sendError(hresponse, ex);

Loading…
Cancel
Save