001    /*
002     * Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.
003     * Copyright 2006 Stephen McConnell.
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package net.dpml.test.http;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    import java.io.PrintWriter;
023    
024    import javax.servlet.RequestDispatcher;
025    import javax.servlet.ServletConfig;
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletException;
028    import javax.servlet.ServletOutputStream;
029    import javax.servlet.http.HttpServlet;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletRequestWrapper;
032    import javax.servlet.http.HttpServletResponse;
033    import javax.servlet.http.HttpServletResponseWrapper;
034    
035    /** Test Servlet RequestDispatcher.
036     * 
037     * @author Greg Wilkins (gregw)
038     */
039    public class DispatchServlet extends HttpServlet
040    {
041       /**
042        * Servlet initialization.
043        * @param config the servlet config
044        * @exception ServletException in an initialization error occurs
045        */
046        public void init( ServletConfig config ) throws ServletException
047        {
048            super.init( config );
049        }
050    
051       /**
052        * Process a post request.
053        * @param sreq the servlet http request
054        * @param sres the servlet http response
055        * @exception ServletException in a processing error occurs
056        * @exception IOException in an I/O error occurs
057        */
058        public void doPost( HttpServletRequest sreq, HttpServletResponse sres ) 
059          throws ServletException, IOException
060        {
061            doGet( sreq, sres );
062        }
063    
064       /**
065        * Process a get request.
066        * @param sreq the servlet http request
067        * @param sres the servlet http response
068        * @exception ServletException in a processing error occurs
069        * @exception IOException in an I/O error occurs
070        */
071        public void doGet( HttpServletRequest sreq, HttpServletResponse sres ) 
072          throws ServletException, IOException
073        {
074            if( sreq.getParameter( "wrap" ) != null )
075            {
076                sreq = new HttpServletRequestWrapper( sreq );
077                sres = new HttpServletResponseWrapper( sres );
078            }
079    
080            String prefix;
081            if( sreq.getContextPath() != null )
082            {
083                prefix = sreq.getContextPath() + sreq.getServletPath();
084            }
085            else
086            {
087                prefix = sreq.getServletPath();
088            }
089            
090            String info;
091            if( sreq.getAttribute( "javax.servlet.include.servlet_path" ) != null )
092            {
093                info = (String) sreq.getAttribute( "javax.servlet.include.path_info" );
094            }
095            else
096            {
097                info = sreq.getPathInfo();
098            }
099    
100            if( info == null )
101            {
102                info= "NULL";
103            }
104            
105            if( info.startsWith( "/includeW/" ) )
106            {
107                doGetIncludeW( info, sreq, sres );
108            }
109            else if( info.startsWith( "/includeS/" ) )
110            {
111                doGetIncludeS( info, sreq, sres );
112            }
113            else if( info.startsWith( "/forward/" ) )
114            {
115                doGetForward( info, sreq, sres );
116            }
117            else if( info.startsWith( "/forwardC/" ) )
118            {
119                doGetForwardC( info, sreq, sres );
120            }
121            else if( info.startsWith( "/forwardSC/" ) )
122            {
123                doGetForwardSC( info, sreq, sres );
124            }
125            else if( info.startsWith( "/includeN/" ) )
126            {
127                doGetIncludeN( info, sreq, sres );
128            }
129            else if( info.startsWith( "/forwardN/" ) )
130            {
131                doGetForwardN( info, sreq, sres );
132            }
133            else
134            {
135                sres.setContentType( "text/html" );
136                PrintWriter pout= sres.getWriter();
137                pout.write(
138                  "<H1>Dispatch URL must be of the form: </H1>"
139                  + "<PRE>"
140                  + prefix
141                  + "/includeW/path\n"
142                  + prefix
143                  + "/includeS/path\n"
144                  + prefix
145                  + "/forward/path\n"
146                  + prefix
147                  + "/includeN/name\n"
148                  + prefix
149                  + "/forwardC/_context/path\n"
150                  + prefix
151                  + "/forwardSC/_context/path</PRE>" );
152                pout.flush();
153            }
154        }
155    
156        private void doGetIncludeW( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
157          throws ServletException, IOException
158        {
159            sres.setContentType( "text/html" );
160            info = info.substring( 9 );
161            if( info.indexOf( '?' ) < 0 )
162            {
163                info += "?Dispatch=include";
164            }
165            else
166            {
167                info += "&Dispatch=include";
168            }
169            
170            PrintWriter pout = null;
171            pout = sres.getWriter();
172            pout.write( 
173              "<H1>Include (writer): " 
174              + info 
175              + "</H1><HR>" );
176            
177            RequestDispatcher dispatch = 
178              getServletContext().getRequestDispatcher( info );
179            if( dispatch == null )
180            {
181                pout = sres.getWriter();
182                pout.write( "<H1>Null dispatcher</H1>" );
183            }
184            else
185            {
186                dispatch.include( sreq, sres );
187            }
188            pout.write( "<HR><H1>-- Included (writer)</H1>" );
189        }
190        
191        private void doGetIncludeS( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
192          throws ServletException, IOException
193        {
194            sres.setContentType( "text/html" );
195            info = info.substring( 9 );
196            if( info.indexOf( '?' ) < 0 )
197            {
198                info += "?Dispatch=include";
199            }
200            else
201            {
202                info += "&Dispatch=include";
203            }
204            
205            OutputStream out = null;
206            out = sres.getOutputStream();
207            out.write( 
208              (
209                "<H1>Include (outputstream): " 
210                + info 
211                + "</H1><HR>" 
212              ).getBytes() 
213            );
214            
215            RequestDispatcher dispatch = 
216              getServletContext().getRequestDispatcher( info );
217            if( dispatch == null )
218            {
219                out = sres.getOutputStream();
220                out.write( "<H1>Null dispatcher</H1>".getBytes() );
221            }
222            else
223            {
224                dispatch.include( sreq, sres );
225            }
226            out.write( "<HR><H1>-- Included (outputstream)</H1>".getBytes() );
227        }
228        
229        private void doGetForward( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
230          throws ServletException, IOException
231        {
232            info = info.substring( 8 );
233            if( info.indexOf( '?' ) < 0 )
234            {
235                info += "?Dispatch=forward";
236            }
237            else
238            {
239                info += "&Dispatch=forward";
240            }
241            
242            RequestDispatcher dispatch = 
243              getServletContext().getRequestDispatcher( info );
244              
245            if( dispatch != null )
246            {
247                ServletOutputStream out = sres.getOutputStream();
248                out.print( "Can't see this" );
249                dispatch.forward( sreq, sres );
250                try
251                {
252                    out.println( "IOException" );
253                    throw new IllegalStateException();
254                }
255                catch( IOException e )
256                {
257                }
258            }
259            else
260            {
261                sres.setContentType( "text/html" );
262                PrintWriter pout= sres.getWriter();
263                pout.write( "<H1>No dispatcher for: " + info + "</H1><HR>" );
264                pout.flush();
265            }
266        }
267        
268        private void doGetForwardC( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
269          throws ServletException, IOException
270        {
271            info = info.substring( 9 );
272            if( info.indexOf( '?' ) < 0 )
273            {
274                info += "?Dispatch=forward";
275            }
276            else
277            {
278                info += "&Dispatch=forward";
279            }
280            
281            String cpath = info.substring( 0, info.indexOf( '/', 1 ) );
282            info = info.substring( cpath.length() );
283            ServletContext context= getServletContext().getContext( cpath );
284            RequestDispatcher dispatch = context.getRequestDispatcher( info );
285            
286            if( dispatch != null )
287            {
288                dispatch.forward( sreq, sres );
289            }
290            else
291            {
292                sres.setContentType( "text/html" );
293                PrintWriter pout = sres.getWriter();
294                pout.write( 
295                  "<H1>No dispatcher for: " 
296                  + cpath 
297                  + "/" 
298                  + info 
299                  + "</H1><HR>" );
300                pout.flush();
301            }
302        }
303        
304        private void doGetForwardSC( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
305          throws ServletException, IOException
306        {
307            sreq.getSession( true );
308            info = info.substring( 10 );
309            if( info.indexOf( '?' ) < 0 )
310            {
311                info += "?Dispatch=forward";
312            }
313            else
314            {
315                info += "&Dispatch=forward";
316            }
317            String cpath = info.substring( 0, info.indexOf( '/', 1 ) );
318            info = info.substring( cpath.length() );
319            
320            ServletContext context = getServletContext().getContext( cpath );
321            RequestDispatcher dispatch = context.getRequestDispatcher( info );
322            
323            if( dispatch != null )
324            {
325                dispatch.forward( sreq, sres );
326            }
327            else
328            {
329                sres.setContentType( "text/html" );
330                PrintWriter pout= sres.getWriter();
331                pout.write( 
332                  "<H1>No dispatcher for: " 
333                  + cpath 
334                  + "/" 
335                  + info 
336                  + "</H1><HR>" );
337                pout.flush();
338            }
339        }
340        
341        private void doGetIncludeN( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
342          throws ServletException, IOException
343        {
344            sres.setContentType( "text/html" );
345            info = info.substring( 10 );
346            if( info.indexOf( "/" ) >= 0 )
347            {
348                info = info.substring( 0, info.indexOf( "/" ) );
349            }
350            PrintWriter pout;
351            if( info.startsWith( "/null" ) )
352            {
353                info = info.substring( 5 );
354            }
355            else
356            {
357                pout = sres.getWriter();
358                pout.write( 
359                  "<H1>Include named: " 
360                  + info 
361                  + "</H1><HR>" );
362            }
363            
364            RequestDispatcher dispatch = 
365              getServletContext().getNamedDispatcher( info );
366            if( dispatch != null )
367            {
368                dispatch.include( sreq, sres );
369            }
370            else
371            {
372                pout = sres.getWriter();
373                pout.write( 
374                  "<H1>No servlet named: " 
375                  + info 
376                  + "</H1>" );
377            }
378            
379            pout = sres.getWriter();
380            pout.write( "<HR><H1>Included " );
381        }
382        
383        private void doGetForwardN( String info, HttpServletRequest sreq, HttpServletResponse sres ) 
384          throws ServletException, IOException
385        {
386            info = info.substring( 10 );
387            if( info.indexOf( "/" ) >= 0 )
388            {
389                info= info.substring( 0, info.indexOf( "/" ) );
390            }
391            RequestDispatcher dispatch = 
392              getServletContext().getNamedDispatcher( info );
393            if( dispatch != null )
394            {
395                dispatch.forward( sreq, sres );
396            }
397            else
398            {
399                sres.setContentType( "text/html" );
400                PrintWriter pout = sres.getWriter();
401                pout.write(
402                  "<H1>No servlet named: " 
403                  + info 
404                  + "</H1>" );
405                pout.flush();
406            }
407        }
408        
409       /**
410        * Return thr servlet info.
411        * @return the info
412        */
413        public String getServletInfo()
414        {
415            return "Include Servlet";
416        }
417    
418       /**
419        * Destroy the servlet.
420        */
421        public synchronized void destroy()
422        {
423        }
424    
425    }