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.IOException;
30
import java.io.InputStream;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Set;
36

    
37
import org.apache.commons.logging.Log;
38
import org.apache.commons.logging.LogFactory;
39
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
40

    
41
import org.apache.solr.common.params.SolrParams;
42
import org.apache.solr.schema.SchemaField;
43
import org.dataone.cn.indexer.solrhttp.HTTPService;
44
import org.dataone.cn.indexer.solrhttp.SolrDoc;
45
import org.dataone.service.exceptions.NotFound;
46
import org.dataone.service.exceptions.NotImplemented;
47
import org.dataone.service.types.v1.Subject;
48

    
49

    
50

    
51
/**
52
 * The query service for the http solr server.
53
 * @author tao
54
 *
55
 */
56
public class HttpSolrQueryService extends SolrQueryService {
57
    
58
    private String solrServerBaseURL = null;
59
    private CommonsHttpSolrServer httpSolrServer = null;
60
    private static Log log = LogFactory.getLog(HttpSolrQueryService.class);
61
    /**
62
     * Constructor
63
     * @param httpSolrServer
64
     */
65
    public HttpSolrQueryService(CommonsHttpSolrServer httpSolrServer) {
66
        if(httpSolrServer == null) {
67
            throw new NullPointerException("HttpSolrQueryService.constructor - The httpSolrServer parameter can't be null");
68
        }
69
        this.httpSolrServer = httpSolrServer;
70
        this.solrServerBaseURL = httpSolrServer.getBaseURL();
71
    }
72
    
73
    /**
74
     * Query the Solr server with specified query string and the user's identity. This is the for the http solr server.
75
     * It is hard to transform the SolrQueryReponse object to the InputStream object for the HttpSolrServer
76
     * since the transform needs the SolrCore. We have to open the solr url directly to get the InputStream.
77
     * @param query the query string
78
     * @param subjects the user's identity which sent the query
79
     * @return the response
80
     * @throws NotFound 
81
     * @throws IOException 
82
     * @throws Exception
83
     */
84
    public InputStream query(String query, Set<Subject>subjects) throws NotFound, IOException {
85
        StringBuffer accessFilter = generateAccessFilterParamsString(subjects);
86
        if(accessFilter != null && accessFilter.length() != 0) {
87
            query = solrServerBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
88
            query = HTTPService.escapeQueryChars(query);
89
        } else {
90
            throw new NotFound("0000", "HttpSolrQueryService.query - There is no identity (even user public) for the user who issued the query");
91
        }
92
        log.info("==========HttpSolrQueryService.query - the final url for querying the solr http server is "+query);
93
        URL url = new URL(query);
94
        
95
        return url.openStream();
96
    }
97
    
98
    /**
99
     * Query the Solr server with specified query and user's identity. If the Subjects
100
     * is null, there will be no access rules for the query. This is for the embedded solr server.
101
     * @param query the query params. 
102
     * @param subjects the user's identity which sent the query
103
     * @return the response
104
     * @throws Exception
105
     */
106
    public  InputStream query(SolrParams query, Set<Subject>subjects) throws Exception {
107
        throw new NotImplemented("0000", "HttpSolrQueryService - the method of  query(SolrParams query, Set<Subject>subjects) is not for the HttpSolrQueryService. We donot need to implemente it");
108
    }
109
    
110
    
111
    /**
112
     * Get the list of SolrDocs for the specified ids from the solr server.
113
     *  * Note: each id only can have one SolrDoc or null SolrDoc. If it is null, it
114
     * wouldn't be added to the list.
115
     * @param ids the specified ids
116
     * @return the SolrDocs of the ids.
117
     * @throws Exception
118
     */
119
    public  List<SolrDoc> query(List<String> ids) throws Exception {
120
        return null;
121
    }
122
    
123
    
124
    /**
125
     * Get the fields list of the index schema
126
     * @return
127
     * @throws Exception
128
     */
129
    public  Map<String, SchemaField> getIndexSchemaFields() throws Exception {
130
        return null;
131
    }
132
    
133
    /**
134
     * Get the version of the solr server.
135
     * @return
136
     */
137
    public String getSolrServerVersion() {
138
        return null;
139
    }
140
}
(2-2/4)