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