1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000-2011 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: leinfelder $'
|
7
|
* '$Date: 2012-11-29 16:52:29 -0800 (Thu, 29 Nov 2012) $'
|
8
|
*
|
9
|
* This program is free software; you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation; either version 2 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* You should have received a copy of the GNU General Public License
|
20
|
* along with this program; if not, write to the Free Software
|
21
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22
|
*/
|
23
|
package edu.ucsb.nceas.metacat.index;
|
24
|
|
25
|
import java.io.ByteArrayInputStream;
|
26
|
import java.io.ByteArrayOutputStream;
|
27
|
import java.io.File;
|
28
|
import java.io.FileInputStream;
|
29
|
import java.io.IOException;
|
30
|
import java.io.InputStream;
|
31
|
import java.io.OutputStreamWriter;
|
32
|
import java.io.StringWriter;
|
33
|
import java.io.Writer;
|
34
|
import java.net.MalformedURLException;
|
35
|
import java.net.URL;
|
36
|
import java.sql.SQLException;
|
37
|
import java.util.ArrayList;
|
38
|
import java.util.Collection;
|
39
|
import java.util.Hashtable;
|
40
|
import java.util.List;
|
41
|
import java.util.Map;
|
42
|
|
43
|
import javax.xml.parsers.ParserConfigurationException;
|
44
|
|
45
|
import org.apache.commons.io.IOUtils;
|
46
|
import org.apache.commons.logging.Log;
|
47
|
import org.apache.commons.logging.LogFactory;
|
48
|
import org.apache.solr.client.solrj.SolrServer;
|
49
|
import org.apache.solr.client.solrj.SolrServerException;
|
50
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
51
|
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
52
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
53
|
import org.apache.solr.common.params.AppendedSolrParams;
|
54
|
import org.apache.solr.common.params.ModifiableSolrParams;
|
55
|
import org.apache.solr.common.params.SolrParams;
|
56
|
import org.apache.solr.common.util.NamedList;
|
57
|
import org.apache.solr.core.CoreContainer;
|
58
|
import org.apache.solr.core.SolrConfig;
|
59
|
import org.apache.solr.core.SolrCore;
|
60
|
import org.apache.solr.request.LocalSolrQueryRequest;
|
61
|
import org.apache.solr.response.CSVResponseWriter;
|
62
|
import org.apache.solr.response.JSONResponseWriter;
|
63
|
import org.apache.solr.response.PHPResponseWriter;
|
64
|
import org.apache.solr.response.PHPSerializedResponseWriter;
|
65
|
import org.apache.solr.response.PythonResponseWriter;
|
66
|
import org.apache.solr.response.QueryResponseWriter;
|
67
|
import org.apache.solr.response.RubyResponseWriter;
|
68
|
import org.apache.solr.response.SolrQueryResponse;
|
69
|
import org.apache.solr.response.VelocityResponseWriter;
|
70
|
import org.apache.solr.response.XMLResponseWriter;
|
71
|
import org.apache.solr.schema.IndexSchema;
|
72
|
import org.apache.solr.schema.SchemaField;
|
73
|
import org.apache.solr.servlet.SolrRequestParsers;
|
74
|
import org.dataone.configuration.Settings;
|
75
|
import org.dataone.service.types.v1_1.QueryEngineDescription;
|
76
|
import org.dataone.service.types.v1_1.QueryField;
|
77
|
import org.dataone.service.util.Constants;
|
78
|
import org.xml.sax.InputSource;
|
79
|
import org.xml.sax.SAXException;
|
80
|
|
81
|
import edu.ucsb.nceas.metacat.DBTransform;
|
82
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
83
|
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
|
84
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
85
|
|
86
|
|
87
|
/**
|
88
|
* This class will query the solr server and return the result.
|
89
|
* @author tao
|
90
|
*
|
91
|
*/
|
92
|
public class MetacatSolrIndex {
|
93
|
|
94
|
|
95
|
public static final String SOLRQUERY = "solr";
|
96
|
public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
|
97
|
public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
|
98
|
public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
|
99
|
public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
|
100
|
private static final String WT = "wt";//the property name to specify the return type
|
101
|
private static final String XML = "xml";
|
102
|
private static final String JSON = "json";
|
103
|
private static final String PYTHON = "python";
|
104
|
private static final String RUBY = "ruby";
|
105
|
private static final String PHP = "php";
|
106
|
private static final String PHPS = "phps";
|
107
|
private static final String VELOCITY = "velocity";
|
108
|
private static final String CSV ="csv";
|
109
|
private static List<String> supportedWriterTypes = null;
|
110
|
private static final String VERSION = "3.4";
|
111
|
private static final String SOLRCONFDIR = "/conf";
|
112
|
private static final String SOLRSCHEMAFILEPATH = SOLRCONFDIR+"/schema.xml";
|
113
|
private static final String FILTERQUERY = "fq";
|
114
|
private static final String READPERMISSION = "readPermission";
|
115
|
private static final String RIGHTSHOLDER = "rightsHolder";
|
116
|
private static final String OPENPARENTHESE = "(";
|
117
|
private static final String CLOSEPARENTHESE = ")";
|
118
|
private static final String COLON = ":";
|
119
|
private static final String OR = "OR";
|
120
|
//private static final String SOLRCONFIGPATH = "/conf/solrconfig.xml";
|
121
|
|
122
|
private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
|
123
|
private CoreContainer coreContainer = null;
|
124
|
private SolrServer solrServer = null;
|
125
|
//private String wt = null;//specify the return format.
|
126
|
private String collectionName = null;
|
127
|
private String solrBaseURL = null;
|
128
|
private boolean isEmbeddedSolrServer = true;
|
129
|
private static MetacatSolrIndex solrIndex = null;
|
130
|
|
131
|
public static MetacatSolrIndex getInstance() throws Exception {
|
132
|
if (solrIndex == null) {
|
133
|
solrIndex = new MetacatSolrIndex();
|
134
|
}
|
135
|
return solrIndex;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Constructor
|
140
|
* @throws SAXException
|
141
|
* @throws IOException
|
142
|
* @throws ParserConfigurationException
|
143
|
*/
|
144
|
private MetacatSolrIndex() throws Exception {
|
145
|
// these are handled directly by solr
|
146
|
supportedWriterTypes = new ArrayList<String>();
|
147
|
supportedWriterTypes.add(CSV);
|
148
|
supportedWriterTypes.add(JSON);
|
149
|
supportedWriterTypes.add(PHP);
|
150
|
supportedWriterTypes.add(PHPS);
|
151
|
supportedWriterTypes.add(RUBY);
|
152
|
supportedWriterTypes.add(VELOCITY);
|
153
|
supportedWriterTypes.add(PYTHON);
|
154
|
supportedWriterTypes.add(XML);
|
155
|
|
156
|
generateSolrServer();
|
157
|
}
|
158
|
|
159
|
|
160
|
/*
|
161
|
* Generate the embedded solr server
|
162
|
*/
|
163
|
private void generateSolrServer() throws Exception {
|
164
|
solrServer = SolrServerFactory.createSolrServer();
|
165
|
if(solrServer instanceof EmbeddedSolrServer) {
|
166
|
isEmbeddedSolrServer = true;
|
167
|
coreContainer = SolrServerFactory.getCoreContainer();
|
168
|
collectionName = SolrServerFactory.getCollectionName();
|
169
|
} else {
|
170
|
isEmbeddedSolrServer = false;
|
171
|
CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
|
172
|
solrBaseURL = httpServer.getBaseURL();
|
173
|
}
|
174
|
|
175
|
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Query the solr server
|
180
|
* @param query the solr query
|
181
|
* @return the result as the InputStream
|
182
|
* @throws SolrServerException
|
183
|
* @throws ClassNotFoundException
|
184
|
* @throws SQLException
|
185
|
* @throws PropertyNotFoundException
|
186
|
*/
|
187
|
public InputStream query(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
188
|
if(isEmbeddedSolrServer) {
|
189
|
return queryEmbedded(query, user, groups);
|
190
|
} else {
|
191
|
return queryHttp(query, user, groups);
|
192
|
}
|
193
|
}
|
194
|
/**
|
195
|
* Query a solr embedded server
|
196
|
* @param query the solr query
|
197
|
* @return the result as the InputStream
|
198
|
* @throws SolrServerException
|
199
|
* @throws ClassNotFoundException
|
200
|
* @throws SQLException
|
201
|
* @throws PropertyNotFoundException
|
202
|
*/
|
203
|
private InputStream queryEmbedded(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
204
|
InputStream inputStream = null;
|
205
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
206
|
solrParams = appendAccessFilterParams(solrParams, user, groups);
|
207
|
String wt = solrParams.get(WT);
|
208
|
|
209
|
// handle normal and skin-based queries
|
210
|
if (wt == null ||supportedWriterTypes.contains(wt)) {
|
211
|
// just handle as normal solr query
|
212
|
QueryResponse response = solrServer.query(solrParams);
|
213
|
inputStream = transformResults(solrParams, response, wt);
|
214
|
}
|
215
|
else {
|
216
|
// assume it is a skin name
|
217
|
String qformat = wt;
|
218
|
|
219
|
// perform the solr query using wt=XML
|
220
|
wt = XML;
|
221
|
ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
|
222
|
msp.set(WT, wt);
|
223
|
QueryResponse response = solrServer.query(msp);
|
224
|
inputStream = transformResults(msp, response, wt);
|
225
|
|
226
|
// apply the stylesheet (XML->HTML)
|
227
|
DBTransform transformer = new DBTransform();
|
228
|
String documentContent = IOUtils.toString(inputStream, "UTF-8");
|
229
|
String sourceType = "solr";
|
230
|
String targetType = "-//W3C//HTML//EN";
|
231
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
232
|
Writer writer = new OutputStreamWriter(baos , "UTF-8");
|
233
|
// TODO: include more params?
|
234
|
Hashtable<String, String[]> params = new Hashtable<String, String[]>();
|
235
|
params.put("qformat", new String[] {qformat});
|
236
|
transformer.transformXMLDocument(
|
237
|
documentContent ,
|
238
|
sourceType,
|
239
|
targetType ,
|
240
|
qformat,
|
241
|
writer,
|
242
|
params,
|
243
|
null //sessionid
|
244
|
);
|
245
|
|
246
|
// finally, get the HTML back
|
247
|
inputStream = new ByteArrayInputStream(baos.toByteArray());
|
248
|
}
|
249
|
|
250
|
return inputStream;
|
251
|
}
|
252
|
|
253
|
/*
|
254
|
* Query a http server. We directly build the url to send the http server.
|
255
|
* The reason we don't use the method is:
|
256
|
* QueryResponse response = solrServer.query(solrParams);
|
257
|
* When we transform the QueryReponse object to the InputStream object, we need to have SolrCore object
|
258
|
* which is not available for the SolrHttpServer.
|
259
|
*
|
260
|
*/
|
261
|
private InputStream queryHttp(String query, String user, String[] groups) throws IOException {
|
262
|
StringBuffer accessFilter = generateAccessFilterParamsString(user, groups);
|
263
|
if(accessFilter != null && accessFilter.length() != 0) {
|
264
|
query = solrBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
|
265
|
}
|
266
|
URL url = new URL(query);
|
267
|
return url.openStream();
|
268
|
}
|
269
|
|
270
|
|
271
|
/*
|
272
|
* Append the access filter query to the params
|
273
|
*/
|
274
|
private SolrParams appendAccessFilterParams(SolrParams solrParams, String user, String[] groups) {
|
275
|
SolrParams append = null;
|
276
|
if(solrParams != null) {
|
277
|
StringBuffer query = generateAccessFilterParamsString(user, groups);
|
278
|
if(query != null && query.length() != 0) {
|
279
|
log.info("=================== fq query is "+query.toString());
|
280
|
NamedList fq = new NamedList();
|
281
|
fq.add(FILTERQUERY, query.toString());
|
282
|
SolrParams fqParam = SolrParams.toSolrParams(fq);
|
283
|
append = new AppendedSolrParams(solrParams, fqParam);
|
284
|
} else {
|
285
|
append = solrParams;
|
286
|
}
|
287
|
}
|
288
|
return append;
|
289
|
}
|
290
|
|
291
|
private StringBuffer generateAccessFilterParamsString(String user, String[] groups) {
|
292
|
StringBuffer query = new StringBuffer();
|
293
|
if (user != null && groups != null) {
|
294
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
295
|
if(!user.equals(Constants.SUBJECT_PUBLIC)) {
|
296
|
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
297
|
}
|
298
|
for(int i=0; i<groups.length; i++) {
|
299
|
if(groups[i] != null) {
|
300
|
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
301
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
302
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
303
|
}
|
304
|
}
|
305
|
}
|
306
|
} else if (user != null && groups == null) {
|
307
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
308
|
if(!user.equals(Constants.SUBJECT_PUBLIC)) {
|
309
|
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
310
|
}
|
311
|
} else if ( user == null && groups != null) {
|
312
|
for (int i=0; i<groups.length; i++) {
|
313
|
if(groups[i]!= null) {
|
314
|
if(i==0) {
|
315
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
316
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
317
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
318
|
}
|
319
|
} else {
|
320
|
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
321
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
322
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
323
|
}
|
324
|
}
|
325
|
}
|
326
|
|
327
|
}
|
328
|
}
|
329
|
return query;
|
330
|
}
|
331
|
|
332
|
|
333
|
/*
|
334
|
* Transform the Queryresponse to the InputStream
|
335
|
*/
|
336
|
private InputStream transformResults(SolrParams request, QueryResponse response, String wt) throws SolrServerException, IOException {
|
337
|
//InputStream stream = null;
|
338
|
QueryResponseWriter writer = generateResponseWriter(wt);
|
339
|
Writer results = new StringWriter();
|
340
|
SolrQueryResponse sResponse = new SolrQueryResponse();
|
341
|
sResponse.setAllValues(response.getResponse());
|
342
|
SolrCore core =coreContainer.getCore(collectionName);
|
343
|
writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
|
344
|
return new ByteArrayInputStream(results.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING));
|
345
|
}
|
346
|
|
347
|
|
348
|
/* Here is the list of the handler class to handle different format.
|
349
|
* <queryResponseWriter name="xml" default="true" class="solr.XMLResponseWriter" />
|
350
|
* <queryResponseWriter name="json" class="solr.JSONResponseWriter"/>
|
351
|
* <queryResponseWriter name="python" class="solr.PythonResponseWriter"/>
|
352
|
* <queryResponseWriter name="ruby" class="solr.RubyResponseWriter"/>
|
353
|
* <queryResponseWriter name="php" class="solr.PHPResponseWriter"/>
|
354
|
* <queryResponseWriter name="phps" class="solr.PHPSerializedResponseWriter"/>
|
355
|
* <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter"/>
|
356
|
* <queryResponseWriter name="csv" class="solr.CSVResponseWriter"/>
|
357
|
*/
|
358
|
private QueryResponseWriter generateResponseWriter(String wt) throws SolrServerException {
|
359
|
QueryResponseWriter writer = null;
|
360
|
if(wt == null || wt.trim().equals("") || wt.equals(XML)) {
|
361
|
writer = new XMLResponseWriter();
|
362
|
} else if(wt.equals(JSON)) {
|
363
|
writer = new JSONResponseWriter();
|
364
|
} else if(wt.equals(PYTHON)) {
|
365
|
writer = new PythonResponseWriter();
|
366
|
} else if(wt.equals(RUBY)) {
|
367
|
writer = new RubyResponseWriter();
|
368
|
} else if(wt.equals(PHP)) {
|
369
|
writer = new PHPResponseWriter();
|
370
|
} else if(wt.equals(PHPS)) {
|
371
|
writer = new PHPSerializedResponseWriter();
|
372
|
} else if(wt.equals(VELOCITY)) {
|
373
|
writer = new VelocityResponseWriter();
|
374
|
} else if(wt.equals(CSV)) {
|
375
|
writer = new CSVResponseWriter();
|
376
|
} else {
|
377
|
throw new SolrServerException("Metacat doesn't support this response format "+wt);
|
378
|
}
|
379
|
return writer;
|
380
|
}
|
381
|
|
382
|
}
|