View Javadoc

1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.domain.geoloc.service.fulltextsearch;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.io.Reader;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.apache.solr.client.solrj.ResponseParser;
32  import org.apache.solr.common.util.NamedList;
33  import org.springframework.util.Assert;
34  
35  import com.gisgraphy.domain.valueobject.Constants;
36  
37  /**
38   * Wrapper that must be used when we want the fulltext query to be serialize
39   * into an output stream
40   * 
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   * @since solr 1.3
43   */
44  public class OutputstreamResponseWrapper extends ResponseParser {
45  
46      private final OutputStream outputStream;
47      private final String writerType;
48  
49      /**
50       * The encoding of the response Default to {@link Constants#CHARSET}
51       */
52      private String encoding = Constants.CHARSET;
53  
54      /**
55       * @param outputStream
56       *                The OutpuStream to serialize the fulltext response in
57       * @param writerType
58       *                The writerType (aka : the wt parameter)
59       */
60      public OutputstreamResponseWrapper(OutputStream outputStream,
61  	    String writerType) {
62  	super();
63  	Assert.notNull(outputStream, "outputstream can not be null");
64  	this.outputStream = outputStream;
65  	this.writerType = writerType == null ? "XML" : writerType;
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see org.apache.solr.client.solrj.ResponseParser#getWriterType()
72       */
73      @Override
74      public String getWriterType() {
75  	return this.writerType;
76      }
77  
78      /*
79       * (non-Javadoc)
80       * 
81       * @see org.apache.solr.client.solrj.ResponseParser#processResponse(java.io.Reader)
82       */
83      @Override
84      public NamedList<Object> processResponse(Reader reader) {
85  
86  	try {
87  	    IOUtils.copy(reader, outputStream, Constants.CHARSET);
88  	} catch (IOException e) {
89  	    e.printStackTrace();
90  	} finally {
91  
92  	    try {
93  		outputStream.flush();
94  		outputStream.close();
95  	    } catch (IOException e) {
96  
97  	    }
98  	}
99  	return new NamedList<Object>();
100     }
101 
102     /*
103      * (non-Javadoc)
104      * 
105      * @see org.apache.solr.client.solrj.ResponseParser#processResponse(java.io.InputStream,
106      *      java.lang.String)
107      */
108     @Override
109     public NamedList<Object> processResponse(InputStream inputStream,
110 	    String encoding) {
111 	try {
112 	    this.encoding = encoding;
113 	    IOUtils.copy(inputStream, outputStream);
114 	} catch (IOException e1) {
115 	} finally {
116 
117 	    try {
118 		outputStream.flush();
119 		outputStream.close();
120 	    } catch (IOException e) {
121 
122 	    }
123 	}
124 	return new NamedList<Object>();
125     }
126 
127     /**
128      * @return The encoding of the response
129      */
130     public String getEncoding() {
131 	return encoding;
132     }
133 
134 }