Project

General

Profile

« Previous | Next » 

Revision 7723

Added by Jing Tao over 11 years ago

move SolrQueryResponseTransformer and SolrQueryReponseWriterFactory to the query package.

View differences:

metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/EmbeddedSolrQueryService.java
47 47
import org.dataone.service.types.v1.Subject;
48 48
import org.xml.sax.SAXException;
49 49

  
50
import edu.ucsb.nceas.metacat.common.SolrQueryResponseTransformer;
50
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseTransformer;
51 51

  
52 52

  
53 53
/**
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryService.java
41 41
import org.dataone.service.types.v1.Subject;
42 42
import org.dataone.service.util.Constants;
43 43

  
44
import edu.ucsb.nceas.metacat.common.SolrQueryResponseWriterFactory;
44
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
45 45

  
46 46

  
47 47
/**
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryResponseTransformer.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2013-04-19 17:47:14 -0700 (Fri, 19 Apr 2013) $'
11
 * '$Revision: 7595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.common.query;
28

  
29
import java.io.ByteArrayInputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.StringWriter;
33
import java.io.Writer;
34

  
35
import org.apache.solr.client.solrj.SolrServerException;
36
import org.apache.solr.client.solrj.response.QueryResponse;
37
import org.apache.solr.common.params.SolrParams;
38
import org.apache.solr.core.SolrCore;
39
import org.apache.solr.request.LocalSolrQueryRequest;
40
import org.apache.solr.response.QueryResponseWriter;
41
import org.apache.solr.response.SolrQueryResponse;
42

  
43
/**
44
 * Transform the solr QueryReponse to the InputStream. Currently it works only for
45
 * The EmbeddedSolrServer.
46
 * @author tao
47
 *
48
 */
49
public class SolrQueryResponseTransformer {
50
    
51
    private SolrCore core = null;
52
    /**
53
     * Constructor
54
     * @param core
55
     */
56
    public SolrQueryResponseTransformer(SolrCore core) {
57
        this.core = core;
58
    }
59
    
60
    
61
    /**
62
     * Transform the query response the the inputstream.
63
     * @param request
64
     * @param response
65
     * @param wt
66
     * @return
67
     * @throws SolrServerException
68
     * @throws IOException
69
     */
70
    public InputStream transformResults(SolrParams request, QueryResponse response, String wt) throws SolrServerException, IOException {
71
        //InputStream stream = null;
72
        QueryResponseWriter writer = SolrQueryResponseWriterFactory.generateResponseWriter(wt);
73
        Writer results = new StringWriter();
74
        SolrQueryResponse sResponse = new SolrQueryResponse();
75
        sResponse.setAllValues(response.getResponse());
76
        writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
77
        return new ByteArrayInputStream(results.toString().getBytes("UTF-8"));
78
    }
79
}
metacat-common/src/main/java/edu/ucsb/nceas/metacat/common/query/SolrQueryResponseWriterFactory.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2013-04-19 17:47:14 -0700 (Fri, 19 Apr 2013) $'
11
 * '$Revision: 7595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.common.query;
28

  
29
import org.apache.solr.client.solrj.SolrServerException;
30
import org.apache.solr.response.CSVResponseWriter;
31
import org.apache.solr.response.JSONResponseWriter;
32
import org.apache.solr.response.PHPResponseWriter;
33
import org.apache.solr.response.PHPSerializedResponseWriter;
34
import org.apache.solr.response.PythonResponseWriter;
35
import org.apache.solr.response.QueryResponseWriter;
36
import org.apache.solr.response.RubyResponseWriter;
37
import org.apache.solr.response.VelocityResponseWriter;
38
import org.apache.solr.response.XMLResponseWriter;
39

  
40
/**
41
 * Generate a QueryResponseWriter base on the specified format.
42
 * @author tao
43
 *
44
 */
45
public class SolrQueryResponseWriterFactory {
46
    public static final String XML = "xml";
47
    public static final String JSON = "json";
48
    public static final String PYTHON = "python";
49
    public static final String RUBY = "ruby";
50
    public static final String PHP = "php";
51
    public static final String PHPS = "phps";
52
    public static final String VELOCITY = "velocity";
53
    public static final String CSV ="csv";
54
    
55
   /** Create a QueryReponseWriter based on the specified wt format.    
56
     * Here is the list of the handler class to handle different format.
57
     * <queryResponseWriter name="xml" default="true" class="solr.XMLResponseWriter" />
58
     * <queryResponseWriter name="json" class="solr.JSONResponseWriter"/>
59
     * <queryResponseWriter name="python" class="solr.PythonResponseWriter"/>
60
     * <queryResponseWriter name="ruby" class="solr.RubyResponseWriter"/>
61
     * <queryResponseWriter name="php" class="solr.PHPResponseWriter"/>
62
     * <queryResponseWriter name="phps" class="solr.PHPSerializedResponseWriter"/>
63
     * <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter"/>
64
     * <queryResponseWriter name="csv" class="solr.CSVResponseWriter"/>
65
     */
66
    public static QueryResponseWriter generateResponseWriter(String wt) throws SolrServerException {
67
        QueryResponseWriter writer = null;
68
        if(wt == null || wt.trim().equals("") || wt.equals(XML)) {
69
            writer = new XMLResponseWriter();
70
        } else if(wt.equals(JSON)) {
71
            writer = new JSONResponseWriter();
72
        } else if(wt.equals(PYTHON)) {
73
            writer = new PythonResponseWriter();
74
        } else if(wt.equals(RUBY)) {
75
            writer = new RubyResponseWriter();
76
        } else if(wt.equals(PHP)) {
77
            writer = new PHPResponseWriter();
78
        } else if(wt.equals(PHPS)) {
79
            writer = new PHPSerializedResponseWriter();
80
        } else if(wt.equals(VELOCITY)) {
81
            writer = new VelocityResponseWriter();
82
        } else if(wt.equals(CSV)) {
83
            writer = new CSVResponseWriter();
84
        } else {
85
            throw new SolrServerException("Metacat doesn't support this response format "+wt);
86
        }
87
        return writer;
88
    }
89
}

Also available in: Unified diff