implement HtmlResponseWriter to solrServlet (and rss / opensearch responswriter) as in yacy select servlet.

- set contenttype of HTLM/GrepHTML-Reponsewriter to "text/html"
- set a contenttype to GSAsearchServlet
pull/1/head
reger 12 years ago
parent bfdb404867
commit de1f02420b

@ -27,7 +27,7 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
@ -39,22 +39,24 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector;
import net.yacy.cora.federate.solr.responsewriter.EnhancedXMLResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.GrepHTMLResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.HTMLResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.OpensearchResponseWriter;
import net.yacy.cora.federate.solr.responsewriter.YJsonResponseWriter;
import net.yacy.search.schema.CollectionSchema;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MultiMapSolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.XMLResponseWriter;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.servlet.SolrRequestParsers;
import org.apache.solr.servlet.cache.HttpCacheHeaderUtil;
import org.apache.solr.servlet.cache.Method;
@ -63,10 +65,10 @@ import org.apache.solr.util.FastWriter;
public class SolrServlet implements Filter {
private static final QueryResponseWriter responseWriter = new XMLResponseWriter();
private static EmbeddedSolrConnector connector;
public SolrServlet() {
private final Map<String, QueryResponseWriter> RESPONSE_WRITER = new HashMap<String, QueryResponseWriter>();
public SolrServlet() {
}
public static void initCore(EmbeddedSolrConnector c) {
@ -75,6 +77,19 @@ public class SolrServlet implements Filter {
@Override
public void init(FilterConfig config) throws ServletException {
OpensearchResponseWriter opensearchResponseWriter = new OpensearchResponseWriter();
// xml and xslt reponseWriter included in SorlCore.DEFAULT_RESPONSE_WRITERS
// DEFAULT_RESPONSE_WRITERS is allways checke, here only additional response writers
RESPONSE_WRITER.put("exml", new EnhancedXMLResponseWriter());
RESPONSE_WRITER.put("html", new HTMLResponseWriter());
RESPONSE_WRITER.put("grephtml", new GrepHTMLResponseWriter());
RESPONSE_WRITER.put("rss", opensearchResponseWriter); //try http://localhost:8090/solr/select?wt=rss&q=olympia&hl=true&hl.fl=text_t,h1,h2
RESPONSE_WRITER.put("opensearch", opensearchResponseWriter); //try http://localhost:8090/solr/select?wt=rss&q=olympia&hl=true&hl.fl=text_t,h1,h2
RESPONSE_WRITER.put("yjson", new YJsonResponseWriter()); //try http://localhost:8090/solr/select?wt=json&q=olympia&hl=true&hl.fl=text_t,h1,h2
// GSA response implemented in separate servlet
// RESPONSE_WRITER.put("gsa", new GSAResponseWriter());
}
@Override
@ -129,6 +144,15 @@ public class SolrServlet implements Filter {
queryStr = queryStr + "&df=" + CollectionSchema.text_t.getSolrFieldName();
}
MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(queryStr);
String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml
QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt);
if (responseWriter == null) {
responseWriter = SolrCore.DEFAULT_RESPONSE_WRITERS.get(wt);
if (responseWriter == null) {
hresponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Solr responsewriter not found for " + wt);
return;
}
}
req = connector.request(mmsp);
SolrQueryResponse rsp = connector.query(req);
@ -152,36 +176,11 @@ public class SolrServlet implements Filter {
}
// write response body
Writer out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset));
//debug
@SuppressWarnings("unchecked")
Iterator<Map.Entry<String, Object>> ie = rsp.getValues().iterator();
Map.Entry<String, Object> e;
while (ie.hasNext()) {
e = ie.next();
System.out.println("Field: " + e.getKey() + ", value: " + e.getValue().getClass().getName());
//Field: responseHeader, value: org.apache.solr.common.util.SimpleOrderedMap
//Field: response, value: org.apache.solr.search.DocSlice
if (e.getValue() instanceof DocList) {
DocList ids = (DocList) e.getValue();
SolrIndexSearcher searcher = req.getSearcher();
DocIterator iterator = ids.iterator();
int sz = ids.size();
for (int i = 0; i < sz; i++) {
int id = iterator.nextDoc();
searcher.doc(id);
}
}
}
Writer out = new FastWriter(new OutputStreamWriter(response.getOutputStream(),UTF8.charset));
responseWriter.write(out, req, rsp);
out.flush();
return;
} catch (final Throwable ex) {
sendError(hresponse, ex);
return;
} finally {
if (req != null) {
req.close();
@ -221,7 +220,7 @@ public class SolrServlet implements Filter {
private static final long serialVersionUID=-4497069674942245148L;
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.sendError(404, "Can not find: " + req.getRequestURI());
res.sendError(HttpServletResponse.SC_NOT_FOUND, "Can not find: " + req.getRequestURI());
}
}

@ -68,7 +68,7 @@ public class GrepHTMLResponseWriter implements QueryResponseWriter {
@Override
public String getContentType(final SolrQueryRequest request, final SolrQueryResponse response) {
return CONTENT_TYPE_XML_UTF8;
return "text/html";
}
@Override
@ -116,7 +116,7 @@ public class GrepHTMLResponseWriter implements QueryResponseWriter {
String h1 = "Document Grep for query \"" + query + "\" and grep phrase \"" + grep + "\"";
writer.write("<title>" + h1 + "</title>\n</head><body>\n<h1>" + h1 + "</h1>\n");
writer.write("<div id=\"api\"><a href=\"" + xmlquery + "\"><img src=\"../env/grafics/api.png\" width=\"60\" height=\"40\" alt=\"API\" /></a>\n");
writer.write("<span>This search result can also be retrieved as XML. Click the API icon to see an example call to the search rss API.</span>\n");
writer.write("<span>This search result can also be retrieved as XML. Click the API icon to see an example call to the search rss API.</span></div>\n");
for (int i = 0; i < sz; i++) {
int id = iterator.nextDoc();
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);

@ -60,7 +60,7 @@ public class HTMLResponseWriter implements QueryResponseWriter {
@Override
public String getContentType(final SolrQueryRequest request, final SolrQueryResponse response) {
return CONTENT_TYPE_XML_UTF8;
return "text/html";
}
@Override
@ -91,35 +91,40 @@ public class HTMLResponseWriter implements QueryResponseWriter {
String xmlquery = dqp.matcher("/solr/select?" + SolrParams.toSolrParams(paramsList).toString()).replaceAll("%22");
writer.write("<div id=\"api\"><a href=\"" + xmlquery + "\"><img src=\"../env/grafics/api.png\" width=\"60\" height=\"40\" alt=\"API\" /></a>\n");
writer.write("<span>This search result can also be retrieved as XML. Click the API icon to see this page as XML.</div>\n");
DocList response = ((ResultContext) values.get("response")).docs;
final int sz = response.size();
if (sz > 0) {
SolrIndexSearcher searcher = request.getSearcher();
DocIterator iterator = response.iterator();
IndexSchema schema = request.getSchema();
int id = iterator.nextDoc();
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);
LinkedHashMap<String, String> tdoc = translateDoc(schema, doc);
String title = tdoc.get(CollectionSchema.title.getSolrFieldName());
if (sz == 1) {
int id = iterator.nextDoc();
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);
LinkedHashMap<String, String> tdoc = translateDoc(schema, doc);
String title = tdoc.get(CollectionSchema.title.getSolrFieldName());
writer.write("<title>" + title + "</title>\n</head><body>\n");
writeDoc(writer, tdoc, title);
} else {
writer.write("<title>Document List</title>\n</head><body>\n");
for (int i = 0; i < sz; i++) {
int id = iterator.nextDoc();
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);
LinkedHashMap<String, String> tdoc = translateDoc(schema, doc);
String title = tdoc.get(CollectionSchema.title.getSolrFieldName());
writeDoc(writer, tdoc, title);
}
}
writer.write("<div id=\"api\"><a href=\"" + xmlquery + "\"><img src=\"../env/grafics/api.png\" width=\"60\" height=\"40\" alt=\"API\" /></a>\n");
writer.write("<span>This search result can also be retrieved as XML. Click the API icon to see this page as XML.</span></div>\n");
writeDoc(writer, tdoc, title);
while (iterator.hasNext()) {
id = iterator.nextDoc();
doc = searcher.doc(id, DEFAULT_FIELD_LIST);
tdoc = translateDoc(schema, doc);
title = tdoc.get(CollectionSchema.title.getSolrFieldName());
writeDoc(writer, tdoc, title);
}
} else {
writer.write("<title>No Document Found</title>\n</head><body>\n");
}
writer.write("</body></html>\n");
}

@ -23,7 +23,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.security.Principal;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@ -50,6 +49,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.ResultContext;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.util.FastWriter;
@ -73,6 +73,8 @@ public class GSAsearchServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(QueryResponseWriter.CONTENT_TYPE_XML_UTF8);
response.setStatus(HttpServletResponse.SC_OK);
respond(request, new serverObjects(), Switchboard.getSwitchboard(), response.getOutputStream());
}

Loading…
Cancel
Save