Project

General

Profile

1 7609 tao
/**
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 7647 leinfelder
import java.io.ByteArrayOutputStream;
27 7609 tao
import java.io.IOException;
28
import java.io.InputStream;
29 7647 leinfelder
import java.io.OutputStreamWriter;
30 7609 tao
import java.io.Writer;
31 7647 leinfelder
import java.sql.SQLException;
32
import java.util.Hashtable;
33 7725 tao
34 7680 tao
import java.util.Set;
35 7609 tao
36 7620 tao
import javax.xml.parsers.ParserConfigurationException;
37
38 7647 leinfelder
import org.apache.commons.io.IOUtils;
39 7609 tao
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.apache.solr.client.solrj.SolrServerException;
42 7647 leinfelder
import org.apache.solr.common.params.ModifiableSolrParams;
43 7609 tao
import org.apache.solr.common.params.SolrParams;
44 7725 tao
45 7609 tao
import org.apache.solr.servlet.SolrRequestParsers;
46 7718 tao
import org.dataone.service.exceptions.NotFound;
47
import org.dataone.service.exceptions.NotImplemented;
48
import org.dataone.service.exceptions.UnsupportedType;
49 7680 tao
import org.dataone.service.types.v1.Subject;
50 7620 tao
import org.xml.sax.SAXException;
51 7609 tao
52 7647 leinfelder
import edu.ucsb.nceas.metacat.DBTransform;
53 7725 tao
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
54 7718 tao
import edu.ucsb.nceas.metacat.common.query.SolrQueryService;
55
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
56 7757 leinfelder
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
57 7647 leinfelder
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
58 7609 tao
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 7634 tao
68 7772 tao
    //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 7718 tao
74 7707 tao
75 7609 tao
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
76 7634 tao
    private static MetacatSolrIndex  solrIndex = null;
77 7609 tao
78 7664 tao
    public static MetacatSolrIndex getInstance() throws Exception {
79 7634 tao
        if (solrIndex == null) {
80
            solrIndex = new MetacatSolrIndex();
81
        }
82
        return solrIndex;
83
    }
84
85 7609 tao
    /**
86
     * Constructor
87 7620 tao
     * @throws SAXException
88
     * @throws IOException
89
     * @throws ParserConfigurationException
90 7609 tao
     */
91 7664 tao
    private MetacatSolrIndex() throws Exception {
92 7718 tao
93 7609 tao
    }
94
95
96
97 7718 tao
98 7609 tao
    /**
99
     * Query the solr server
100
     * @param query  the solr query
101 7680 tao
     * @param authorizedSubjects the authorized subjects in this query session
102 7609 tao
     * @return the result as the InputStream
103
     * @throws SolrServerException
104 7647 leinfelder
     * @throws ClassNotFoundException
105
     * @throws SQLException
106
     * @throws PropertyNotFoundException
107 7685 tao
     * @throws SAXException
108
     * @throws ParserConfigurationException
109 7718 tao
     * @throws UnsupportedType
110
     * @throws NotFound
111
     * @throws NotImplemented
112 7609 tao
     */
113 7718 tao
    public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException,
114
    ClassNotFoundException, ParserConfigurationException, SAXException, NotImplemented, NotFound, UnsupportedType {
115 7680 tao
        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 7609 tao
        InputStream inputStream = null;
119
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
120 7718 tao
        String wt = solrParams.get(SolrQueryService.WT);
121 7647 leinfelder
        // handle normal and skin-based queries
122 7718 tao
        if (SolrQueryService.isSupportedWT(wt)) {
123
            // just handle as normal solr query
124
125 7720 tao
            inputStream = SolrQueryServiceController.getInstance().query(solrParams, authorizedSubjects);
126 7647 leinfelder
        }
127
        else {
128 7718 tao
            // 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 7720 tao
            inputStream = SolrQueryServiceController.getInstance().query(msp, authorizedSubjects);
136 7718 tao
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 7757 leinfelder
            inputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
159
            ((ContentTypeByteArrayInputStream) inputStream).setContentType("text/html");
160 7647 leinfelder
        }
161
162 7609 tao
        return inputStream;
163 7718 tao
164 7609 tao
    }
165 7718 tao
166 7609 tao
}