1
|
/**
|
2
|
* Copyright: 2013 Regents of the University of California and the
|
3
|
* National Center for Ecological Analysis and Synthesis
|
4
|
*
|
5
|
* This program is free software; you can redistribute it and/or modify
|
6
|
* it under the terms of the GNU General Public License as published by
|
7
|
* the Free Software Foundation; either version 2 of the License, or
|
8
|
* (at your option) any later version.
|
9
|
*
|
10
|
* This program is distributed in the hope that it will be useful,
|
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
* GNU General Public License for more details.
|
14
|
*
|
15
|
* You should have received a copy of the GNU General Public License
|
16
|
* along with this program; if not, write to the Free Software
|
17
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
*/
|
19
|
package edu.ucsb.nceas.metacat.common.query;
|
20
|
|
21
|
import java.io.IOException;
|
22
|
import java.io.InputStream;
|
23
|
import java.io.StringWriter;
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.List;
|
26
|
import java.util.Map;
|
27
|
import java.util.Set;
|
28
|
|
29
|
import javax.xml.parsers.ParserConfigurationException;
|
30
|
|
31
|
import org.apache.solr.client.solrj.SolrServerException;
|
32
|
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
|
33
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
34
|
import org.apache.solr.common.params.SolrParams;
|
35
|
import org.apache.solr.common.util.XML;
|
36
|
import org.apache.solr.core.CoreContainer;
|
37
|
import org.apache.solr.core.SolrCore;
|
38
|
import org.apache.solr.schema.IndexSchema;
|
39
|
import org.apache.solr.schema.SchemaField;
|
40
|
import org.dataone.service.exceptions.NotFound;
|
41
|
import org.dataone.service.exceptions.NotImplemented;
|
42
|
import org.dataone.service.exceptions.UnsupportedType;
|
43
|
import org.dataone.service.types.v1.Subject;
|
44
|
import org.xml.sax.SAXException;
|
45
|
|
46
|
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseTransformer;
|
47
|
|
48
|
|
49
|
/**
|
50
|
*The query service of the embedded solr server.
|
51
|
* @author tao
|
52
|
*
|
53
|
*/
|
54
|
public class EmbeddedSolrQueryService extends SolrQueryService {
|
55
|
private EmbeddedSolrServer solrServer = null;
|
56
|
private CoreContainer coreContainer = null;
|
57
|
private String collectionName = null;
|
58
|
private SolrCore solrCore = null;
|
59
|
|
60
|
|
61
|
/**
|
62
|
* Constructor.
|
63
|
* @param solrServer
|
64
|
* @param solrCore
|
65
|
* @throws NotFound
|
66
|
*/
|
67
|
public EmbeddedSolrQueryService(EmbeddedSolrServer solrServer, CoreContainer coreContainer, String collectionName) throws NotFound {
|
68
|
if(solrServer == null) {
|
69
|
throw new NullPointerException("EmbeddedSolrQueryService.constructor - the EmbeddedSolrServer parameter can't be null.");
|
70
|
}
|
71
|
if(coreContainer == null) {
|
72
|
throw new NullPointerException("EmbeddedSolrQueryService.constructor - the CoreContainer parameter can't be null.");
|
73
|
}
|
74
|
if(collectionName == null || collectionName.trim().equals("")) {
|
75
|
throw new NullPointerException("EmbeddedSolrQueryService.constructor - the name of Collection parameter can't be null or empty.");
|
76
|
}
|
77
|
this.solrServer = solrServer;
|
78
|
this.coreContainer = coreContainer;
|
79
|
this.collectionName = collectionName;
|
80
|
this.solrCore = this.coreContainer.getCore(collectionName);
|
81
|
if(solrCore == null) {
|
82
|
throw new NotFound("0000","EmbeddedSolrQueryService.constructor - There is no SolrCore named "+collectionName+".");
|
83
|
}
|
84
|
schema = solrCore.getSchema();
|
85
|
fieldMap = schema.getFields();
|
86
|
}
|
87
|
/**
|
88
|
* Query the Solr server with specified query and user's identity. If the Subjects
|
89
|
* is null, there will be no access rules for the query. This is the for the http solr server.
|
90
|
* @param query the query string
|
91
|
* @param subjects the user's identity which sent the query
|
92
|
* @return the response
|
93
|
* @throws Exception
|
94
|
*/
|
95
|
/*public InputStream query(String query, Set<Subject>subjects) throws Exception {
|
96
|
throw new NotImplemented("0000", "EmbeddSolrQueryService - the method of query(String query, Set<Subject>subjects) is not for the EmbeddedSolrServer. We donot need to implemente it");
|
97
|
}*/
|
98
|
|
99
|
/**
|
100
|
* Query the Solr server with specified query and user's identity. If the Subjects
|
101
|
* is null, there will be no access rules for the query. This is for the embedded solr server.
|
102
|
* @param query the query params.
|
103
|
* @param subjects the user's identity which sent the query
|
104
|
* @return the response
|
105
|
* @throws SAXException
|
106
|
* @throws IOException
|
107
|
* @throws ParserConfigurationException
|
108
|
* @throws SolrServerException
|
109
|
* @throws UnsupportedType
|
110
|
* @throws Exception
|
111
|
*/
|
112
|
public InputStream query(SolrParams query, Set<Subject>subjects) throws ParserConfigurationException, IOException, SAXException, SolrServerException, UnsupportedType {
|
113
|
InputStream inputStream = null;
|
114
|
String wt = query.get(WT);
|
115
|
query = appendAccessFilterParams(query, subjects);
|
116
|
SolrQueryResponseTransformer solrTransformer = new SolrQueryResponseTransformer(solrCore);
|
117
|
// handle normal and skin-based queries
|
118
|
if (isSupportedWT(wt)) {
|
119
|
// just handle as normal solr query
|
120
|
//reload the core before query. Only after reloading the core, the query result can reflect the change made in metacat-index module.
|
121
|
coreContainer.reload(collectionName);
|
122
|
QueryResponse response = solrServer.query(query);
|
123
|
inputStream = solrTransformer.transformResults(query, response, wt);
|
124
|
} else {
|
125
|
throw new UnsupportedType("0000","EmbeddSolrQueryService.query - the wt type "+wt+" in the solr query is not supported");
|
126
|
}
|
127
|
return inputStream;
|
128
|
}
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|
/**
|
134
|
* Get the fields map of the index schema
|
135
|
* @return the fields map (the field name is the key and the SchemaField is the value).
|
136
|
*/
|
137
|
public Map<String, SchemaField> getIndexSchemaFields() {
|
138
|
return fieldMap;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
|
143
|
* @return
|
144
|
*/
|
145
|
public List<String> getValidSchemaField() {
|
146
|
return super.getValidSchemaFields();
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Get the version of the solr server.
|
151
|
* @return
|
152
|
*/
|
153
|
public String getSolrServerVersion() {
|
154
|
if( solrSpecVersion !=null ) {
|
155
|
return solrSpecVersion;
|
156
|
} else {
|
157
|
Package p = SolrCore.class.getPackage();
|
158
|
StringWriter tmp = new StringWriter();
|
159
|
solrSpecVersion = p.getSpecificationVersion();
|
160
|
if (null != solrSpecVersion) {
|
161
|
try {
|
162
|
XML.escapeCharData(solrSpecVersion, tmp);
|
163
|
solrSpecVersion = tmp.toString();
|
164
|
} catch (IOException e) {
|
165
|
e.printStackTrace();
|
166
|
}
|
167
|
}
|
168
|
if (solrSpecVersion == null || solrSpecVersion.trim().equals("")) {
|
169
|
solrSpecVersion = UNKNOWN;
|
170
|
}
|
171
|
return solrSpecVersion;
|
172
|
}
|
173
|
}
|
174
|
}
|