39 |
39 |
import java.util.Hashtable;
|
40 |
40 |
import java.util.List;
|
41 |
41 |
import java.util.Map;
|
|
42 |
import java.util.Set;
|
42 |
43 |
|
43 |
44 |
import javax.xml.parsers.ParserConfigurationException;
|
44 |
45 |
|
... | ... | |
72 |
73 |
import org.apache.solr.schema.SchemaField;
|
73 |
74 |
import org.apache.solr.servlet.SolrRequestParsers;
|
74 |
75 |
import org.dataone.configuration.Settings;
|
|
76 |
import org.dataone.service.types.v1.Subject;
|
75 |
77 |
import org.dataone.service.types.v1_1.QueryEngineDescription;
|
76 |
78 |
import org.dataone.service.types.v1_1.QueryField;
|
77 |
79 |
import org.dataone.service.util.Constants;
|
... | ... | |
178 |
180 |
/**
|
179 |
181 |
* Query the solr server
|
180 |
182 |
* @param query the solr query
|
|
183 |
* @param authorizedSubjects the authorized subjects in this query session
|
181 |
184 |
* @return the result as the InputStream
|
182 |
185 |
* @throws SolrServerException
|
183 |
186 |
* @throws ClassNotFoundException
|
184 |
187 |
* @throws SQLException
|
185 |
188 |
* @throws PropertyNotFoundException
|
186 |
189 |
*/
|
187 |
|
public InputStream query(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
|
190 |
public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
|
191 |
if(authorizedSubjects == null || authorizedSubjects.isEmpty()) {
|
|
192 |
throw new SolrServerException("MetacatSolrIndex.query - There is no any authorized subjects(even the public user) in this query session.");
|
|
193 |
}
|
188 |
194 |
if(isEmbeddedSolrServer) {
|
189 |
|
return queryEmbedded(query, user, groups);
|
|
195 |
return queryEmbedded(query, authorizedSubjects);
|
190 |
196 |
} else {
|
191 |
|
return queryHttp(query, user, groups);
|
|
197 |
return queryHttp(query, authorizedSubjects);
|
192 |
198 |
}
|
193 |
199 |
}
|
194 |
200 |
/**
|
... | ... | |
200 |
206 |
* @throws SQLException
|
201 |
207 |
* @throws PropertyNotFoundException
|
202 |
208 |
*/
|
203 |
|
private InputStream queryEmbedded(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
|
209 |
private InputStream queryEmbedded(String query, Set<Subject>subjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
|
204 |
210 |
InputStream inputStream = null;
|
205 |
211 |
SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
|
206 |
|
solrParams = appendAccessFilterParams(solrParams, user, groups);
|
|
212 |
solrParams = appendAccessFilterParams(solrParams, subjects);
|
207 |
213 |
String wt = solrParams.get(WT);
|
208 |
214 |
|
209 |
215 |
// handle normal and skin-based queries
|
... | ... | |
258 |
264 |
* which is not available for the SolrHttpServer.
|
259 |
265 |
*
|
260 |
266 |
*/
|
261 |
|
private InputStream queryHttp(String query, String user, String[] groups) throws IOException {
|
262 |
|
StringBuffer accessFilter = generateAccessFilterParamsString(user, groups);
|
|
267 |
private InputStream queryHttp(String query, Set<Subject>subjects) throws IOException {
|
|
268 |
StringBuffer accessFilter = generateAccessFilterParamsString(subjects);
|
263 |
269 |
if(accessFilter != null && accessFilter.length() != 0) {
|
264 |
270 |
query = solrBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
|
265 |
271 |
}
|
... | ... | |
271 |
277 |
/*
|
272 |
278 |
* Append the access filter query to the params
|
273 |
279 |
*/
|
274 |
|
private SolrParams appendAccessFilterParams(SolrParams solrParams, String user, String[] groups) {
|
|
280 |
private SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
|
275 |
281 |
SolrParams append = null;
|
276 |
282 |
if(solrParams != null) {
|
277 |
|
StringBuffer query = generateAccessFilterParamsString(user, groups);
|
|
283 |
StringBuffer query = generateAccessFilterParamsString(subjects);
|
278 |
284 |
if(query != null && query.length() != 0) {
|
279 |
285 |
log.info("=================== fq query is "+query.toString());
|
280 |
286 |
NamedList fq = new NamedList();
|
... | ... | |
288 |
294 |
return append;
|
289 |
295 |
}
|
290 |
296 |
|
291 |
|
private StringBuffer generateAccessFilterParamsString(String user, String[] groups) {
|
|
297 |
private StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
|
292 |
298 |
StringBuffer query = new StringBuffer();
|
293 |
|
if (user != null && groups != null) {
|
294 |
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
295 |
|
if(!user.equals(Constants.SUBJECT_PUBLIC)) {
|
296 |
|
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
297 |
|
}
|
298 |
|
for(int i=0; i<groups.length; i++) {
|
299 |
|
if(groups[i] != null) {
|
300 |
|
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
301 |
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
302 |
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
303 |
|
}
|
304 |
|
}
|
305 |
|
}
|
306 |
|
} else if (user != null && groups == null) {
|
307 |
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
308 |
|
if(!user.equals(Constants.SUBJECT_PUBLIC)) {
|
309 |
|
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+user+"\""+CLOSEPARENTHESE);
|
310 |
|
}
|
311 |
|
} else if ( user == null && groups != null) {
|
312 |
|
for (int i=0; i<groups.length; i++) {
|
313 |
|
if(groups[i]!= null) {
|
314 |
|
if(i==0) {
|
315 |
|
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
316 |
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
317 |
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
|
299 |
if(subjects != null) {
|
|
300 |
for(Subject subject : subjects) {
|
|
301 |
if(subject != null) {
|
|
302 |
String subjectName = subject.getValue();
|
|
303 |
if(subjectName != null && !subjectName.trim().equals("")) {
|
|
304 |
boolean first = true;
|
|
305 |
if(first) {
|
|
306 |
first = false;
|
|
307 |
query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
|
308 |
if(!subjectName.equals(Constants.SUBJECT_PUBLIC)) {
|
|
309 |
query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
318 |
310 |
}
|
319 |
311 |
} else {
|
320 |
|
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
321 |
|
if(!groups[i].equals(Constants.SUBJECT_PUBLIC)) {
|
322 |
|
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
|
|
312 |
query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
|
313 |
if(!subjectName.equals(Constants.SUBJECT_PUBLIC)) {
|
|
314 |
query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
|
323 |
315 |
}
|
324 |
316 |
}
|
325 |
317 |
}
|
326 |
|
|
|
318 |
|
327 |
319 |
}
|
|
320 |
|
|
321 |
}
|
328 |
322 |
}
|
329 |
323 |
return query;
|
330 |
324 |
}
|
Use the set of subjects to replace the user and groups for the solr query.