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