Project

General

Profile

« Previous | Next » 

Revision 7680

Added by Jing Tao almost 11 years ago

Use the set of subjects to replace the user and groups for the solr query.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
31 31
import java.util.ArrayList;
32 32
import java.util.Calendar;
33 33
import java.util.Date;
34
import java.util.HashSet;
34 35
import java.util.List;
35 36
import java.util.Set;
36 37
import java.util.Timer;
......
1461 1462
			NotFound {
1462 1463
	    String user = Constants.SUBJECT_PUBLIC;
1463 1464
        String[] groups= null;
1465
        Set<Subject> subjects = null;
1464 1466
        if (session != null) {
1465 1467
            user = session.getSubject().getValue();
1466
            Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1468
            subjects = AuthUtils.authorizedClientSubjects(session);
1467 1469
            if (subjects != null) {
1468 1470
                List<String> groupList = new ArrayList<String>();
1469 1471
                for (Subject subject: subjects) {
......
1471 1473
                }
1472 1474
                groups = groupList.toArray(new String[0]);
1473 1475
            }
1476
        } else {
1477
            //add the public user subject to the set 
1478
            Subject subject = new Subject();
1479
            subject.setValue(Constants.SUBJECT_PUBLIC);
1480
            subjects = new HashSet();
1481
            subjects.add(subject);
1474 1482
        }
1483
        //System.out.println("====== user is "+user);
1484
        //System.out.println("====== groups are "+groups);
1475 1485
		if (engine != null && engine.equals(PATHQUERY)) {
1476 1486
			try {
1477 1487
				DBQuery queryobj = new DBQuery();
......
1487 1497
		    logMetacat.info("The query is ==================================== \n"+query);
1488 1498
		    try {
1489 1499
		        
1490
                return MetacatSolrIndex.getInstance().query(query, user, groups);
1500
                return MetacatSolrIndex.getInstance().query(query, subjects);
1491 1501
            } catch (Exception e) {
1492 1502
                // TODO Auto-generated catch block
1493 1503
                throw new ServiceFailure("Solr server error", e.getMessage());
src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
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
    }

Also available in: Unified diff