View Javadoc

1   // ========================================================================
2   // Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package com.acme;
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.io.PrintWriter;
18  
19  import javax.servlet.RequestDispatcher;
20  import javax.servlet.ServletConfig;
21  import javax.servlet.ServletContext;
22  import javax.servlet.ServletException;
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.http.HttpServlet;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletRequestWrapper;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.servlet.http.HttpServletResponseWrapper;
29  
30  import org.eclipse.jetty.util.log.Log;
31  
32  
33  /* ------------------------------------------------------------ */
34  /** Test Servlet RequestDispatcher.
35   * 
36   * 
37   */
38  public class DispatchServlet extends HttpServlet
39  {
40      /* ------------------------------------------------------------ */
41      String pageType;
42  
43      /* ------------------------------------------------------------ */
44      public void init(ServletConfig config) throws ServletException
45      {
46          super.init(config);
47      }
48  
49      /* ------------------------------------------------------------ */
50      public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
51      {
52          doGet(sreq, sres);
53      }
54  
55      /* ------------------------------------------------------------ */
56      public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
57      {
58          if (sreq.getParameter("wrap") != null)
59          {
60              sreq= new HttpServletRequestWrapper(sreq);
61              sres= new HttpServletResponseWrapper(sres);
62          }
63          
64          if (sreq.getParameter("session") != null)
65              sreq.getSession(true);
66  
67          String prefix=
68              sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath();
69              
70          String info;
71          
72          if (sreq.getAttribute("javax.servlet.include.servlet_path") != null)
73              info= (String)sreq.getAttribute("javax.servlet.include.path_info");
74          else
75              info= sreq.getPathInfo();
76          
77          if (info == null)
78              info= "NULL";
79  
80          if (info.indexOf(sreq.getServletPath()) > 0)
81          {
82              sres.sendError(403,"Nested " + sreq.getServletPath() + " forbidden.");
83              return;
84          }
85          
86          if(info.indexOf(getServletName()) > 0)
87          {
88              sres.sendError(403,"Nested " + getServletName() + " forbidden.");
89              return;
90          }
91  
92          if (info.startsWith("/includeW/"))
93          {
94              sres.setContentType("text/html");
95              info= info.substring(9);
96              if (info.indexOf('?') < 0)
97                  info += "?Dispatch=include";
98              else
99                  info += "&Dispatch=include";
100             
101             PrintWriter pout= null;
102             pout= sres.getWriter();
103             pout.write("<H1>Include (writer): " + info + "</H1><HR>");
104             
105             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
106             if (dispatch == null)
107             {
108                 pout= sres.getWriter();
109                 pout.write("<H1>Null dispatcher</H1>");
110             }
111             else
112                 dispatch.include(sreq, sres);
113             
114             pout.write("<HR><H1>-- Included (writer)</H1>");
115         }
116         else if (info.startsWith("/includeS/"))
117         {
118             sres.setContentType("text/html");
119             info= info.substring(9);
120             if (info.indexOf('?') < 0)
121                 info += "?Dispatch=include";
122             else
123                 info += "&Dispatch=include";
124             
125             OutputStream out= null;
126             out= sres.getOutputStream();
127             out.write(("<H1>Include (outputstream): " + info + "</H1><HR>").getBytes());
128             
129             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
130             if (dispatch == null)
131             {
132                 out= sres.getOutputStream();
133                 out.write("<H1>Null dispatcher</H1>".getBytes());
134             }
135             else
136                 dispatch.include(sreq, sres);
137             
138             out.write("<HR><H1>-- Included (outputstream)</H1>".getBytes());
139             
140         }
141         else if (info.startsWith("/forward/"))
142         {
143             info= info.substring(8);
144             if (info.indexOf('?') < 0)
145                 info += "?Dispatch=forward";
146             else
147                 info += "&Dispatch=forward";
148             
149             RequestDispatcher dispatch= getServletContext().getRequestDispatcher(info);
150             if (dispatch != null)
151             {
152                 ServletOutputStream out =sres.getOutputStream();
153                 out.print("Can't see this");
154                 dispatch.forward(sreq, sres);
155                 try
156                 {
157                     // should be closed
158                     out.println("IOException");
159                     // should not get here
160                     throw new IllegalStateException();
161                 }
162                 catch(IOException e)
163                 {
164                     Log.ignore(e);
165                 }
166             }
167             else
168             {
169                 sres.setContentType("text/html");
170                 PrintWriter pout= sres.getWriter();
171                 pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
172                 pout.flush();
173             }
174         }
175         else if (info.startsWith("/forwardC/"))
176         {
177             info= info.substring(9);
178             if (info.indexOf('?') < 0)
179                 info += "?Dispatch=forward";
180             else
181                 info += "&Dispatch=forward";
182             
183             String cpath= info.substring(0, info.indexOf('/', 1));
184             info= info.substring(cpath.length());
185             
186             ServletContext context= getServletContext().getContext(cpath);
187             RequestDispatcher dispatch= context.getRequestDispatcher(info);
188             
189             if (dispatch != null)
190             {
191                 dispatch.forward(sreq, sres);
192             }
193             else
194             {
195                 sres.setContentType("text/html");
196                 PrintWriter pout= sres.getWriter();
197                 pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
198                 pout.flush();
199             }
200         }
201         else if (info.startsWith("/includeN/"))
202         {
203             sres.setContentType("text/html");
204             info= info.substring(10);
205             if (info.indexOf("/") >= 0)
206                 info= info.substring(0, info.indexOf("/"));
207             
208             PrintWriter pout;
209             if (info.startsWith("/null"))
210                 info= info.substring(5);
211             else
212             {
213                 pout= sres.getWriter();
214                 pout.write("<H1>Include named: " + info + "</H1><HR>");
215             }
216             
217             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
218             if (dispatch != null)
219                 dispatch.include(sreq, sres);
220             else
221             {
222                 pout= sres.getWriter();
223                 pout.write("<H1>No servlet named: " + info + "</H1>");
224             }
225             
226             pout= sres.getWriter();
227             pout.write("<HR><H1>Included ");
228         }
229         else if (info.startsWith("/forwardN/"))
230         {
231             info= info.substring(10);
232             if (info.indexOf("/") >= 0)
233                 info= info.substring(0, info.indexOf("/"));
234             RequestDispatcher dispatch= getServletContext().getNamedDispatcher(info);
235             if (dispatch != null)
236                 dispatch.forward(sreq, sres);
237             else
238             {
239                 sres.setContentType("text/html");
240                 PrintWriter pout= sres.getWriter();
241                 pout.write("<H1>No servlet named: " + info + "</H1>");
242                 pout.flush();
243             }
244         }
245         else
246         {
247             sres.setContentType("text/html");
248             PrintWriter pout= sres.getWriter();
249             pout.write(
250                     "<H1>Dispatch URL must be of the form: </H1>"
251                     + "<PRE>"
252                     + prefix
253                     + "/includeW/path\n"
254                     + prefix
255                     + "/includeS/path\n"
256                     + prefix
257                     + "/forward/path\n"
258                     + prefix
259                     + "/includeN/name\n"
260                     + prefix
261                     + "/forwardC/_context/path\n</PRE>");
262         }
263         
264     }
265 
266     /* ------------------------------------------------------------ */
267     public String getServletInfo()
268     {
269         return "Include Servlet";
270     }
271 
272     /* ------------------------------------------------------------ */
273     public synchronized void destroy()
274     {
275     }
276 
277 }