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.SolrQueryResponseWriterFactory;
|
86
|
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
|
87
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
88
|
|
89
|
|
90
|
/**
|
91
|
* This class will query the solr server and return the result.
|
92
|
* @author tao
|
93
|
*
|
94
|
*/
|
95
|
public class MetacatSolrIndex {
|
96
|
|
97
|
|
98
|
public static final String SOLRQUERY = "solr";
|
99
|
public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
|
100
|
public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
|
101
|
public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
|
102
|
public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
|
103
|
private static final String WT = "wt";//the property name to specify the return type
|
104
|
|
105
|
private static List<String> supportedWriterTypes = null;
|
106
|
private static final String VERSION = "3.4";
|
107
|
private static final String SOLRCONFDIR = "/conf";
|
108
|
private static final String SOLRSCHEMAFILEPATH = SOLRCONFDIR+"/schema.xml";
|
109
|
private static final String FILTERQUERY = "fq";
|
110
|
private static final String READPERMISSION = "readPermission";
|
111
|
private static final String RIGHTSHOLDER = "rightsHolder";
|
112
|
private static final String OPENPARENTHESE = "(";
|
113
|
private static final String CLOSEPARENTHESE = ")";
|
114
|
private static final String COLON = ":";
|
115
|
private static final String OR = "OR";
|
116
|
//private static final String SOLRCONFIGPATH = "/conf/solrconfig.xml";
|
117
|
|
118
|
private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
|
119
|
private CoreContainer coreContainer = null;
|
120
|
private SolrServer solrServer = null;
|
121
|
//private String wt = null;//specify the return format.
|
122
|
private String collectionName = null;
|
123
|
private String solrBaseURL = null;
|
124
|
private boolean isEmbeddedSolrServer = true;
|
125
|
private static MetacatSolrIndex solrIndex = null;
|
126
|
|
127
|
public static MetacatSolrIndex getInstance() throws Exception {
|
128
|
if (solrIndex == null) {
|
129
|
solrIndex = new MetacatSolrIndex();
|
130
|
}
|
131
|
return solrIndex;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Constructor
|
136
|
* @throws SAXException
|
137
|
* @throws IOException
|
138
|
* @throws ParserConfigurationException
|
139
|
*/
|
140
|
private MetacatSolrIndex() throws Exception {
|
141
|
// these are handled directly by solr
|
142
|
supportedWriterTypes = new ArrayList<String>();
|
143
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
|
144
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
|
145
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
|
146
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
|
147
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
|
148
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
|
149
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
|
150
|
supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
|
151
|
|
152
|
generateSolrServer();
|
153
|
}
|
154
|
|
155
|
|
156
|
/*
|
157
|
* Generate the embedded solr server
|
158
|
*/
|
159
|
private void generateSolrServer() throws Exception {
|
160
|
solrServer = SolrServerFactory.createSolrServer();
|
161
|
if(solrServer instanceof EmbeddedSolrServer) {
|
162
|
isEmbeddedSolrServer = true;
|
163
|
coreContainer = SolrServerFactory.getCoreContainer();
|
164
|
collectionName = SolrServerFactory.getCollectionName();
|
165
|
} else {
|
166
|
isEmbeddedSolrServer = false;
|
167
|
CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
|
168
|
solrBaseURL = httpServer.getBaseURL();
|
169
|
}
|
170
|
|
171
|
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Query the solr server
|
176
|
* @param query the solr query
|
177
|
* @param authorizedSubjects the authorized subjects in this query session
|
178
|
* @return the result as the InputStream
|
179
|
* @throws SolrServerException
|
180
|
* @throws ClassNotFoundException
|
181
|
* @throws SQLException
|
182
|
* @throws PropertyNotFoundException
|
183
|
* @throws SAXException
|
184
|
* @throws ParserConfigurationException
|
185
|
*/
|
186
|
public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException, ParserConfigurationException, SAXException {
|
187
|
if(authorizedSubjects == null || authorizedSubjects.isEmpty()) {
|
188
|
throw new SolrServerException("MetacatSolrIndex.query - There is no any authorized subjects(even the public user) in this query session.");
|
189
|
}
|
190
|
if(isEmbeddedSolrServer) {
|
191
|
return queryEmbedded(query, authorizedSubjects);
|
192
|
} else {
|
193
|
return queryHttp(query, authorizedSubjects);
|
194
|
}
|
195
|
}
|
196
|
/**
|
197
|
* Query a solr embedded server
|
198
|
* @param query the solr query
|
199
|
* @return the result as the InputStream
|
200
|
* @throws SolrServerException
|
201
|
* @throws ClassNotFoundException
|
202
|
* @throws SQLException
|
203
|
* @throws PropertyNotFoundException
|
204
|
* @throws SAXException
|
205
|
* @throws ParserConfigurationException
|
206
|
*/
|
207
|
private InputStream queryEmbedded(String query, Set<Subject>subjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException, ParserConfigurationException, SAXException {
|
208
|
InputStream inputStream = null;
|
209
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
210
|
solrParams = appendAccessFilterParams(solrParams, subjects);
|
211
|
String wt = solrParams.get(WT);
|
212
|
|
213
|
//reload the core before query. Only after reloading the core, the query result can reflect the change made in metacat-index module.
|
214
|
coreContainer.reload(collectionName);
|
215
|
|
216
|
// handle normal and skin-based queries
|
217
|
if (wt == null ||supportedWriterTypes.contains(wt)) {
|
218
|
// just handle as normal solr query
|
219
|
QueryResponse response = solrServer.query(solrParams);
|
220
|
inputStream = transformResults(solrParams, response, wt);
|
221
|
}
|
222
|
else {
|
223
|
// assume it is a skin name
|
224
|
String qformat = wt;
|
225
|
|
226
|
// perform the solr query using wt=XML
|
227
|
wt = SolrQueryResponseWriterFactory.XML;
|
228
|
ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
|
229
|
msp.set(WT, wt);
|
230
|
QueryResponse response = solrServer.query(msp);
|
231
|
inputStream = transformResults(msp, response, wt);
|
232
|
|
233
|
// apply the stylesheet (XML->HTML)
|
234
|
DBTransform transformer = new DBTransform();
|
235
|
String documentContent = IOUtils.toString(inputStream, "UTF-8");
|
236
|
String sourceType = "solr";
|
237
|
String targetType = "-//W3C//HTML//EN";
|
238
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
239
|
Writer writer = new OutputStreamWriter(baos , "UTF-8");
|
240
|
// TODO: include more params?
|
241
|
Hashtable<String, String[]> params = new Hashtable<String, String[]>();
|
242
|
params.put("qformat", new String[] {qformat});
|
243
|
transformer.transformXMLDocument(
|
244
|
documentContent ,
|
245
|
sourceType,
|
246
|
targetType ,
|
247
|
qformat,
|
248
|
writer,
|
249
|
params,
|
250
|
null //sessionid
|
251
|
);
|
252
|
|
253
|
// finally, get the HTML back
|
254
|
inputStream = new ByteArrayInputStream(baos.toByteArray());
|
255
|
}
|
256
|
|
257
|
return inputStream;
|
258
|
}
|
259
|
|
260
|
/*
|
261
|
* Query a http server. We directly build the url to send the http server.
|
262
|
* The reason we don't use the method is:
|
263
|
* QueryResponse response = solrServer.query(solrParams);
|
264
|
* When we transform the QueryReponse object to the InputStream object, we need to have SolrCore object
|
265
|
* which is not available for the SolrHttpServer.
|
266
|
*
|
267
|
*/
|
268
|
private InputStream queryHttp(String query, Set<Subject>subjects) throws IOException {
|
269
|
StringBuffer accessFilter = generateAccessFilterParamsString(subjects);
|
270
|
if(accessFilter != null && accessFilter.length() != 0) {
|
271
|
query = solrBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
|
272
|
}
|
273
|
URL url = new URL(query);
|
274
|
return url.openStream();
|
275
|
}
|
276
|
|
277
|
|
278
|
/*
|
279
|
* Append the access filter query to the params
|
280
|
*/
|
281
|
private SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
|
282
|
SolrParams append = null;
|
283
|
if(solrParams != null) {
|
284
|
StringBuffer query = generateAccessFilterParamsString(subjects);
|
285
|
if(query != null && query.length() != 0) {
|
286
|
log.info("=================== fq query is "+query.toString());
|
287
|
NamedList fq = new NamedList();
|
288
|
fq.add(FILTERQUERY, query.toString());
|
289
|
SolrParams fqParam = SolrParams.toSolrParams(fq);
|
290
|
append = new AppendedSolrParams(solrParams, fqParam);
|
291
|
} else {
|
292
|
append = solrParams;
|
293
|
}
|
294
|
}
|
295
|
return append;
|
296
|
}
|
297
|
|
298
|
private StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
|
299
|
StringBuffer query = new StringBuffer();
|
300
|
boolean first = true;
|
301
|
if(subjects != null) {
|
302
|
for(Subject subject : subjects) {
|
303
|
if(subject != null) {
|
304
|
String subjectName = subject.getValue();
|
305
|
if(subjectName != null && !subjectName.trim().equals("")) {
|
306
|
if(first) {
|
307
|
first = false;
|
308
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
309
|
if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
|
310
|
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
311
|
}
|
312
|
} else {
|
313
|
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
314
|
if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
|
315
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
316
|
}
|
317
|
}
|
318
|
}
|
319
|
|
320
|
}
|
321
|
|
322
|
}
|
323
|
}
|
324
|
return query;
|
325
|
}
|
326
|
|
327
|
|
328
|
/*
|
329
|
* Transform the Queryresponse to the InputStream
|
330
|
*/
|
331
|
private InputStream transformResults(SolrParams request, QueryResponse response, String wt) throws SolrServerException, IOException {
|
332
|
//InputStream stream = null;
|
333
|
QueryResponseWriter writer = SolrQueryResponseWriterFactory.generateResponseWriter(wt);
|
334
|
Writer results = new StringWriter();
|
335
|
SolrQueryResponse sResponse = new SolrQueryResponse();
|
336
|
sResponse.setAllValues(response.getResponse());
|
337
|
SolrCore core =coreContainer.getCore(collectionName);
|
338
|
writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
|
339
|
return new ByteArrayInputStream(results.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING));
|
340
|
}
|
341
|
|
342
|
|
343
|
}
|