parent
082c9a98c1
commit
1437c45383
@ -1,195 +1,193 @@
|
|||||||
/**
|
/**
|
||||||
* HTMLResponseWriter
|
* HTMLResponseWriter
|
||||||
* Copyright 2013 by Michael Peter Christen
|
* Copyright 2013 by Michael Peter Christen
|
||||||
* First released 09.06.2013 at http://yacy.net
|
* First released 09.06.2013 at http://yacy.net
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
* 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,
|
* This library is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
* along with this program in the file lgpl21.txt
|
* along with this program in the file lgpl21.txt
|
||||||
* If not, see <http://www.gnu.org/licenses/>.
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.yacy.cora.federate.solr.responsewriter;
|
package net.yacy.cora.federate.solr.responsewriter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import net.yacy.cora.federate.solr.SolrType;
|
import net.yacy.cora.federate.solr.SolrType;
|
||||||
import net.yacy.search.schema.CollectionSchema;
|
import net.yacy.search.schema.CollectionSchema;
|
||||||
|
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.index.IndexableField;
|
import org.apache.lucene.index.IndexableField;
|
||||||
import org.apache.solr.common.params.SolrParams;
|
import org.apache.solr.common.params.SolrParams;
|
||||||
import org.apache.solr.common.util.NamedList;
|
import org.apache.solr.common.util.NamedList;
|
||||||
import org.apache.solr.common.util.XML;
|
import org.apache.solr.common.util.XML;
|
||||||
import org.apache.solr.request.SolrQueryRequest;
|
import org.apache.solr.request.SolrQueryRequest;
|
||||||
import org.apache.solr.response.QueryResponseWriter;
|
import org.apache.solr.response.QueryResponseWriter;
|
||||||
import org.apache.solr.response.ResultContext;
|
import org.apache.solr.response.ResultContext;
|
||||||
import org.apache.solr.response.SolrQueryResponse;
|
import org.apache.solr.response.SolrQueryResponse;
|
||||||
import org.apache.solr.schema.FieldType;
|
import org.apache.solr.schema.FieldType;
|
||||||
import org.apache.solr.schema.IndexSchema;
|
import org.apache.solr.schema.IndexSchema;
|
||||||
import org.apache.solr.schema.SchemaField;
|
import org.apache.solr.schema.SchemaField;
|
||||||
import org.apache.solr.schema.TextField;
|
import org.apache.solr.schema.TextField;
|
||||||
import org.apache.solr.search.DocIterator;
|
import org.apache.solr.search.DocIterator;
|
||||||
import org.apache.solr.search.DocList;
|
import org.apache.solr.search.DocList;
|
||||||
import org.apache.solr.search.SolrIndexSearcher;
|
import org.apache.solr.search.SolrIndexSearcher;
|
||||||
|
|
||||||
public class HTMLResponseWriter implements QueryResponseWriter {
|
public class HTMLResponseWriter implements QueryResponseWriter {
|
||||||
|
|
||||||
private static final Set<String> DEFAULT_FIELD_LIST = null;
|
private static final Set<String> DEFAULT_FIELD_LIST = null;
|
||||||
private static final Pattern dqp = Pattern.compile("\"");
|
private static final Pattern dqp = Pattern.compile("\"");
|
||||||
|
|
||||||
public HTMLResponseWriter() {
|
public HTMLResponseWriter() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getContentType(final SolrQueryRequest request, final SolrQueryResponse response) {
|
public String getContentType(final SolrQueryRequest request, final SolrQueryResponse response) {
|
||||||
return "text/html";
|
return "text/html";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(@SuppressWarnings("rawtypes") NamedList n) {
|
public void init(@SuppressWarnings("rawtypes") NamedList n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException {
|
public void write(final Writer writer, final SolrQueryRequest request, final SolrQueryResponse rsp) throws IOException {
|
||||||
NamedList<?> values = rsp.getValues();
|
NamedList<?> values = rsp.getValues();
|
||||||
assert values.get("responseHeader") != null;
|
assert values.get("responseHeader") != null;
|
||||||
assert values.get("response") != null;
|
assert values.get("response") != null;
|
||||||
|
|
||||||
writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
|
writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
|
||||||
//writer.write("<!--\n");
|
//writer.write("<!--\n");
|
||||||
//writer.write("this is a XHTML+RDFa file. It contains RDF annotations with dublin core properties\n");
|
//writer.write("this is a XHTML+RDFa file. It contains RDF annotations with dublin core properties\n");
|
||||||
//writer.write("you can validate it with http://validator.w3.org/\n");
|
//writer.write("you can validate it with http://validator.w3.org/\n");
|
||||||
//writer.write("-->\n");
|
//writer.write("-->\n");
|
||||||
writer.write("<html xmlns=\"http://www.w3.org/1999/xhtml\"\n");
|
writer.write("<html xmlns=\"http://www.w3.org/1999/xhtml\"\n");
|
||||||
writer.write(" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n");
|
writer.write(" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n");
|
||||||
writer.write(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
|
writer.write(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
|
||||||
writer.write(" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n");
|
writer.write(" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n");
|
||||||
writer.write("<head profile=\"http://www.w3.org/2003/g/data-view\">\n");
|
writer.write("<head profile=\"http://www.w3.org/2003/g/data-view\">\n");
|
||||||
//writer.write("<link rel=\"transformation\" href=\"http://www-sop.inria.fr/acacia/soft/RDFa2RDFXML.xsl\"/>\n");
|
//writer.write("<link rel=\"transformation\" href=\"http://www-sop.inria.fr/acacia/soft/RDFa2RDFXML.xsl\"/>\n");
|
||||||
writer.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/env/base.css\" />\n");
|
writer.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/env/base.css\" />\n");
|
||||||
writer.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/env/style.css\" />\n");
|
writer.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/env/style.css\" />\n");
|
||||||
NamedList<Object> paramsList = request.getOriginalParams().toNamedList();
|
NamedList<Object> paramsList = request.getOriginalParams().toNamedList();
|
||||||
paramsList.remove("wt");
|
paramsList.remove("wt");
|
||||||
String xmlquery = dqp.matcher("/solr/select?" + SolrParams.toSolrParams(paramsList).toString()).replaceAll("%22");
|
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();
|
||||||
DocList response = ((ResultContext) values.get("response")).docs;
|
if (sz > 0) {
|
||||||
final int sz = response.size();
|
SolrIndexSearcher searcher = request.getSearcher();
|
||||||
if (sz > 0) {
|
DocIterator iterator = response.iterator();
|
||||||
SolrIndexSearcher searcher = request.getSearcher();
|
IndexSchema schema = request.getSchema();
|
||||||
DocIterator iterator = response.iterator();
|
|
||||||
IndexSchema schema = request.getSchema();
|
int id = iterator.nextDoc();
|
||||||
|
Document doc = searcher.doc(id, DEFAULT_FIELD_LIST);
|
||||||
int id = iterator.nextDoc();
|
LinkedHashMap<String, String> tdoc = translateDoc(schema, doc);
|
||||||
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) {
|
||||||
String title = tdoc.get(CollectionSchema.title.getSolrFieldName());
|
writer.write("<title>" + title + "</title>\n</head><body>\n");
|
||||||
if (sz == 1) {
|
} else {
|
||||||
writer.write("<title>" + title + "</title>\n</head><body>\n");
|
writer.write("<title>Document List</title>\n</head><body>\n");
|
||||||
} else {
|
}
|
||||||
writer.write("<title>Document List</title>\n</head><body>\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 this page as XML.</span></div>\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 this page as XML.</span></div>\n");
|
writeDoc(writer, tdoc, title);
|
||||||
|
|
||||||
writeDoc(writer, tdoc, title);
|
while (iterator.hasNext()) {
|
||||||
|
id = iterator.nextDoc();
|
||||||
while (iterator.hasNext()) {
|
doc = searcher.doc(id, DEFAULT_FIELD_LIST);
|
||||||
id = iterator.nextDoc();
|
tdoc = translateDoc(schema, doc);
|
||||||
doc = searcher.doc(id, DEFAULT_FIELD_LIST);
|
title = tdoc.get(CollectionSchema.title.getSolrFieldName());
|
||||||
tdoc = translateDoc(schema, doc);
|
writeDoc(writer, tdoc, title);
|
||||||
title = tdoc.get(CollectionSchema.title.getSolrFieldName());
|
}
|
||||||
writeDoc(writer, tdoc, title);
|
} else {
|
||||||
}
|
writer.write("<title>No Document Found</title>\n</head><body>\n");
|
||||||
} else {
|
}
|
||||||
writer.write("<title>No Document Found</title>\n</head><body>\n");
|
|
||||||
}
|
writer.write("</body></html>\n");
|
||||||
|
}
|
||||||
writer.write("</body></html>\n");
|
|
||||||
}
|
private static final void writeDoc(Writer writer, LinkedHashMap<String, String> tdoc, String title) throws IOException {
|
||||||
|
writer.write("<form name=\"yacydoc" + title + "\" method=\"post\" action=\"#\" enctype=\"multipart/form-data\" accept-charset=\"UTF-8\">\n");
|
||||||
private static final void writeDoc(Writer writer, LinkedHashMap<String, String> tdoc, String title) throws IOException {
|
writer.write("<fieldset>\n");
|
||||||
writer.write("<form name=\"yacydoc" + title + "\" method=\"post\" action=\"#\" enctype=\"multipart/form-data\" accept-charset=\"UTF-8\">\n");
|
writer.write("<h1 property=\"dc:Title\">" + title + "</h1>\n");
|
||||||
writer.write("<fieldset>\n");
|
writer.write("<dl>\n");
|
||||||
writer.write("<h1 property=\"dc:Title\">" + title + "</h1>\n");
|
for (Map.Entry<String, String> entry: tdoc.entrySet()) {
|
||||||
writer.write("<dl>\n");
|
writer.write("<dt>");
|
||||||
for (Map.Entry<String, String> entry: tdoc.entrySet()) {
|
writer.write(entry.getKey());
|
||||||
writer.write("<dt>");
|
writer.write("</dt><dd>");
|
||||||
writer.write(entry.getKey());
|
XML.escapeAttributeValue(entry.getValue(), writer);
|
||||||
writer.write("</dt><dd>");
|
writer.write("</dd>\n");
|
||||||
XML.escapeAttributeValue(entry.getValue(), writer);
|
}
|
||||||
writer.write("</dd>\n");
|
writer.write("</dl>\n");
|
||||||
}
|
writer.write("</fieldset>\n");
|
||||||
writer.write("</dl>\n");
|
writer.write("</form>\n");
|
||||||
writer.write("</fieldset>\n");
|
}
|
||||||
writer.write("</form>\n");
|
|
||||||
}
|
static final LinkedHashMap<String, String> translateDoc(final IndexSchema schema, final Document doc) {
|
||||||
|
List<IndexableField> fields = doc.getFields();
|
||||||
static final LinkedHashMap<String, String> translateDoc(final IndexSchema schema, final Document doc) {
|
int sz = fields.size();
|
||||||
List<IndexableField> fields = doc.getFields();
|
int fidx1 = 0, fidx2 = 0;
|
||||||
int sz = fields.size();
|
LinkedHashMap<String, String> kv = new LinkedHashMap<String, String>();
|
||||||
int fidx1 = 0, fidx2 = 0;
|
while (fidx1 < sz) {
|
||||||
LinkedHashMap<String, String> kv = new LinkedHashMap<String, String>();
|
IndexableField value = fields.get(fidx1);
|
||||||
while (fidx1 < sz) {
|
String fieldName = value.name();
|
||||||
IndexableField value = fields.get(fidx1);
|
fidx2 = fidx1 + 1;
|
||||||
String fieldName = value.name();
|
while (fidx2 < sz && fieldName.equals(fields.get(fidx2).name())) {
|
||||||
fidx2 = fidx1 + 1;
|
fidx2++;
|
||||||
while (fidx2 < sz && fieldName.equals(fields.get(fidx2).name())) {
|
}
|
||||||
fidx2++;
|
SchemaField sf = schema.getFieldOrNull(fieldName);
|
||||||
}
|
if (sf == null) sf = new SchemaField(fieldName, new TextField());
|
||||||
SchemaField sf = schema.getFieldOrNull(fieldName);
|
FieldType type = sf.getType();
|
||||||
if (sf == null) sf = new SchemaField(fieldName, new TextField());
|
|
||||||
FieldType type = sf.getType();
|
if (fidx1 + 1 == fidx2) {
|
||||||
|
if (sf.multiValued()) {
|
||||||
if (fidx1 + 1 == fidx2) {
|
String sv = value.stringValue();
|
||||||
if (sf.multiValued()) {
|
kv.put(fieldName, field2string(type, sv));
|
||||||
String sv = value.stringValue();
|
} else {
|
||||||
kv.put(fieldName, field2string(type, sv));
|
kv.put(fieldName, field2string(type, value.stringValue()));
|
||||||
} else {
|
}
|
||||||
kv.put(fieldName, field2string(type, value.stringValue()));
|
} else {
|
||||||
}
|
for (int i = fidx1; i < fidx2; i++) {
|
||||||
} else {
|
String sv = fields.get(i).stringValue();
|
||||||
for (int i = fidx1; i < fidx2; i++) {
|
kv.put(fieldName + "_" + i, field2string(type, sv));
|
||||||
String sv = fields.get(i).stringValue();
|
}
|
||||||
kv.put(fieldName + "_" + i, field2string(type, sv));
|
}
|
||||||
}
|
|
||||||
}
|
fidx1 = fidx2;
|
||||||
|
}
|
||||||
fidx1 = fidx2;
|
return kv;
|
||||||
}
|
}
|
||||||
return kv;
|
|
||||||
}
|
@SuppressWarnings("deprecation")
|
||||||
|
private static String field2string(final FieldType type, final String value) {
|
||||||
@SuppressWarnings("deprecation")
|
String typeName = type.getTypeName();
|
||||||
private static String field2string(final FieldType type, final String value) {
|
if (typeName.equals(SolrType.bool.printName())) {
|
||||||
String typeName = type.getTypeName();
|
return "F".equals(value) ? "false" : "true";
|
||||||
if (typeName.equals(SolrType.bool.printName())) {
|
} else if (typeName.equals(SolrType.date.printName())) {
|
||||||
return "F".equals(value) ? "false" : "true";
|
return org.apache.solr.schema.DateField.formatExternal(new Date(Long.parseLong(value))); // this is declared deprecated in solr 4.2.1 but is still used as done here
|
||||||
} else if (typeName.equals(SolrType.date.printName())) {
|
}
|
||||||
return org.apache.solr.schema.DateField.formatExternal(new Date(Long.parseLong(value))); // this is declared deprecated in solr 4.2.1 but is still used as done here
|
return value;
|
||||||
}
|
}
|
||||||
return value;
|
|
||||||
}
|
// XML.escapeCharData(val, writer);
|
||||||
|
}
|
||||||
// XML.escapeCharData(val, writer);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in new issue