CustomerResponseWriter
package com.upxiaofeng.solr;
import java.io.IOException;
import java.io.Writer;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.SolrQueryResponse;
public class CustomerResponseWriter implements QueryResponseWriter {
public static final String ContentType_UTF_8 = "application/json; charset=UTF-8";
private String contentType;
/**
* 獲取字符類型
*/
public String getContentType(SolrQueryRequest arg0, SolrQueryResponse arg1) {
return this.contentType;
}
/**
* 初始化
*/
public void init(NamedList arg0) {
String content_type = (String) arg0.get("content-type");
this.contentType = content_type == null ? ContentType_UTF_8: content_type;
}
public void write(Writer arg0, SolrQueryRequest arg1, SolrQueryResponse arg2) throws IOException {
CustomerWriter writer = new CustomerWriter(arg0, arg1, arg2);
try {
writer.writerResponse(arg2.getValues());
} catch (Exception e) {
}
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}
CustomerWriter
package com.upxiaofeng.solr;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.ReturnFields;
import com.google.gson.Gson;
public class CustomerWriter extends TextResponseWriter {
private Writer writer ;
private SolrQueryRequest req ;
private SolrQueryResponse rsp ;
public CustomerWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) {
super(writer, req, rsp);
this.writer=writer;
this.rsp=rsp;
this.req=req;
}
/**
* 將響應數據返回爲指定的格式
* 這裏是json爲例
* @param val
*/
public void writerResponse(NamedList val){
Gson gson = new Gson();
String responseVal = gson.toJson(val);
try {
if (responseVal!=null&&!responseVal.isEmpty()) {
this.writer.write(responseVal);
this.writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void writeArray(String arg0, Iterator arg1) throws IOException {
}
@Override
public void writeBool(String arg0, String arg1) throws IOException {
}
@Override
public void writeDate(String arg0, String arg1) throws IOException {
}
@Override
public void writeDouble(String arg0, String arg1) throws IOException {
}
@Override
public void writeEndDocumentList() throws IOException {
}
@Override
public void writeFloat(String arg0, String arg1) throws IOException {
}
@Override
public void writeInt(String arg0, String arg1) throws IOException {
}
@Override
public void writeLong(String arg0, String arg1) throws IOException {
}
@Override
public void writeMap(String arg0, Map arg1, boolean arg2, boolean arg3) throws IOException {
}
@Override
public void writeNamedList(String arg0, NamedList arg1) throws IOException {
}
@Override
public void writeNull(String arg0) throws IOException {
}
@Override
public void writeSolrDocument(String arg0, SolrDocument arg1, ReturnFields arg2, int arg3) throws IOException {
}
@Override
public void writeStartDocumentList(String arg0, long arg1, int arg2, long arg3, Float arg4) throws IOException {
}
@Override
public void writeStr(String arg0, String arg1, boolean arg2) throws IOException {
}
}