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 org.apache.solr.client.solrj.SolrServerException;
22
import org.apache.solr.response.CSVResponseWriter;
23
import org.apache.solr.response.JSONResponseWriter;
24
import org.apache.solr.response.PHPResponseWriter;
25
import org.apache.solr.response.PHPSerializedResponseWriter;
26
import org.apache.solr.response.PythonResponseWriter;
27
import org.apache.solr.response.QueryResponseWriter;
28
import org.apache.solr.response.RubyResponseWriter;
29
//import org.apache.solr.response.VelocityResponseWriter;
30
import org.apache.solr.response.XMLResponseWriter;
31

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

    
82
	public static String getContentType(String wt) {
83
		String contentType = null;
84
		if (wt == null || wt.trim().equals("") || wt.equals(XML)) {
85
			contentType = "text/xml";
86
		} else if (wt.equals(JSON)) {
87
			contentType = "text/json";
88
		} else if (wt.equals(PYTHON)) {
89
			contentType = "text/plain";
90
		} else if (wt.equals(RUBY)) {
91
			contentType = "text/plain";
92
		} else if (wt.equals(PHP)) {
93
			contentType = "text/plain";
94
		} else if (wt.equals(PHPS)) {
95
			contentType = "text/plain";
96
		} else if (wt.equals(VELOCITY)) {
97
			contentType = "text/html";
98
		} else if (wt.equals(CSV)) {
99
			contentType = "text/csv";
100
		} else {
101
			// just don't know what it is...
102
			contentType = null;
103
		}
104
		return contentType;
105
	}
106
}
(5-5/7)