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.fulltext;
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.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.util.Assert;
36  
37  import com.gisgraphy.domain.valueobject.Constants;
38  
39  /**
40   * Wrapper that must be used when we want the fulltext query to be serialize
41   * into an output stream
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   * @since solr 1.3
45   */
46  public class OutputstreamResponseWrapper extends ResponseParser {
47  
48      protected static final Logger logger = LoggerFactory
49      .getLogger(OutputstreamResponseWrapper.class);
50      
51      private final OutputStream outputStream;
52      private final String writerType;
53  
54      /**
55       * The encoding of the response Default to {@link Constants#CHARSET}
56       */
57      private String encoding = Constants.CHARSET;
58  
59      /**
60       * @param outputStream
61       *                The OutpuStream to serialize the fulltext response in
62       * @param writerType
63       *                The writerType (aka : the wt parameter)
64       */
65      public OutputstreamResponseWrapper(OutputStream outputStream,
66  	    String writerType) {
67  	super();
68  	Assert.notNull(outputStream, "outputstream can not be null");
69  	this.outputStream = outputStream;
70  	this.writerType = writerType == null ? "XML" : writerType;
71      }
72  
73      /*
74       * (non-Javadoc)
75       * 
76       * @see org.apache.solr.client.solrj.ResponseParser#getWriterType()
77       */
78      @Override
79      public String getWriterType() {
80  	return this.writerType;
81      }
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see org.apache.solr.client.solrj.ResponseParser#processResponse(java.io.Reader)
87       */
88      @Override
89      public NamedList<Object> processResponse(Reader reader) {
90  
91  	try {
92  	    IOUtils.copy(reader, outputStream, Constants.CHARSET);
93  	} catch (IOException e) {
94  	   logger.error("error when writing fulltext respone : "+e,e);
95  	} finally {
96  
97  	    try {
98  		outputStream.flush();
99  		outputStream.close();
100 	    } catch (IOException e) {
101 
102 	    }
103 	}
104 	return new NamedList<Object>();
105     }
106 
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.apache.solr.client.solrj.ResponseParser#processResponse(java.io.InputStream,
111      *      java.lang.String)
112      */
113     @Override
114     public NamedList<Object> processResponse(InputStream inputStream,
115 	    String encoding) {
116 	try {
117 	    this.encoding = encoding;
118 	    IOUtils.copy(inputStream, outputStream);
119 	} catch (IOException e1) {
120 	} finally {
121 
122 	    try {
123 		outputStream.flush();
124 		outputStream.close();
125 	    } catch (IOException e) {
126 
127 	    }
128 	}
129 	return new NamedList<Object>();
130     }
131 
132     /**
133      * @return The encoding of the response
134      */
135     public String getEncoding() {
136 	return encoding;
137     }
138 
139 }