Changeset 4182


Ignore:
Timestamp:
12/12/13 14:38:00 (10 years ago)
Author:
keeloo
Message:

First part of solr server input sanitising. Preferably to be included in the 2.18 release.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vlo/trunk/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/dao/SolrDao.java

    r2768 r4182  
    33import eu.clarin.cmdi.vlo.config.VloConfig;
    44import java.net.MalformedURLException;
     5import java.util.Arrays;
     6import java.util.HashSet;
     7import java.util.Set;
    58import org.apache.solr.client.solrj.SolrQuery;
    69import org.apache.solr.client.solrj.SolrServerException;
     
    3134        return solrServer;
    3235    }
     36   
     37    /**
     38     * Basic sanitising of Solr queries.
     39     *
     40     * Query is based on the URL to the VLO web application. Also, explain
     41     * about the URL and ?fq=language:dutch
     42     * Assume filters have the form a:b
     43     * like for example language:dutch
     44     *
     45     * @param query
     46     * @return
     47     */
     48    private SolrQuery sanitise (SolrQuery query){
     49       
     50        // String [] facetsFromConfig;
     51       
     52        // try and get the filters facets from the query
     53        String [] filtersInQuery;
     54        filtersInQuery = query.getFilterQueries();
     55 
     56        if (filtersInQuery == null) {
     57            // the query does not contain filters
     58        } else {
     59            // get the facets from the configuration file
     60            // facetsFromConfig = VloConfig.getFacetFields();
     61
     62            // present the facets from the config file as a list to a new set
     63            Set<String> facetsDefined;
     64            facetsDefined = new HashSet<String>(Arrays.asList(VloConfig.getFacetFields()));
     65
     66            // check the filters in the query by name
     67            for (String filter : filtersInQuery) {
     68                // split up a filter, look at the string preceeding the semicolon
     69                String facetInFilter = filter.split(":") [0];
     70               
     71                if (facetsDefined.contains(facetInFilter)) {
     72                    // facet in the filter is in the set that is defined by the config file
     73                } else {
     74                    if (facetInFilter.startsWith("_")) {
     75                        // this facet is hidden, do not consider it
     76                    } else {
     77                        // the filter name does not match a facet in the facet
     78                        query.removeFilterQuery(filter);
     79                    }
     80                }
     81            }
     82        }
     83
     84        // finally, return the sanitised query
     85        return query;
     86    }
    3387
    3488    protected QueryResponse fireQuery(SolrQuery query) {
     89        SolrQuery sanitisedQuery;
     90        sanitisedQuery = sanitise(query);
    3591        try {
    36             return solrServer.query(query);
     92            return solrServer.query(sanitisedQuery);
    3793        } catch (SolrServerException e) {
    3894            LOG.error("Error getting data:", e);
Note: See TracChangeset for help on using the changeset viewer.