Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.common.query;
20

    
21
import java.io.InputStream;
22
import java.util.ArrayList;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Set;
26

    
27
import org.apache.commons.logging.Log;
28
import org.apache.commons.logging.LogFactory;
29
import org.apache.solr.common.params.AppendedSolrParams;
30
import org.apache.solr.common.params.SolrParams;
31
import org.apache.solr.common.util.NamedList;
32
import org.apache.solr.schema.IndexSchema;
33
import org.apache.solr.schema.SchemaField;
34
import org.dataone.service.types.v1.Subject;
35
import org.dataone.service.util.Constants;
36

    
37
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
38

    
39

    
40
/**
41
 * An abstract query service class for the solr server
42
 * @author tao
43
 *
44
 */
45
public abstract class SolrQueryService {
46
    
47
    public static final String WT = "wt";//the property name to specify the return type
48
    
49
    protected static final String FILTERQUERY = "fq";
50
    protected static final String UNKNOWN = "Unknown";
51
    private static final String READPERMISSION = "readPermission";
52
    private static final String IS_PUBLIC = "isPublic";
53
    private static final String RIGHTSHOLDER = "rightsHolder";
54
    private static final String OPENPARENTHESE = "(";
55
    private static final String CLOSEPARENTHESE = ")";
56
    private static final String COLON = ":";
57
    private static final String OR = "OR";
58
    
59
    private static Log log = LogFactory.getLog(SolrQueryService.class);
60
    private static List<String> supportedWriterTypes = null;
61
    
62
    protected IndexSchema schema = null;
63
    protected Map<String, SchemaField> fieldMap = null;
64
    protected List<String> validSolrFieldNames = null;
65
    protected String solrSpecVersion = null;
66
    
67
    static {
68
        supportedWriterTypes = new ArrayList<String>();
69
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
70
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
71
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
72
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
73
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
74
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
75
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
76
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
77
    }
78
  
79
    
80
    /**
81
     * Query the Solr server with specified query and user's identity. If the Subjects
82
     * is null, there will be no access rules for the query. This is for the embedded solr server.
83
     * @param query the query params. 
84
     * @param subjects the user's identity which sent the query
85
     * @return the response
86
     * @throws Exception
87
     */
88
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
89
    
90
    
91
  
92
    
93
    /**
94
     * Get the fields list of the index schema
95
     * @return
96
     * @throws Exception
97
     */
98
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
99
    
100
    public IndexSchema getSchema() {
101
		return schema;
102
	}
103

    
104
	/**
105
     * Get the version of the solr server.
106
     * @return
107
     */
108
    public abstract String getSolrServerVersion();
109
    
110
    /**
111
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
112
     * @return
113
     */
114
    protected List<String> getValidSchemaFields() {
115
        if (validSolrFieldNames != null && !validSolrFieldNames.isEmpty()) {
116
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
117
            return validSolrFieldNames;
118
        } else {
119
            validSolrFieldNames = new ArrayList<String>();
120
            if(fieldMap != null) {
121
                Set<String> fieldNames = fieldMap.keySet();
122
                for(String fieldName : fieldNames) {
123
                    SchemaField field = fieldMap.get(fieldName);
124
                    //remove the field which is the target field of a CopyField.
125
                    if(field != null && !schema.isCopyFieldTarget(field)) {
126
                         validSolrFieldNames.add(fieldName);
127
                    }
128
                }
129
            }
130
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
131
            return validSolrFieldNames;
132
        }
133
    }
134
    
135
    /**
136
     * If the solr server supports the specified wt.
137
     * @param wt
138
     * @return true if it supports; otherwise false.
139
     */
140
    public static boolean isSupportedWT(String wt) {
141
        if (wt == null ||supportedWriterTypes.contains(wt)) {
142
            return true;
143
        } else {
144
            return false;
145
        }
146
    }
147
    
148
    /*
149
     * Append the access filter query to the params
150
     */
151
    protected SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
152
        SolrParams append = null;
153
        if(solrParams != null) {
154
            StringBuffer query = generateAccessFilterParamsString(subjects);      
155
            if(query != null && query.length() != 0) {
156
                log.info("=================== fq query is "+query.toString());
157
                NamedList fq = new NamedList();
158
                fq.add(FILTERQUERY, query.toString());
159
                SolrParams fqParam = SolrParams.toSolrParams(fq);
160
                append = new AppendedSolrParams(solrParams, fqParam);
161
            } else {
162
                append = solrParams;
163
            }
164
        }
165
        return append;
166
    }
167
    
168
    protected StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
169
        StringBuffer query = new StringBuffer();
170
        boolean first = true;
171
        if(subjects != null) {
172
            for(Subject subject : subjects) {
173
                if(subject != null) {
174
                    String subjectName = subject.getValue();
175
                    if(subjectName != null && !subjectName.trim().equals("")) {
176
                        if(first) {
177
                            first = false;
178
                            query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
179
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
180
                                query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
181
                            } else if (subjectName.equals(Constants.SUBJECT_PUBLIC) {
182
                                query.append(OR+OPENPARENTHESE+IS_PUBLIC+COLON+"true"+CLOSEPARENTHESE);
183
                            }
184
                        } else {
185
                            query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
186
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
187
                                query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
188
                            } else if (subjectName.equals(Constants.SUBJECT_PUBLIC) {
189
                                query.append(OR+OPENPARENTHESE+IS_PUBLIC+COLON+"true"+CLOSEPARENTHESE);
190
                            }
191
                        }
192
                    }
193
                }
194
               
195
            }
196
        }
197
        return query;
198
    }
199
}
(6-6/7)