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.Identifier;
52
import org.dataone.service.types.v1.Subject;
53
import org.dataone.service.types.v1.SystemMetadata;
54
import org.xml.sax.SAXException;
55

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

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

    
180
}
(4-4/4)