23 |
23 |
package edu.ucsb.nceas.metacat.index;
|
24 |
24 |
|
25 |
25 |
import java.io.ByteArrayInputStream;
|
|
26 |
import java.io.ByteArrayOutputStream;
|
26 |
27 |
import java.io.File;
|
27 |
28 |
import java.io.FileInputStream;
|
28 |
29 |
import java.io.IOException;
|
29 |
30 |
import java.io.InputStream;
|
|
31 |
import java.io.OutputStreamWriter;
|
30 |
32 |
import java.io.StringWriter;
|
31 |
33 |
import java.io.Writer;
|
|
34 |
import java.sql.SQLException;
|
|
35 |
import java.util.ArrayList;
|
32 |
36 |
import java.util.Collection;
|
33 |
|
import java.util.Iterator;
|
|
37 |
import java.util.Hashtable;
|
|
38 |
import java.util.List;
|
34 |
39 |
import java.util.Map;
|
35 |
|
import java.util.Set;
|
36 |
|
import java.util.Vector;
|
37 |
40 |
|
38 |
41 |
import javax.xml.parsers.ParserConfigurationException;
|
39 |
42 |
|
|
43 |
import org.apache.commons.io.IOUtils;
|
40 |
44 |
import org.apache.commons.logging.Log;
|
41 |
45 |
import org.apache.commons.logging.LogFactory;
|
42 |
|
import org.apache.lucene.index.IndexReader;
|
43 |
46 |
import org.apache.solr.client.solrj.SolrServer;
|
44 |
47 |
import org.apache.solr.client.solrj.SolrServerException;
|
45 |
48 |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
46 |
49 |
import org.apache.solr.client.solrj.response.QueryResponse;
|
|
50 |
import org.apache.solr.common.params.ModifiableSolrParams;
|
47 |
51 |
import org.apache.solr.common.params.SolrParams;
|
48 |
|
import org.apache.solr.common.util.SimpleOrderedMap;
|
49 |
52 |
import org.apache.solr.core.CoreContainer;
|
50 |
53 |
import org.apache.solr.core.SolrConfig;
|
51 |
54 |
import org.apache.solr.core.SolrCore;
|
52 |
|
import org.apache.solr.core.StandardDirectoryFactory;
|
53 |
|
import org.apache.solr.core.StandardIndexReaderFactory;
|
54 |
|
import org.apache.solr.handler.admin.LukeRequestHandler;
|
55 |
55 |
import org.apache.solr.request.LocalSolrQueryRequest;
|
56 |
56 |
import org.apache.solr.response.CSVResponseWriter;
|
57 |
57 |
import org.apache.solr.response.JSONResponseWriter;
|
... | ... | |
71 |
71 |
import org.xml.sax.InputSource;
|
72 |
72 |
import org.xml.sax.SAXException;
|
73 |
73 |
|
|
74 |
import edu.ucsb.nceas.metacat.DBTransform;
|
74 |
75 |
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
|
76 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
75 |
77 |
|
76 |
78 |
|
77 |
79 |
/**
|
... | ... | |
96 |
98 |
private static final String PHPS = "phps";
|
97 |
99 |
private static final String VELOCITY = "velocity";
|
98 |
100 |
private static final String CSV ="csv";
|
|
101 |
private static List<String> supportedWriterTypes = null;
|
99 |
102 |
private static final String VERSION = "3.4";
|
100 |
103 |
private static final String SOLRCONFDIR = "/conf";
|
101 |
104 |
private static final String SOLRSCHEMAFILEPATH = SOLRCONFDIR+"/schema.xml";
|
... | ... | |
123 |
126 |
* @throws ParserConfigurationException
|
124 |
127 |
*/
|
125 |
128 |
private MetacatSolrIndex() throws ParserConfigurationException, IOException, SAXException {
|
|
129 |
// these are handled directly by solr
|
|
130 |
supportedWriterTypes = new ArrayList<String>();
|
|
131 |
supportedWriterTypes.add(CSV);
|
|
132 |
supportedWriterTypes.add(JSON);
|
|
133 |
supportedWriterTypes.add(PHP);
|
|
134 |
supportedWriterTypes.add(PHPS);
|
|
135 |
supportedWriterTypes.add(RUBY);
|
|
136 |
supportedWriterTypes.add(VELOCITY);
|
|
137 |
supportedWriterTypes.add(PYTHON);
|
|
138 |
supportedWriterTypes.add(XML);
|
|
139 |
|
126 |
140 |
generateEmbeddedServer();
|
127 |
141 |
}
|
128 |
142 |
|
... | ... | |
147 |
161 |
* @param query the solr query
|
148 |
162 |
* @return the result as the InputStream
|
149 |
163 |
* @throws SolrServerException
|
|
164 |
* @throws ClassNotFoundException
|
|
165 |
* @throws SQLException
|
|
166 |
* @throws PropertyNotFoundException
|
150 |
167 |
*/
|
151 |
|
public InputStream query(String query) throws SolrServerException, IOException {
|
|
168 |
public InputStream query(String query) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
152 |
169 |
InputStream inputStream = null;
|
153 |
170 |
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
154 |
171 |
wt = solrParams.get(WT);
|
155 |
|
QueryResponse response = solrServer.query(solrParams);
|
156 |
|
inputStream = transformResults(solrParams, response);
|
|
172 |
|
|
173 |
// handle normal and skin-based queries
|
|
174 |
if (supportedWriterTypes.contains(wt)) {
|
|
175 |
// just handle as normal solr query
|
|
176 |
QueryResponse response = solrServer.query(solrParams);
|
|
177 |
inputStream = transformResults(solrParams, response);
|
|
178 |
}
|
|
179 |
else {
|
|
180 |
// assume it is a skin name
|
|
181 |
String qformat = wt;
|
|
182 |
|
|
183 |
// perform the solr query using wt=XML
|
|
184 |
wt = XML;
|
|
185 |
ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
|
|
186 |
msp.set(WT, wt);
|
|
187 |
QueryResponse response = solrServer.query(msp);
|
|
188 |
inputStream = transformResults(msp, response);
|
|
189 |
|
|
190 |
// apply the stylesheet (XML->HTML)
|
|
191 |
DBTransform transformer = new DBTransform();
|
|
192 |
String documentContent = IOUtils.toString(inputStream, "UTF-8");
|
|
193 |
String sourceType = "solr";
|
|
194 |
String targetType = "-//W3C//HTML//EN";
|
|
195 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
196 |
Writer writer = new OutputStreamWriter(baos , "UTF-8");
|
|
197 |
// TODO: include more params?
|
|
198 |
Hashtable<String, String[]> params = new Hashtable<String, String[]>();
|
|
199 |
params.put("qformat", new String[] {qformat});
|
|
200 |
transformer.transformXMLDocument(
|
|
201 |
documentContent ,
|
|
202 |
sourceType,
|
|
203 |
targetType ,
|
|
204 |
qformat,
|
|
205 |
writer,
|
|
206 |
params,
|
|
207 |
null //sessionid
|
|
208 |
);
|
|
209 |
|
|
210 |
// finally, get the HTML back
|
|
211 |
inputStream = new ByteArrayInputStream(baos.toByteArray());
|
|
212 |
}
|
|
213 |
|
157 |
214 |
return inputStream;
|
158 |
215 |
}
|
159 |
216 |
|
Allow use of server-side XSLT for SOLR queries that include "wt=<qformat>". https://projects.ecoinformatics.org/ecoinfo/issues/5812