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