Project

General

Profile

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

    
25
import java.io.ByteArrayInputStream;
26
import java.io.ByteArrayOutputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStreamWriter;
30
import java.io.Writer;
31
import java.sql.SQLException;
32
import java.util.Hashtable;
33

    
34
import java.util.Set;
35

    
36
import javax.xml.parsers.ParserConfigurationException;
37

    
38
import org.apache.commons.io.IOUtils;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.apache.solr.client.solrj.SolrServerException;
42
import org.apache.solr.common.params.ModifiableSolrParams;
43
import org.apache.solr.common.params.SolrParams;
44

    
45
import org.apache.solr.servlet.SolrRequestParsers;
46
import org.dataone.service.exceptions.NotFound;
47
import org.dataone.service.exceptions.NotImplemented;
48
import org.dataone.service.exceptions.UnsupportedType;
49
import org.dataone.service.types.v1.Subject;
50
import org.xml.sax.SAXException;
51

    
52
import edu.ucsb.nceas.metacat.DBTransform;
53
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
54
import edu.ucsb.nceas.metacat.common.query.SolrQueryService;
55
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
56
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
57
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
58

    
59

    
60
/**
61
 * This class will query the solr server and return the result.
62
 * @author tao
63
 *
64
 */
65
public class MetacatSolrIndex {
66
    
67
    
68
    //public static final String SOLRQUERY = "solr";
69
    //public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
70
    //public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
71
    //public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
72
    //public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
73
   
74
    
75
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
76
    private static MetacatSolrIndex  solrIndex = null;
77
    
78
    public static MetacatSolrIndex getInstance() throws Exception {
79
        if (solrIndex == null) {
80
            solrIndex = new MetacatSolrIndex();
81
        }
82
        return solrIndex;
83
    }
84
    
85
    /**
86
     * Constructor
87
     * @throws SAXException 
88
     * @throws IOException 
89
     * @throws ParserConfigurationException 
90
     */
91
    private MetacatSolrIndex() throws Exception {
92
    	
93
    }
94
    
95
    
96
    
97
    
98
    /**
99
     * Query the solr server
100
     * @param query  the solr query
101
     * @param authorizedSubjects the authorized subjects in this query session
102
     * @return the result as the InputStream
103
     * @throws SolrServerException 
104
     * @throws ClassNotFoundException 
105
     * @throws SQLException 
106
     * @throws PropertyNotFoundException 
107
     * @throws SAXException 
108
     * @throws ParserConfigurationException 
109
     * @throws UnsupportedType 
110
     * @throws NotFound 
111
     * @throws NotImplemented 
112
     */
113
    public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, 
114
    ClassNotFoundException, ParserConfigurationException, SAXException, NotImplemented, NotFound, UnsupportedType {
115
        if(authorizedSubjects == null || authorizedSubjects.isEmpty()) {
116
            throw new SolrServerException("MetacatSolrIndex.query - There is no any authorized subjects(even the public user) in this query session.");
117
        }
118
        InputStream inputStream = null;
119
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
120
        String wt = solrParams.get(SolrQueryService.WT);
121
        // handle normal and skin-based queries
122
        if (SolrQueryService.isSupportedWT(wt)) {
123
            // just handle as normal solr query
124
           
125
            inputStream = SolrQueryServiceController.getInstance().query(solrParams, authorizedSubjects);
126
        }
127
        else {
128
            // assume it is a skin name
129
            String qformat = wt;
130
            
131
            // perform the solr query using wt=XML
132
            wt = SolrQueryResponseWriterFactory.XML;
133
            ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
134
            msp.set(SolrQueryService.WT, wt);
135
            inputStream = SolrQueryServiceController.getInstance().query(msp, authorizedSubjects);
136
            
137
            // apply the stylesheet (XML->HTML)
138
            DBTransform transformer = new DBTransform();
139
            String documentContent = IOUtils.toString(inputStream, "UTF-8");
140
            String sourceType = "solr";
141
            String targetType = "-//W3C//HTML//EN";
142
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
143
            Writer writer = new OutputStreamWriter(baos , "UTF-8");
144
            // TODO: include more params?
145
            Hashtable<String, String[]> params = new Hashtable<String, String[]>();
146
            params.put("qformat", new String[] {qformat});
147
            transformer.transformXMLDocument(
148
                    documentContent , 
149
                    sourceType, 
150
                    targetType , 
151
                    qformat, 
152
                    writer, 
153
                    params, 
154
                    null //sessionid
155
                    );
156
            
157
            // finally, get the HTML back
158
            inputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
159
            ((ContentTypeByteArrayInputStream) inputStream).setContentType("text/html");
160
        }
161
        
162
        return inputStream;
163
     
164
    }
165

    
166
}
(4-4/4)