01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test.web.servlets;
23:
24: import java.io.IOException;
25: import java.io.PrintWriter;
26: import javax.servlet.ServletConfig;
27: import javax.servlet.ServletException;
28: import javax.servlet.http.HttpServlet;
29: import javax.servlet.http.HttpServletRequest;
30: import javax.servlet.http.HttpServletResponse;
31: import javax.servlet.http.HttpSession;
32:
33: import org.jboss.test.web.util.Util;
34:
35: /** A servlet that tests use of various servlet API calls that can be affected
36: by the web container integration layer.
37:
38: @author Scott.Stark@jboss.org
39: @version $Revision: 57211 $
40: */
41: public class APIServlet extends HttpServlet {
42: protected void processRequest(HttpServletRequest request,
43: HttpServletResponse response) throws ServletException,
44: IOException {
45: response.setContentType("text/html");
46: PrintWriter out = response.getWriter();
47: out.println("<html>");
48: out
49: .println("<head><title>APIServlet</title></head><body><pre>");
50: String op = request.getParameter("op");
51: if (op.equals("testGetRealPath")) {
52: String realPath = testGetRealPath();
53: out.println("testGetRealPath ok, realPath=" + realPath
54: + "\n");
55: } else if (op.equals("testSessionListener")) {
56: testSessionListener(request);
57: } else {
58: throw new ServletException("Unknown operation called, op="
59: + op);
60: }
61: out.println("</pre></body></html>");
62: out.close();
63: }
64:
65: protected void doGet(HttpServletRequest request,
66: HttpServletResponse response) throws ServletException,
67: IOException {
68: processRequest(request, response);
69: }
70:
71: protected void doPost(HttpServletRequest request,
72: HttpServletResponse response) throws ServletException,
73: IOException {
74: processRequest(request, response);
75: }
76:
77: private String testGetRealPath() throws ServletException {
78: String realPath = getServletContext().getRealPath("/");
79: if (realPath == null)
80: throw new ServletException(
81: "getServletContext().getRealPath(/) returned null");
82: return realPath;
83: }
84:
85: private void testSessionListener(HttpServletRequest request)
86: throws ServletException {
87: // Create/get the session
88: HttpSession session = request.getSession(true);
89: String sessionID = session.getId();
90: boolean created = TestSessionListener.wasCreated(sessionID);
91: if (created == false)
92: throw new ServletException("No session create event seen");
93: // Invalidate the session to test the destroy event
94: session.invalidate();
95: boolean destroyed = TestSessionListener.wasDestroyed(sessionID);
96: if (destroyed == false)
97: throw new ServletException("No session destroy event seen");
98: }
99: }
|