47 |
47 |
import org.apache.solr.client.solrj.SolrServerException;
|
48 |
48 |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
49 |
49 |
import org.apache.solr.client.solrj.response.QueryResponse;
|
|
50 |
import org.apache.solr.common.params.AppendedSolrParams;
|
50 |
51 |
import org.apache.solr.common.params.ModifiableSolrParams;
|
51 |
52 |
import org.apache.solr.common.params.SolrParams;
|
|
53 |
import org.apache.solr.common.util.NamedList;
|
52 |
54 |
import org.apache.solr.core.CoreContainer;
|
53 |
55 |
import org.apache.solr.core.SolrConfig;
|
54 |
56 |
import org.apache.solr.core.SolrCore;
|
... | ... | |
61 |
63 |
import org.apache.solr.response.QueryResponseWriter;
|
62 |
64 |
import org.apache.solr.response.RubyResponseWriter;
|
63 |
65 |
import org.apache.solr.response.SolrQueryResponse;
|
|
66 |
import org.apache.solr.response.VelocityResponseWriter;
|
64 |
67 |
import org.apache.solr.response.XMLResponseWriter;
|
65 |
68 |
import org.apache.solr.schema.IndexSchema;
|
66 |
69 |
import org.apache.solr.schema.SchemaField;
|
... | ... | |
102 |
105 |
private static final String VERSION = "3.4";
|
103 |
106 |
private static final String SOLRCONFDIR = "/conf";
|
104 |
107 |
private static final String SOLRSCHEMAFILEPATH = SOLRCONFDIR+"/schema.xml";
|
|
108 |
private static final String FILTERQUERY = "fq";
|
|
109 |
private static final String READPERMISSION = "readPermission";
|
|
110 |
private static final String OPENPARENTHESE = "(";
|
|
111 |
private static final String CLOSEPARENTHESE = ")";
|
|
112 |
private static final String COLON = ":";
|
|
113 |
private static final String OR = "OR";
|
105 |
114 |
//private static final String SOLRCONFIGPATH = "/conf/solrconfig.xml";
|
106 |
115 |
|
107 |
116 |
private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
|
108 |
117 |
private CoreContainer coreContainer = null;
|
109 |
118 |
private SolrServer solrServer = null;
|
110 |
|
private String wt = null;//specify the return format.
|
|
119 |
//private String wt = null;//specify the return format.
|
111 |
120 |
private String collectionName = null;
|
112 |
121 |
private String solrHomeDir = null;
|
113 |
122 |
private static MetacatSolrIndex solrIndex = null;
|
... | ... | |
165 |
174 |
* @throws SQLException
|
166 |
175 |
* @throws PropertyNotFoundException
|
167 |
176 |
*/
|
168 |
|
public InputStream query(String query) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
|
177 |
public InputStream query(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
169 |
178 |
InputStream inputStream = null;
|
170 |
179 |
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
171 |
|
wt = solrParams.get(WT);
|
|
180 |
solrParams = appendAccessFilterParams(solrParams, user, groups);
|
|
181 |
String wt = solrParams.get(WT);
|
172 |
182 |
|
173 |
183 |
// handle normal and skin-based queries
|
174 |
|
if (supportedWriterTypes.contains(wt)) {
|
|
184 |
if (wt == null ||supportedWriterTypes.contains(wt)) {
|
175 |
185 |
// just handle as normal solr query
|
176 |
186 |
QueryResponse response = solrServer.query(solrParams);
|
177 |
|
inputStream = transformResults(solrParams, response);
|
|
187 |
inputStream = transformResults(solrParams, response, wt);
|
178 |
188 |
}
|
179 |
189 |
else {
|
180 |
190 |
// assume it is a skin name
|
... | ... | |
185 |
195 |
ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
|
186 |
196 |
msp.set(WT, wt);
|
187 |
197 |
QueryResponse response = solrServer.query(msp);
|
188 |
|
inputStream = transformResults(msp, response);
|
|
198 |
inputStream = transformResults(msp, response, wt);
|
189 |
199 |
|
190 |
200 |
// apply the stylesheet (XML->HTML)
|
191 |
201 |
DBTransform transformer = new DBTransform();
|
... | ... | |
214 |
224 |
return inputStream;
|
215 |
225 |
}
|
216 |
226 |
|
|
227 |
|
|
228 |
/*
|
|
229 |
* Append the access filter query to the params
|
|
230 |
*/
|
|
231 |
private SolrParams appendAccessFilterParams(SolrParams solrParams, String user, String[] groups) {
|
|
232 |
SolrParams append = null;
|
|
233 |
if(solrParams != null) {
|
|
234 |
StringBuffer query = new StringBuffer();
|
|
235 |
if (user != null && groups != null) {
|
|
236 |
query.append(OPENPARENTHESE+READPERMISSION+COLON+user+CLOSEPARENTHESE);
|
|
237 |
for(int i=0; i<groups.length; i++) {
|
|
238 |
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+groups[i]+CLOSEPARENTHESE);
|
|
239 |
}
|
|
240 |
} else if (user != null && groups == null) {
|
|
241 |
query.append(OPENPARENTHESE+READPERMISSION+COLON+user+CLOSEPARENTHESE);
|
|
242 |
} else if ( user == null && groups != null) {
|
|
243 |
for (int i=0; i<groups.length; i++) {
|
|
244 |
if(i==0) {
|
|
245 |
query.append(OPENPARENTHESE+READPERMISSION+COLON+groups[i]+CLOSEPARENTHESE);
|
|
246 |
} else {
|
|
247 |
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+groups[i]+CLOSEPARENTHESE);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
|
251 |
if(query.length() != 0) {
|
|
252 |
//System.out.println("=================== fq query is "+query.toString());
|
|
253 |
NamedList fq = new NamedList();
|
|
254 |
fq.add(FILTERQUERY, query.toString());
|
|
255 |
SolrParams fqParam = SolrParams.toSolrParams(fq);
|
|
256 |
append = new AppendedSolrParams(solrParams, fqParam);
|
|
257 |
} else {
|
|
258 |
append = solrParams;
|
|
259 |
}
|
|
260 |
}
|
|
261 |
return append;
|
|
262 |
}
|
|
263 |
|
217 |
264 |
|
218 |
265 |
/*
|
219 |
266 |
* Transform the Queryresponse to the InputStream
|
220 |
267 |
*/
|
221 |
|
private InputStream transformResults(SolrParams request, QueryResponse response) throws SolrServerException, IOException {
|
|
268 |
private InputStream transformResults(SolrParams request, QueryResponse response, String wt) throws SolrServerException, IOException {
|
222 |
269 |
//InputStream stream = null;
|
223 |
|
QueryResponseWriter writer = generateResponseWriter();
|
|
270 |
QueryResponseWriter writer = generateResponseWriter(wt);
|
224 |
271 |
Writer results = new StringWriter();
|
225 |
272 |
SolrQueryResponse sResponse = new SolrQueryResponse();
|
226 |
273 |
sResponse.setAllValues(response.getResponse());
|
... | ... | |
240 |
287 |
* <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter"/>
|
241 |
288 |
* <queryResponseWriter name="csv" class="solr.CSVResponseWriter"/>
|
242 |
289 |
*/
|
243 |
|
private QueryResponseWriter generateResponseWriter() throws SolrServerException {
|
|
290 |
private QueryResponseWriter generateResponseWriter(String wt) throws SolrServerException {
|
244 |
291 |
QueryResponseWriter writer = null;
|
245 |
292 |
if(wt == null || wt.trim().equals("") || wt.equals(XML)) {
|
246 |
293 |
writer = new XMLResponseWriter();
|
... | ... | |
254 |
301 |
writer = new PHPResponseWriter();
|
255 |
302 |
} else if(wt.equals(PHPS)) {
|
256 |
303 |
writer = new PHPSerializedResponseWriter();
|
257 |
|
/*} else if(wt.equals(VELOCITY)) {
|
258 |
|
writer = new VelocityResponseWriter();*/
|
|
304 |
} else if(wt.equals(VELOCITY)) {
|
|
305 |
writer = new VelocityResponseWriter();
|
259 |
306 |
} else if(wt.equals(CSV)) {
|
260 |
307 |
writer = new CSVResponseWriter();
|
261 |
308 |
} else {
|
Add the access query filter.