1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class that gets Accession Number, check for uniqueness
|
4
|
* and register it into db
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova, Matt Jones
|
8
|
*
|
9
|
* '$Author: tao $'
|
10
|
* '$Date: 2013-04-19 17:47:14 -0700 (Fri, 19 Apr 2013) $'
|
11
|
* '$Revision: 7595 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
package edu.ucsb.nceas.metacat.common.query;
|
28
|
|
29
|
import java.io.IOException;
|
30
|
import java.io.InputStream;
|
31
|
import java.net.MalformedURLException;
|
32
|
import java.net.URL;
|
33
|
import java.util.List;
|
34
|
import java.util.Map;
|
35
|
import java.util.Set;
|
36
|
|
37
|
import javax.xml.parsers.DocumentBuilder;
|
38
|
import javax.xml.parsers.DocumentBuilderFactory;
|
39
|
import javax.xml.parsers.ParserConfigurationException;
|
40
|
import javax.xml.xpath.XPathConstants;
|
41
|
import javax.xml.xpath.XPathFactory;
|
42
|
|
43
|
import org.apache.commons.codec.net.URLCodec;
|
44
|
import org.apache.commons.logging.Log;
|
45
|
import org.apache.commons.logging.LogFactory;
|
46
|
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
|
47
|
import org.apache.solr.client.solrj.util.ClientUtils;
|
48
|
|
49
|
import org.apache.solr.common.params.SolrParams;
|
50
|
import org.apache.solr.schema.SchemaField;
|
51
|
import org.dataone.configuration.Settings;
|
52
|
import org.dataone.service.exceptions.NotFound;
|
53
|
import org.dataone.service.exceptions.NotImplemented;
|
54
|
import org.dataone.service.types.v1.Subject;
|
55
|
import org.w3c.dom.Document;
|
56
|
import org.w3c.dom.Node;
|
57
|
import org.w3c.dom.NodeList;
|
58
|
import org.xml.sax.SAXException;
|
59
|
|
60
|
|
61
|
|
62
|
/**
|
63
|
* The query service for the http solr server.
|
64
|
* @author tao
|
65
|
*
|
66
|
*/
|
67
|
public class HttpSolrQueryService extends SolrQueryService {
|
68
|
private static final String SELECTIONPHASE = "/select";
|
69
|
private static final String SOLR_SYSTEMINFO_URLAPPENDIX = "solr.systeminfo.urlappendix";
|
70
|
private static final String SOLR_SCHEMA_URLAPPENDIX = "sorl.schema.urlappendix";
|
71
|
private static final String SPEC_PATH = "//str[@name=\"solr-spec-version\"]";
|
72
|
|
73
|
private String solrServerBaseURL = null;
|
74
|
private CommonsHttpSolrServer httpSolrServer = null;
|
75
|
private static Log log = LogFactory.getLog(HttpSolrQueryService.class);
|
76
|
/**
|
77
|
* Constructor
|
78
|
* @param httpSolrServer
|
79
|
*/
|
80
|
public HttpSolrQueryService(CommonsHttpSolrServer httpSolrServer) {
|
81
|
if(httpSolrServer == null) {
|
82
|
throw new NullPointerException("HttpSolrQueryService.constructor - The httpSolrServer parameter can't be null");
|
83
|
}
|
84
|
this.httpSolrServer = httpSolrServer;
|
85
|
this.solrServerBaseURL = httpSolrServer.getBaseURL();
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Query the Solr server with specified query string and the user's identity. This is the for the http solr server.
|
90
|
* It is hard to transform the SolrQueryReponse object to the InputStream object for the HttpSolrServer
|
91
|
* since the transform needs the SolrCore. We have to open the solr url directly to get the InputStream.
|
92
|
* @param query the query string
|
93
|
* @param subjects the user's identity which sent the query
|
94
|
* @return the response
|
95
|
* @throws NotFound
|
96
|
* @throws IOException
|
97
|
* @throws Exception
|
98
|
*/
|
99
|
/*public InputStream query(String query, Set<Subject>subjects) throws NotFound, IOException {
|
100
|
StringBuffer accessFilter = generateAccessFilterParamsString(subjects);
|
101
|
if(accessFilter != null && accessFilter.length() != 0) {
|
102
|
query = solrServerBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
|
103
|
query = ClientUtils.escapeQueryChars(query);
|
104
|
} else {
|
105
|
throw new NotFound("0000", "HttpSolrQueryService.query - There is no identity (even user public) for the user who issued the query");
|
106
|
}
|
107
|
log.info("==========HttpSolrQueryService.query - the final url for querying the solr http server is "+query);
|
108
|
URL url = new URL(query);
|
109
|
|
110
|
return url.openStream();
|
111
|
}*/
|
112
|
|
113
|
/**
|
114
|
* Query the Solr server with specified query and user's identity.
|
115
|
* It is hard to transform the SolrQueryReponse object to the InputStream object for the HttpSolrServer
|
116
|
* since the transform needs the SolrCore. We have to open the solr url directly to get the InputStream.
|
117
|
* @param query the query params.
|
118
|
* @param subjects the user's identity which sent the query
|
119
|
* @return the response
|
120
|
* @throws IOException
|
121
|
* @throws NotFound
|
122
|
* @throws Exception
|
123
|
*/
|
124
|
public InputStream query(SolrParams query, Set<Subject>subjects) throws IOException, NotFound {
|
125
|
boolean xmlFormat = false;
|
126
|
String queryString = ClientUtils.toQueryString(query, xmlFormat);
|
127
|
log.info("==========HttpSolrQueryService.query - the query string after transforming from the SolrParams to the string "+queryString);
|
128
|
StringBuffer accessFilter = generateAccessFilterParamsString(subjects);
|
129
|
if(accessFilter != null && accessFilter.length() != 0) {
|
130
|
String accessStr = accessFilter.toString();
|
131
|
log.debug("==========HttpSolrQueryService.query - the access string is "+accessStr);
|
132
|
URLCodec urlCodec = new URLCodec();
|
133
|
accessStr = urlCodec.encode(accessStr, "UTF-8");
|
134
|
log.debug("==========HttpSolrQueryService.query - the access string after escape special characters string "+accessStr);
|
135
|
queryString = queryString+"&"+FILTERQUERY+"="+accessStr;
|
136
|
|
137
|
} else {
|
138
|
throw new NotFound("0000", "HttpSolrQueryService.query - There is no identity (even user public) for the user who issued the query");
|
139
|
}
|
140
|
|
141
|
|
142
|
//queryString = ClientUtils.escapeQueryChars(queryString);
|
143
|
queryString = solrServerBaseURL+SELECTIONPHASE+queryString;
|
144
|
log.info("==========HttpSolrQueryService.query - the final url for querying the solr http server is "+queryString);
|
145
|
URL url = new URL(queryString);
|
146
|
return url.openStream();
|
147
|
//throw new NotImplemented("0000", "HttpSolrQueryService - the method of query(SolrParams query, Set<Subject>subjects) is not for the HttpSolrQueryService. We donot need to implemente it");
|
148
|
}
|
149
|
|
150
|
|
151
|
|
152
|
|
153
|
/**
|
154
|
* Get the fields list of the index schema
|
155
|
* @return
|
156
|
* @throws Exception
|
157
|
*/
|
158
|
public Map<String, SchemaField> getIndexSchemaFields() throws Exception {
|
159
|
return null;
|
160
|
}
|
161
|
|
162
|
|
163
|
|
164
|
/**
|
165
|
* Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
|
166
|
* @return
|
167
|
*/
|
168
|
public List<String> getValidSchemaField() {
|
169
|
return null;
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Get the version of the solr server.
|
174
|
* @return
|
175
|
*/
|
176
|
public String getSolrServerVersion() {
|
177
|
if(solrSpecVersion == null) {
|
178
|
getHttpSolrServerVersion();
|
179
|
}
|
180
|
return solrSpecVersion;
|
181
|
}
|
182
|
|
183
|
|
184
|
/*
|
185
|
* Get the solr server version from the system information url.
|
186
|
*/
|
187
|
private void getHttpSolrServerVersion() {
|
188
|
String systemInfoUrlAppendix = Settings.getConfiguration().getString(SOLR_SYSTEMINFO_URLAPPENDIX);
|
189
|
String systemInfoUrl = solrServerBaseURL+systemInfoUrlAppendix;
|
190
|
try {
|
191
|
Document doc = transformInputStreamToDoc((new URL(systemInfoUrl)).openStream());
|
192
|
NodeList nodeList = (NodeList) XPathFactory.newInstance().newXPath()
|
193
|
.evaluate(SPEC_PATH, doc, XPathConstants.NODESET);
|
194
|
if(nodeList != null && nodeList.getLength() >0) {
|
195
|
Node specNode = nodeList.item(0);
|
196
|
solrSpecVersion = specNode.getFirstChild().getNodeValue();
|
197
|
} else {
|
198
|
solrSpecVersion = UNKNOWN;
|
199
|
}
|
200
|
|
201
|
} catch (Exception e) {
|
202
|
log.error("HttpSolrQueryService.getHttpSolrServerVersion - can't get the solr specification version since "+e.getMessage());
|
203
|
solrSpecVersion = UNKNOWN;
|
204
|
}
|
205
|
|
206
|
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Generate a Document from the InputStream
|
211
|
* @param input
|
212
|
* @return
|
213
|
* @throws ParserConfigurationException
|
214
|
* @throws SAXException
|
215
|
* @throws IOException
|
216
|
*/
|
217
|
private Document transformInputStreamToDoc(InputStream input) throws ParserConfigurationException, SAXException, IOException {
|
218
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
219
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
220
|
Document doc = dBuilder.parse(input);
|
221
|
return doc;
|
222
|
}
|
223
|
|
224
|
|
225
|
}
|