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 8464 leinfelder
import java.util.List;
34
import java.util.Map;
35 7725 tao
36 7680 tao
import java.util.Set;
37 7609 tao
38 7620 tao
import javax.xml.parsers.ParserConfigurationException;
39
40 7647 leinfelder
import org.apache.commons.io.IOUtils;
41 7609 tao
import org.apache.commons.logging.Log;
42
import org.apache.commons.logging.LogFactory;
43
import org.apache.solr.client.solrj.SolrServerException;
44 7647 leinfelder
import org.apache.solr.common.params.ModifiableSolrParams;
45 7609 tao
import org.apache.solr.common.params.SolrParams;
46 7725 tao
47 7609 tao
import org.apache.solr.servlet.SolrRequestParsers;
48 7718 tao
import org.dataone.service.exceptions.NotFound;
49
import org.dataone.service.exceptions.NotImplemented;
50
import org.dataone.service.exceptions.UnsupportedType;
51 8464 leinfelder
import org.dataone.service.types.v1.Identifier;
52 7680 tao
import org.dataone.service.types.v1.Subject;
53 8464 leinfelder
import org.dataone.service.types.v1.SystemMetadata;
54 7620 tao
import org.xml.sax.SAXException;
55 7609 tao
56 7647 leinfelder
import edu.ucsb.nceas.metacat.DBTransform;
57 8464 leinfelder
import edu.ucsb.nceas.metacat.common.index.IndexTask;
58 7725 tao
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
59 7718 tao
import edu.ucsb.nceas.metacat.common.query.SolrQueryService;
60
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
61 7757 leinfelder
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
62 8464 leinfelder
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
63 7647 leinfelder
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
64 7609 tao
65
66
/**
67
 * This class will query the solr server and return the result.
68
 * @author tao
69
 *
70
 */
71
public class MetacatSolrIndex {
72
73 7634 tao
74 7772 tao
    //public static final String SOLRQUERY = "solr";
75
    //public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
76
    //public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
77
    //public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
78
    //public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
79 7718 tao
80 7707 tao
81 7609 tao
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
82 7634 tao
    private static MetacatSolrIndex  solrIndex = null;
83 7609 tao
84 7664 tao
    public static MetacatSolrIndex getInstance() throws Exception {
85 7634 tao
        if (solrIndex == null) {
86
            solrIndex = new MetacatSolrIndex();
87
        }
88
        return solrIndex;
89
    }
90
91 7609 tao
    /**
92
     * Constructor
93 7620 tao
     * @throws SAXException
94
     * @throws IOException
95
     * @throws ParserConfigurationException
96 7609 tao
     */
97 7664 tao
    private MetacatSolrIndex() throws Exception {
98 7718 tao
99 7609 tao
    }
100
101
102
103 7718 tao
104 7609 tao
    /**
105
     * Query the solr server
106
     * @param query  the solr query
107 7680 tao
     * @param authorizedSubjects the authorized subjects in this query session
108 7609 tao
     * @return the result as the InputStream
109
     * @throws SolrServerException
110 7647 leinfelder
     * @throws ClassNotFoundException
111
     * @throws SQLException
112
     * @throws PropertyNotFoundException
113 7685 tao
     * @throws SAXException
114
     * @throws ParserConfigurationException
115 7718 tao
     * @throws UnsupportedType
116
     * @throws NotFound
117
     * @throws NotImplemented
118 7609 tao
     */
119 7718 tao
    public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException,
120
    ClassNotFoundException, ParserConfigurationException, SAXException, NotImplemented, NotFound, UnsupportedType {
121 7680 tao
        if(authorizedSubjects == null || authorizedSubjects.isEmpty()) {
122
            throw new SolrServerException("MetacatSolrIndex.query - There is no any authorized subjects(even the public user) in this query session.");
123
        }
124 7609 tao
        InputStream inputStream = null;
125
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
126 7718 tao
        String wt = solrParams.get(SolrQueryService.WT);
127 7647 leinfelder
        // handle normal and skin-based queries
128 7718 tao
        if (SolrQueryService.isSupportedWT(wt)) {
129
            // just handle as normal solr query
130
131 7720 tao
            inputStream = SolrQueryServiceController.getInstance().query(solrParams, authorizedSubjects);
132 7647 leinfelder
        }
133
        else {
134 7718 tao
            // assume it is a skin name
135
            String qformat = wt;
136
137
            // perform the solr query using wt=XML
138
            wt = SolrQueryResponseWriterFactory.XML;
139
            ModifiableSolrParams msp = new ModifiableSolrParams(solrParams);
140
            msp.set(SolrQueryService.WT, wt);
141 7720 tao
            inputStream = SolrQueryServiceController.getInstance().query(msp, authorizedSubjects);
142 7718 tao
143
            // apply the stylesheet (XML->HTML)
144
            DBTransform transformer = new DBTransform();
145
            String documentContent = IOUtils.toString(inputStream, "UTF-8");
146
            String sourceType = "solr";
147
            String targetType = "-//W3C//HTML//EN";
148
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
149
            Writer writer = new OutputStreamWriter(baos , "UTF-8");
150
            // TODO: include more params?
151
            Hashtable<String, String[]> params = new Hashtable<String, String[]>();
152
            params.put("qformat", new String[] {qformat});
153
            transformer.transformXMLDocument(
154
                    documentContent ,
155
                    sourceType,
156
                    targetType ,
157
                    qformat,
158
                    writer,
159
                    params,
160
                    null //sessionid
161
                    );
162
163
            // finally, get the HTML back
164 7757 leinfelder
            inputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
165
            ((ContentTypeByteArrayInputStream) inputStream).setContentType("text/html");
166 7647 leinfelder
        }
167
168 7609 tao
        return inputStream;
169 7718 tao
170 7609 tao
    }
171 8464 leinfelder
172
    public void submit(Identifier pid, SystemMetadata systemMetadata, Map<String, List<Object>> fields) {
173
    	IndexTask task = new IndexTask();
174
    	task.setSystemMetadata(systemMetadata);
175
    	task.setFields(fields);
176
		HazelcastService.getInstance().getIndexQueue().put(pid, task );
177
    }
178
179 7718 tao
180 7609 tao
}