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.File;
|
27
|
import java.io.IOException;
|
28
|
import java.io.InputStream;
|
29
|
import java.io.StringWriter;
|
30
|
import java.io.Writer;
|
31
|
|
32
|
import javax.xml.parsers.ParserConfigurationException;
|
33
|
|
34
|
import org.apache.commons.logging.Log;
|
35
|
import org.apache.commons.logging.LogFactory;
|
36
|
import org.apache.solr.client.solrj.SolrServer;
|
37
|
import org.apache.solr.client.solrj.SolrServerException;
|
38
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
39
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
40
|
import org.apache.solr.common.params.SolrParams;
|
41
|
import org.apache.solr.core.CoreContainer;
|
42
|
import org.apache.solr.core.SolrCore;
|
43
|
import org.apache.solr.request.LocalSolrQueryRequest;
|
44
|
import org.apache.solr.response.CSVResponseWriter;
|
45
|
import org.apache.solr.response.JSONResponseWriter;
|
46
|
import org.apache.solr.response.PHPResponseWriter;
|
47
|
import org.apache.solr.response.PHPSerializedResponseWriter;
|
48
|
import org.apache.solr.response.PythonResponseWriter;
|
49
|
import org.apache.solr.response.QueryResponseWriter;
|
50
|
import org.apache.solr.response.RubyResponseWriter;
|
51
|
import org.apache.solr.response.SolrQueryResponse;
|
52
|
import org.apache.solr.response.XMLResponseWriter;
|
53
|
import org.apache.solr.servlet.SolrRequestParsers;
|
54
|
import org.dataone.configuration.Settings;
|
55
|
import org.xml.sax.SAXException;
|
56
|
|
57
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
58
|
|
59
|
|
60
|
/**
|
61
|
* This class will query the solr server and return the result.
|
62
|
* @author tao
|
63
|
*
|
64
|
*/
|
65
|
public class MetacatSolrIndex {
|
66
|
|
67
|
public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
|
68
|
public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
|
69
|
public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
|
70
|
public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
|
71
|
private static final String WT = "wt";//the property name to specify the return type
|
72
|
private static final String XML = "xml";
|
73
|
private static final String JSON = "json";
|
74
|
private static final String PYTHON = "python";
|
75
|
private static final String RUBY = "ruby";
|
76
|
private static final String PHP = "php";
|
77
|
private static final String PHPS = "phps";
|
78
|
private static final String VELOCITY = "velocity";
|
79
|
private static final String CSV ="csv";
|
80
|
|
81
|
private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
|
82
|
private CoreContainer coreContainer = null;
|
83
|
private SolrServer solrServer = null;
|
84
|
private String wt = null;//specify the return format.
|
85
|
private String collectionName = null;
|
86
|
|
87
|
/**
|
88
|
* Constructor
|
89
|
* @throws SAXException
|
90
|
* @throws IOException
|
91
|
* @throws ParserConfigurationException
|
92
|
*/
|
93
|
public MetacatSolrIndex() throws ParserConfigurationException, IOException, SAXException {
|
94
|
generateEmbeddedServer();
|
95
|
}
|
96
|
|
97
|
|
98
|
/*
|
99
|
* Generate the embedded solr server
|
100
|
*/
|
101
|
private void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException {
|
102
|
String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
|
103
|
log.info("The configured solr home from properties is " + solrHomeDir);
|
104
|
String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
|
105
|
File configFile = new File(solrHomeDir, configFileName);
|
106
|
coreContainer = new CoreContainer(solrHomeDir, configFile);
|
107
|
coreContainer.load(solrHomeDir, configFile);
|
108
|
collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
|
109
|
solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Query the solr server
|
114
|
* @param query the solr query
|
115
|
* @return the result as the InputStream
|
116
|
* @throws SolrServerException
|
117
|
*/
|
118
|
public InputStream query(String query) throws SolrServerException, IOException {
|
119
|
InputStream inputStream = null;
|
120
|
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
121
|
wt = solrParams.get(WT);
|
122
|
QueryResponse response = solrServer.query(solrParams);
|
123
|
inputStream = transformResults(solrParams, response);
|
124
|
return inputStream;
|
125
|
}
|
126
|
|
127
|
|
128
|
/*
|
129
|
* Transform the Queryresponse to the InputStream
|
130
|
*/
|
131
|
private InputStream transformResults(SolrParams request, QueryResponse response) throws SolrServerException, IOException {
|
132
|
//InputStream stream = null;
|
133
|
QueryResponseWriter writer = generateResponseWriter();
|
134
|
Writer results = new StringWriter();
|
135
|
SolrQueryResponse sResponse = new SolrQueryResponse();
|
136
|
sResponse.setAllValues(response.getResponse());
|
137
|
SolrCore core =coreContainer.getCore(collectionName);
|
138
|
writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
|
139
|
return new ByteArrayInputStream(results.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING));
|
140
|
}
|
141
|
|
142
|
|
143
|
/* Here is the list of the handler class to handle different format.
|
144
|
* <queryResponseWriter name="xml" default="true" class="solr.XMLResponseWriter" />
|
145
|
* <queryResponseWriter name="json" class="solr.JSONResponseWriter"/>
|
146
|
* <queryResponseWriter name="python" class="solr.PythonResponseWriter"/>
|
147
|
* <queryResponseWriter name="ruby" class="solr.RubyResponseWriter"/>
|
148
|
* <queryResponseWriter name="php" class="solr.PHPResponseWriter"/>
|
149
|
* <queryResponseWriter name="phps" class="solr.PHPSerializedResponseWriter"/>
|
150
|
* <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter"/>
|
151
|
* <queryResponseWriter name="csv" class="solr.CSVResponseWriter"/>
|
152
|
*/
|
153
|
private QueryResponseWriter generateResponseWriter() throws SolrServerException {
|
154
|
QueryResponseWriter writer = null;
|
155
|
if(wt == null || wt.trim().equals("") || wt.equals(XML)) {
|
156
|
writer = new XMLResponseWriter();
|
157
|
} else if(wt.equals(JSON)) {
|
158
|
writer = new JSONResponseWriter();
|
159
|
} else if(wt.equals(PYTHON)) {
|
160
|
writer = new PythonResponseWriter();
|
161
|
} else if(wt.equals(RUBY)) {
|
162
|
writer = new RubyResponseWriter();
|
163
|
} else if(wt.equals(PHP)) {
|
164
|
writer = new PHPResponseWriter();
|
165
|
} else if(wt.equals(PHPS)) {
|
166
|
writer = new PHPSerializedResponseWriter();
|
167
|
/*} else if(wt.equals(VELOCITY)) {
|
168
|
writer = new VelocityResponseWriter();*/
|
169
|
} else if(wt.equals(CSV)) {
|
170
|
writer = new CSVResponseWriter();
|
171
|
} else {
|
172
|
throw new SolrServerException("Metacat doesn't support this response format "+wt);
|
173
|
}
|
174
|
return writer;
|
175
|
}
|
176
|
}
|