Project

General

Profile

« Previous | Next » 

Revision 7648

Added by Jing Tao over 11 years ago

Add the access query filter.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
1458 1458
	public InputStream query(String engine, String query) throws InvalidToken,
1459 1459
			ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented,
1460 1460
			NotFound {
1461
	    String user = Constants.SUBJECT_PUBLIC;
1462
        String[] groups= null;
1463
        if (session != null) {
1464
            user = session.getSubject().getValue();
1465
            Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1466
            if (subjects != null) {
1467
                List<String> groupList = new ArrayList<String>();
1468
                for (Subject subject: subjects) {
1469
                    groupList.add(subject.getValue());
1470
                }
1471
                groups = groupList.toArray(new String[0]);
1472
            }
1473
        }
1461 1474
		if (engine != null && engine.equals(PATHQUERY)) {
1462 1475
			try {
1463 1476
				DBQuery queryobj = new DBQuery();
1464
				String user = Constants.SUBJECT_PUBLIC;
1465
				String[] groups= null;
1466
				if (session != null) {
1467
					user = session.getSubject().getValue();
1468
					Set<Subject> subjects = AuthUtils.authorizedClientSubjects(session);
1469
					if (subjects != null) {
1470
						List<String> groupList = new ArrayList<String>();
1471
						for (Subject subject: subjects) {
1472
							groupList.add(subject.getValue());
1473
						}
1474
						groups = groupList.toArray(new String[0]);
1475
					}
1476
				}
1477
				
1477 1478
				String results = queryobj.performPathquery(query, user, groups);
1478 1479
				return new ByteArrayInputStream(results.getBytes(MetaCatServlet.DEFAULT_ENCODING));
1479 1480

  
......
1485 1486
		    logMetacat.info("The query is ==================================== \n"+query);
1486 1487
		    try {
1487 1488
		        
1488
                return MetacatSolrIndex.getInstance().query(query);
1489
                return MetacatSolrIndex.getInstance().query(query, user, groups);
1489 1490
            } catch (Exception e) {
1490 1491
                // TODO Auto-generated catch block
1491 1492
                throw new ServiceFailure("Solr server error", e.getMessage());
src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
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 {

Also available in: Unified diff