View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.jsr286.pluto;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  
38  import java.io.ByteArrayOutputStream;
39  import java.io.IOException;
40  import java.io.PrintWriter;
41  import java.io.UnsupportedEncodingException;
42  
43  import javax.servlet.ServletOutputStream;
44  import javax.servlet.ServletResponse;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.http.HttpServletResponseWrapper;
47  
48  
49  /**
50   * Wrap <code>HttpServletResponse</code> for Lutece processing:
51   * We can't output direct flow to response.
52   * We must keep all output, and give to {@link fr.paris.lutece.plugins.jsr168.pluto.LuteceToPlutoConnector}
53   * for return it for render.
54   */
55  public class LuteceHttpServletResponse extends HttpServletResponseWrapper
56  {
57      private ServletOutputStream _servletOutputStream;
58      private PrintWriter _printWriter;
59      private ByteArrayOutputStream _buffer;
60  
61      /**
62       * Initialize buffer and stream of the instance for capture
63       * chars flow.
64       *
65       * @param response the real (parent) <code>HttpServletResponse</code> instance
66       */
67      public LuteceHttpServletResponse( HttpServletResponse response )
68      {
69          super( response );
70  
71          _buffer = new ByteArrayOutputStream(  );
72          _servletOutputStream = new ServletOutputStream(  )
73                  {
74                      public void write( int b )
75                      {
76                          _buffer.write( b );
77                      }
78                  };
79          _printWriter = new PrintWriter( _servletOutputStream );
80      }
81  
82      /**
83       * Return the content of the buffer
84       *
85       * @return the content of the buffer
86       */
87      public String getBufferString(  )
88      {
89          flushBuffer(  );
90  
91          try
92          {
93              return _buffer.toString( "ISO-8859-1" );
94          }
95          catch ( UnsupportedEncodingException e )
96          {
97              // UTF-8 est un encoding standard de JAVA
98              AppLogService.error( e.getMessage(  ), e );
99              throw new RuntimeException( e.getMessage(  ) );
100         }
101     }
102 
103     /**
104      * Overridden: flush local buffer instead "real" buffer (in parent).
105      *
106      * @see javax.servlet.ServletResponse#flushBuffer()
107      */
108     public void flushBuffer(  )
109     {
110         // super.flushBuffer(  );
111         _printWriter.flush(  );
112 
113         try
114         {
115             _servletOutputStream.flush(  );
116         }
117         catch ( IOException e )
118         {
119             // Nothing to do
120             AppLogService.error( "Unreachable catch case", e );
121         }
122     }
123 
124     /**
125      * Overridden: return local <code>OutputStream</code>.
126      *
127      * @see javax.servlet.ServletResponse#getOutputStream()
128      */
129     public ServletOutputStream getOutputStream(  )
130     {
131         // return super.getOutputStream(  );
132         return _servletOutputStream;
133     }
134 
135     /**
136      * Overridden: return local <code>PrintWriter</code>.
137      *
138      * @see javax.servlet.ServletResponse#getWriter()
139      */
140     public PrintWriter getWriter(  )
141     {
142         // return super.getWriter(  );
143         return _printWriter;
144     }
145 
146     /**
147      * Overridden: flush buffer and clear content
148      *
149      * @see javax.servlet.ServletResponse#reset()
150      */
151     public void reset(  )
152     {
153         // return super.reset(  );
154         flushBuffer(  );
155         _buffer.reset(  );
156     }
157 
158     /**
159      * Overridden: flush buffer and clear content
160      *
161      * @see javax.servlet.ServletResponse#resetBuffer()
162      */
163     public void resetBuffer(  )
164     {
165         // return super.resetBuffer(  );
166         throw new UnsupportedOperationException( "resetBuffer n'est pas supporte!" );
167     }
168 
169     /**
170      * Overridden: unsupported operation
171      *
172      * @see HttpServletResponse#sendError(int)
173      */
174     public void sendError( int nHttpErrorCode )
175     {
176         // return super.sendError( nHttpErrorCode );
177         throw new UnsupportedOperationException( "sendError n'est pas supporte!" );
178     }
179 
180     /**
181      * Overridden: unsupported operation
182      *
183      * @see HttpServletResponse#sendError(int, java.lang.String)
184      */
185     public void sendError( int arg0, String arg1 ) throws IOException
186     {
187         throw new UnsupportedOperationException( "sendError n'est pas supporte!" );
188     }
189 
190     /**
191      * Overridden: unsupported operation
192      *
193      * @see HttpServletResponse#sendRedirect(java.lang.String)
194      */
195     public void sendRedirect( String arg0 ) throws IOException
196     {
197     	super.sendRedirect(arg0);
198         //throw new UnsupportedOperationException( "rediriger vers "+ arg0 );
199     }
200 
201     /**
202      * Overridden: empty operation
203      *
204      * @see javax.servlet.ServletResponse#setBufferSize(int)
205      */
206     public void setBufferSize( int nSize )
207     {
208         // super.setBufferSize( nSize );
209     }
210 
211     /**
212      * Overridden: unsupported operation
213      *
214      * @see javax.servlet.ServletResponse#setContentLength(int)
215      */
216     public void setContentLength( int nContentLength )
217     {
218         // super.setContentType( nContentLength );
219         throw new UnsupportedOperationException( "setContentLength n'est pas supporte!" );
220     }
221 
222     /**
223      * Overridden: empty operation
224      *
225      * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
226      */
227     public void setContentType( String strContentType )
228     {
229         // super.setContentType( strContentType );
230     }
231 
232     /**
233      * Overridden: unsupported operation
234      *
235      * @see HttpServletResponse#setStatus(int)
236      */
237     public void setStatus( int nHttpStatus )
238     {
239         throw new UnsupportedOperationException( "setStatus n'est pas supporte!" );
240     }
241 
242     /**
243      * Overridden: unsupported operation
244      *
245      * @see HttpServletResponse#setStatus(int, java.lang.String)
246      */
247     public void setStatus( int arg0, String arg1 )
248     {
249         throw new UnsupportedOperationException( "setStatus n'est pas supporte!" );
250     }
251 
252     /**
253      * Overridden: empty operation (warn logged)
254      *
255      * @see javax.servlet.ServletResponseWrapper#setResponse(javax.servlet.ServletResponse)
256      */
257     public void setResponse( ServletResponse response )
258     {
259         AppLogService.info( "Plugin JSR 168: Try to redefine a response object!" );
260     }
261 
262     /**
263      * Overridden: return local buffer size
264      *
265      * @see javax.servlet.ServletResponse#getBufferSize()
266      */
267     public int getBufferSize(  )
268     {
269         // return super.getBufferSize(  );
270         return _buffer.size(  );
271     }
272 
273     /**
274      * Overridden: proxy operation (info logged)
275      *
276      * @see javax.servlet.ServletResponseWrapper#getResponse()
277      */
278     public ServletResponse getResponse(  )
279     {
280         ServletResponse response = super.getResponse(  );
281 
282         AppLogService.debug( "Plugin JSR 168: getResponse() asked (response class is " +
283             response.getClass(  ).getName(  ) + ")." );
284 
285         return response;
286     }
287 
288     /**
289      * Overridden: always "true" operation (info logged)
290      *
291      * @see javax.servlet.ServletResponse#isCommitted()
292      */
293     public boolean isCommitted(  )
294     {
295         boolean comitted = super.isCommitted(  );
296 
297         AppLogService.debug( "Plugin JSR 168: isCommited asked (commited: " + comitted + ")." );
298 
299         return true;
300     }
301 }