Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.common.query;
20

    
21
import java.io.ByteArrayInputStream;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.StringWriter;
25
import java.io.Writer;
26

    
27
import org.apache.solr.client.solrj.SolrServerException;
28
import org.apache.solr.client.solrj.response.QueryResponse;
29
import org.apache.solr.common.params.SolrParams;
30
import org.apache.solr.core.SolrCore;
31
import org.apache.solr.request.LocalSolrQueryRequest;
32
import org.apache.solr.response.QueryResponseWriter;
33
import org.apache.solr.response.SolrQueryResponse;
34

    
35
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
36

    
37
/**
38
 * Transform the solr QueryReponse to the InputStream. Currently it works only for
39
 * The EmbeddedSolrServer.
40
 * @author tao
41
 *
42
 */
43
public class SolrQueryResponseTransformer {
44
    
45
    private SolrCore core = null;
46
    /**
47
     * Constructor
48
     * @param core
49
     */
50
    public SolrQueryResponseTransformer(SolrCore core) {
51
        this.core = core;
52
    }
53
    
54
    
55
    /**
56
     * Transform the query response the the inputstream.
57
     * @param request
58
     * @param response
59
     * @param wt
60
     * @return
61
     * @throws SolrServerException
62
     * @throws IOException
63
     */
64
    public InputStream transformResults(SolrParams request, QueryResponse response, String wt) throws SolrServerException, IOException {
65
        //InputStream stream = null;
66
        QueryResponseWriter writer = SolrQueryResponseWriterFactory.generateResponseWriter(wt);
67
        Writer results = new StringWriter();
68
        SolrQueryResponse sResponse = new SolrQueryResponse();
69
        sResponse.setAllValues(response.getResponse());
70
        writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
71
        ContentTypeByteArrayInputStream ctbais = new ContentTypeByteArrayInputStream(results.toString().getBytes("UTF-8"));
72
        ctbais.setContentType(SolrQueryResponseWriterFactory.getContentType(wt));
73
        return ctbais;
74
    }
75
}
(4-4/7)