Project

General

Profile

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.InputStream;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34

    
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37
import org.apache.solr.common.params.AppendedSolrParams;
38
import org.apache.solr.common.params.SolrParams;
39
import org.apache.solr.common.util.NamedList;
40
import org.apache.solr.schema.SchemaField;
41
import org.dataone.service.types.v1.Subject;
42
import org.dataone.service.util.Constants;
43

    
44
import edu.ucsb.nceas.metacat.common.SolrQueryResponseWriterFactory;
45

    
46

    
47
/**
48
 * A query interface for the solr server
49
 * @author tao
50
 *
51
 */
52
public abstract class SolrQueryService {
53
    
54
    public static final String WT = "wt";//the property name to specify the return type
55
    
56
    protected static final String FILTERQUERY = "fq";
57
    private static final String READPERMISSION = "readPermission";
58
    private static final String RIGHTSHOLDER = "rightsHolder";
59
    private static final String OPENPARENTHESE = "(";
60
    private static final String CLOSEPARENTHESE = ")";
61
    private static final String COLON = ":";
62
    private static final String OR = "OR";
63
    
64
    private static Log log = LogFactory.getLog(SolrQueryService.class);
65
    private static List<String> supportedWriterTypes = null;
66
    static {
67
        supportedWriterTypes = new ArrayList<String>();
68
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
69
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
70
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
71
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
72
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
73
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
74
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
75
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
76
    }
77
    /**
78
     * Query the Solr server with specified query and user's identity. If the Subjects
79
     * is null, there will be no access rules for the query. This is the for the http solr server.
80
     * @param query the query string
81
     * @param subjects the user's identity which sent the query
82
     * @return the response
83
     * @throws Exception
84
     */
85
    //public abstract InputStream query(String query, Set<Subject>subjects) throws Exception;
86
    
87
    /**
88
     * Query the Solr server with specified query and user's identity. If the Subjects
89
     * is null, there will be no access rules for the query. This is for the embedded solr server.
90
     * @param query the query params. 
91
     * @param subjects the user's identity which sent the query
92
     * @return the response
93
     * @throws Exception
94
     */
95
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
96
    
97
    
98
  
99
    
100
    /**
101
     * Get the fields list of the index schema
102
     * @return
103
     * @throws Exception
104
     */
105
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
106
    
107
    /**
108
     * Get the version of the solr server.
109
     * @return
110
     */
111
    public abstract String getSolrServerVersion();
112
    
113
    
114
    
115
    /**
116
     * If the solr server supports the specified wt.
117
     * @param wt
118
     * @return true if it supports; otherwise false.
119
     */
120
    public static boolean isSupportedWT(String wt) {
121
        if (wt == null ||supportedWriterTypes.contains(wt)) {
122
            return true;
123
        } else {
124
            return false;
125
        }
126
    }
127
    
128
    /*
129
     * Append the access filter query to the params
130
     */
131
    protected SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
132
        SolrParams append = null;
133
        if(solrParams != null) {
134
            StringBuffer query = generateAccessFilterParamsString(subjects);      
135
            if(query != null && query.length() != 0) {
136
                log.info("=================== fq query is "+query.toString());
137
                NamedList fq = new NamedList();
138
                fq.add(FILTERQUERY, query.toString());
139
                SolrParams fqParam = SolrParams.toSolrParams(fq);
140
                append = new AppendedSolrParams(solrParams, fqParam);
141
            } else {
142
                append = solrParams;
143
            }
144
        }
145
        return append;
146
    }
147
    
148
    protected StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
149
        StringBuffer query = new StringBuffer();
150
        boolean first = true;
151
        if(subjects != null) {
152
            for(Subject subject : subjects) {
153
                if(subject != null) {
154
                    String subjectName = subject.getValue();
155
                    if(subjectName != null && !subjectName.trim().equals("")) {
156
                        if(first) {
157
                            first = false;
158
                            query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
159
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
160
                                query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
161
                            }
162
                        } else {
163
                            query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
164
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
165
                                query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
166
                            }
167
                        }
168
                    }
169
                   
170
                }
171
               
172
            }
173
        }
174
        return query;
175
    }
176
}
(3-3/4)