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

    
36
import java.util.Set;
37

    
38
import javax.xml.parsers.ParserConfigurationException;
39

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

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

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

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

    
176
    public void submit(Identifier pid, SystemMetadata systemMetadata, Map<String, List<Object>> fields, boolean followRevisions) {
177
    	IndexTask task = new IndexTask();
178
    	task.setSystemMetadata(systemMetadata);
179
    	task.setFields(fields);
180
		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
		    Map<String, List<Object>> obsoletedFields = EventLog.getInstance().getIndexFields(obsoletedPid, Event.READ.xmlValue());
187
			this.submit(obsoletedPid, obsoletedSysMeta , obsoletedFields, followRevisions);
188
		}
189
		
190
    }
191
    
192

    
193
}
(4-4/4)