From c84bcc878a2fff4eeacaab60a2fde5a89477335e Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 23 Jan 2014 19:01:31 +0100 Subject: [PATCH] first try to add a generic solr servlet as luke request servlet --- .../net/yacy/http/Jetty8HttpServerImpl.java | 7 +- .../yacy/http/servlets/SolrSelectServlet.java | 2 +- .../net/yacy/http/servlets/SolrServlet.java | 108 ++++++++++++++++++ source/net/yacy/search/index/Fulltext.java | 7 ++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 source/net/yacy/http/servlets/SolrServlet.java diff --git a/source/net/yacy/http/Jetty8HttpServerImpl.java b/source/net/yacy/http/Jetty8HttpServerImpl.java index c2ba352c1..93b23a6ff 100644 --- a/source/net/yacy/http/Jetty8HttpServerImpl.java +++ b/source/net/yacy/http/Jetty8HttpServerImpl.java @@ -42,11 +42,13 @@ import javax.net.ssl.SSLContext; import net.yacy.cora.util.ConcurrentLog; import net.yacy.http.servlets.GSAsearchServlet; import net.yacy.http.servlets.SolrSelectServlet; +import net.yacy.http.servlets.SolrServlet; import net.yacy.http.servlets.YaCyDefaultServlet; import net.yacy.http.servlets.YaCyProxyServlet; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; import net.yacy.utils.PKCS12Tool; + import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; @@ -133,10 +135,13 @@ public class Jetty8HttpServerImpl implements YaCyHttpServer { //sholder.setInitParameter("welcomeFile", "index.html"); // default is index.html, welcome.html htrootContext.addServlet(sholder,"/*"); - //add SolrServlet + //add SolrSelectServlet htrootContext.addServlet(SolrSelectServlet.class, "/solr/select"); // uses the default core, collection1 htrootContext.addServlet(SolrSelectServlet.class, "/solr/collection1/select"); // the same servlet, identifies the collection1 core using the path htrootContext.addServlet(SolrSelectServlet.class, "/solr/webgraph/select"); // the same servlet, identifies the webgraph core using the path + + htrootContext.addServlet(SolrServlet.class, "/solr/collection1/admin/luke"); + htrootContext.addServlet(SolrServlet.class, "/solr/webgraph/admin/luke"); // add proxy?url= servlet htrootContext.addServlet(YaCyProxyServlet.class,"/proxy.html"); diff --git a/source/net/yacy/http/servlets/SolrSelectServlet.java b/source/net/yacy/http/servlets/SolrSelectServlet.java index 311674764..329f0da41 100644 --- a/source/net/yacy/http/servlets/SolrSelectServlet.java +++ b/source/net/yacy/http/servlets/SolrSelectServlet.java @@ -1,5 +1,5 @@ /** - * SolrServlet + * SolrSelectServlet * Copyright 2012 by Michael Peter Christen * First released 23.08.2012 at http://yacy.net * diff --git a/source/net/yacy/http/servlets/SolrServlet.java b/source/net/yacy/http/servlets/SolrServlet.java new file mode 100644 index 000000000..066f10df4 --- /dev/null +++ b/source/net/yacy/http/servlets/SolrServlet.java @@ -0,0 +1,108 @@ +/** + * SolrServlet + * Copyright 2014 by Michael Peter Christen + * First released 23.01.2014 at http://yacy.net + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program in the file lgpl21.txt + * If not, see . + */ + +package net.yacy.http.servlets; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.Charset; + +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; + +import net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector; +import net.yacy.cora.util.ConcurrentLog; +import net.yacy.search.Switchboard; +import net.yacy.search.schema.CollectionSchema; +import net.yacy.search.schema.WebgraphSchema; + +import org.apache.solr.common.params.MultiMapSolrParams; +import org.apache.solr.common.util.ContentStreamBase; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.request.SolrRequestHandler; +import org.apache.solr.response.BinaryQueryResponseWriter; +import org.apache.solr.response.QueryResponseWriter; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.servlet.SolrRequestParsers; +import org.apache.solr.servlet.cache.Method; +import org.apache.solr.util.FastWriter; + +public class SolrServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + private static final Charset UTF8 = Charset.forName("UTF-8"); + + @Override + public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { + + HttpServletRequest hrequest = (HttpServletRequest) request; + + final Method reqMethod = Method.getMethod(hrequest.getMethod()); + + // get the embedded connector + String requestURI = hrequest.getRequestURI(); + MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString()); + boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME) || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME); + mmsp.getMap().remove("core"); + Switchboard sb = Switchboard.getSwitchboard(); + EmbeddedSolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector() : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME); + if (connector == null) throw new ServletException("no core"); + + SolrQueryResponse solrRsp = new SolrQueryResponse(); + try { + SolrQueryRequest solrReq = connector.request(mmsp); // SolrRequestParsers.DEFAULT.parse(null, hrequest.getServletPath(), hrequest); + solrReq.getContext().put("webapp", hrequest.getContextPath()); + SolrRequestHandler handler = sb.index.fulltext().getEmbeddedInstance().getCoreContainer().getMultiCoreHandler(); + connector.getCore().execute( handler, solrReq, solrRsp ); + + // write response header + QueryResponseWriter responseWriter = connector.getCore().getQueryResponseWriter(solrReq); + + final String ct = responseWriter.getContentType(solrReq, solrRsp); + if (null != ct) response.setContentType(ct); + + if (Method.HEAD != reqMethod) { + if (responseWriter instanceof BinaryQueryResponseWriter) { + BinaryQueryResponseWriter binWriter = (BinaryQueryResponseWriter) responseWriter; + binWriter.write(response.getOutputStream(), solrReq, solrRsp); + } else { + String charset = ContentStreamBase.getCharsetFromContentType(ct); + Writer out = (charset == null || charset.equalsIgnoreCase("UTF-8")) + ? new OutputStreamWriter(response.getOutputStream(), UTF8) + : new OutputStreamWriter(response.getOutputStream(), charset); + out = new FastWriter(out); + responseWriter.write(out, solrReq, solrRsp); + out.flush(); + } + } + + ConcurrentLog.info("luke", solrRsp.getValues().toString()); + + } catch (Exception e) { + ConcurrentLog.logException(e); + } + + } + +} diff --git a/source/net/yacy/search/index/Fulltext.java b/source/net/yacy/search/index/Fulltext.java index 43b4d6cc9..758454451 100644 --- a/source/net/yacy/search/index/Fulltext.java +++ b/source/net/yacy/search/index/Fulltext.java @@ -177,6 +177,13 @@ public final class Fulltext { } } + public EmbeddedInstance getEmbeddedInstance() { + synchronized (this.solrInstances) { + if (this.solrInstances.isConnected0()) return this.solrInstances.getSolr0(); + return null; + } + } + public SolrConnector getDefaultConnector() { synchronized (this.solrInstances) { return this.solrInstances.getDefaultMirrorConnector();