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.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.OutputStreamWriter;
29
import java.io.Writer;
30
import java.sql.SQLException;
31
import java.util.Hashtable;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import java.util.Set;
36

    
37
import javax.xml.parsers.ParserConfigurationException;
38

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

    
46
import org.apache.solr.servlet.SolrRequestParsers;
47
import org.dataone.service.exceptions.NotFound;
48
import org.dataone.service.exceptions.NotImplemented;
49
import org.dataone.service.exceptions.UnsupportedType;
50
import org.dataone.service.types.v1.Event;
51
import org.dataone.service.types.v1.Identifier;
52
import org.dataone.service.types.v1.Subject;
53
import org.dataone.service.types.v2.SystemMetadata;
54
import org.xml.sax.SAXException;
55

    
56
import edu.ucsb.nceas.metacat.DBTransform;
57
import edu.ucsb.nceas.metacat.EventLog;
58
import edu.ucsb.nceas.metacat.common.index.IndexTask;
59
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
60
import edu.ucsb.nceas.metacat.common.query.SolrQueryService;
61
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
62
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
63
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
64
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
65

    
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
    
75
    //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
   
81
    
82
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
83
    private static MetacatSolrIndex  solrIndex = null;
84
    
85
    public static MetacatSolrIndex getInstance() throws Exception {
86
        if (solrIndex == null) {
87
            solrIndex = new MetacatSolrIndex();
88
        }
89
        return solrIndex;
90
    }
91
    
92
    /**
93
     * Constructor
94
     * @throws SAXException 
95
     * @throws IOException 
96
     * @throws ParserConfigurationException 
97
     */
98
    private MetacatSolrIndex() throws Exception {
99
    	
100
    }
101
    
102
    
103
    
104
    
105
    /**
106
     * Query the solr server
107
     * @param query  the solr query
108
     * @param authorizedSubjects the authorized subjects in this query session
109
     * @return the result as the InputStream
110
     * @throws SolrServerException 
111
     * @throws ClassNotFoundException 
112
     * @throws SQLException 
113
     * @throws PropertyNotFoundException 
114
     * @throws SAXException 
115
     * @throws ParserConfigurationException 
116
     * @throws UnsupportedType 
117
     * @throws NotFound 
118
     * @throws NotImplemented 
119
     */
120
    public InputStream query(String query, Set<Subject>authorizedSubjects) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, 
121
    ClassNotFoundException, ParserConfigurationException, SAXException, NotImplemented, NotFound, UnsupportedType {
122
        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
        InputStream inputStream = null;
126
        // allow "+" in query syntax, see: https://projects.ecoinformatics.org/ecoinfo/issues/6435
127
        query = query.replaceAll("\\+", "%2B");
128
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
129
        String wt = solrParams.get(SolrQueryService.WT);
130
        // handle normal and skin-based queries
131
        if (SolrQueryService.isSupportedWT(wt)) {
132
            // just handle as normal solr query
133
           
134
            inputStream = SolrQueryServiceController.getInstance().query(solrParams, authorizedSubjects);
135
        }
136
        else {
137
            // 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
            inputStream = SolrQueryServiceController.getInstance().query(msp, authorizedSubjects);
145
            
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
            inputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
168
            ((ContentTypeByteArrayInputStream) inputStream).setContentType("text/html");
169
        }
170
        
171
        return inputStream;
172
     
173
    }
174

    
175
   
176
    
177
    public void submit(Identifier pid, SystemMetadata systemMetadata, Map<String, List<Object>> fields, boolean followRevisions) {
178
    	IndexTask task = new IndexTask();
179
    	task.setSystemMetadata(systemMetadata);
180
    	task.setFields(fields);
181
    	if(pid != null) {
182
    	    log.debug("MetacatSolrIndex.submit - will put the pid "+pid.getValue()+" into the index queue on hazelcast service.");
183
    	}
184
    	
185
		HazelcastService.getInstance().getIndexQueue().put(pid, task);
186
		
187
		if(pid != null) {
188
            log.info("MetacatSolrIndex.submit - put the pid "+pid.getValue()+" into the index queue on hazelcast service successfully.");
189
        }
190
		
191
		// 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
		    Map<String, List<Object>> obsoletedFields = EventLog.getInstance().getIndexFields(obsoletedPid, Event.READ.xmlValue());
196
		    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
			this.submit(obsoletedPid, obsoletedSysMeta , obsoletedFields, followRevisions);
201
		}
202
    }
203
    
204
    
205

    
206
}
(4-4/4)