001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.sample.servlet;
021:
022: import java.io.IOException;
023: import java.io.PrintWriter;
024:
025: import java.util.Hashtable;
026:
027: import javax.servlet.RequestDispatcher;
028: import javax.servlet.ServletConfig;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.Cookie;
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.http.HttpSession;
035:
036: /**
037: * Sample servlet that implement some very simple business logic. The goal is
038: * to provide functional tests for Cactus, so we focus on providing as many
039: * different test cases as possible rather than implementing a meaningful
040: * business logic.
041: *
042: * @version $Id: SampleServlet.java 238835 2004-03-06 20:59:41Z vmassol $
043: */
044: public class SampleServlet extends HttpServlet {
045: /**
046: * Entry point for the servlet when a GET request is received. This will
047: * be used to verify that we can test for the servlet output stream in
048: * Cactus test cases.
049: *
050: * @param theRequest the HTTP request
051: * @param theResponse the HTTP response
052: *
053: * @exception IOException on failure
054: */
055: public void doGet(HttpServletRequest theRequest,
056: HttpServletResponse theResponse) throws IOException {
057: PrintWriter pw = theResponse.getWriter();
058:
059: theResponse.setContentType("text/html");
060:
061: pw.print("<html><head/><body>");
062: pw.print("A GET request");
063: pw.print("</body></html>");
064: }
065:
066: /**
067: * Return the method used to send data from the client (POST or GET). This
068: * will be used to verify that we can simulate POST or GET methods from
069: * Cactus. This simulates a method which would test the method to
070: * implement it's business logic.
071: *
072: * @param theRequest the HTTP request
073: * @return the method used to post data
074: */
075: public String checkMethod(HttpServletRequest theRequest) {
076: return theRequest.getMethod();
077: }
078:
079: /**
080: * Set some variable in the HTTP session. It verifies that a session object
081: * has automatically been created by Cactus prior to calling this method.
082: *
083: * @param theRequest the HTTP request
084: */
085: public void setSessionVariable(HttpServletRequest theRequest) {
086: HttpSession session = theRequest.getSession(false);
087:
088: session.setAttribute("name_setSessionVariable",
089: "value_setSessionVariable");
090: }
091:
092: /**
093: * Set some attribute in the request.
094: *
095: * @param theRequest the HTTP request
096: */
097: public void setRequestAttribute(HttpServletRequest theRequest) {
098: theRequest.setAttribute("name_setRequestAttribute",
099: "value_setRequestAttribute");
100: }
101:
102: /**
103: * Get some parameters from the HTTP request.
104: *
105: * @param theRequest the HTTP request
106: * @return a hashtable containing some parameters
107: */
108: public Hashtable getRequestParameters(HttpServletRequest theRequest) {
109: Hashtable params = new Hashtable();
110:
111: params.put("param1", theRequest.getParameter("param1"));
112: params.put("param2", theRequest.getParameter("param2"));
113:
114: return params;
115: }
116:
117: /**
118: * Get a header from the request.
119: *
120: * @return a test request header
121: * @param theRequest the HTTP request
122: */
123: public String getRequestHeader(HttpServletRequest theRequest) {
124: return theRequest.getHeader("testheader");
125: }
126:
127: /**
128: * @return the cookies sent in the HTTP request
129: *
130: * @param theRequest the HTTP request
131: */
132: public Hashtable getRequestCookies(HttpServletRequest theRequest) {
133: Hashtable allCookies = new Hashtable();
134:
135: Cookie[] cookies = theRequest.getCookies();
136:
137: if (cookies != null) {
138: for (int i = 0; i < cookies.length; i++) {
139: Cookie cookie = cookies[i];
140:
141: allCookies.put(cookie.getName(), cookie.getValue());
142: }
143: }
144:
145: return allCookies;
146: }
147:
148: /**
149: * Set a header in the HTTP response. This is to verify that Cactus tests
150: * can assert the returned headers.
151: *
152: * @param theResponse the HTTP response
153: */
154: public void setResponseHeader(HttpServletResponse theResponse) {
155: theResponse.setHeader("responseheader",
156: "this is a response header");
157: }
158:
159: /**
160: * Set a cookie for sending back to the client. This is to verify that
161: * it is possible with Cactus to assert the cookies returned to the client
162: *
163: * @param theResponse the HTTP response
164: */
165: public void setResponseCookie(HttpServletResponse theResponse) {
166: Cookie cookie = new Cookie("responsecookie",
167: "this is a response cookie");
168:
169: cookie.setDomain("jakarta.apache.org");
170: theResponse.addCookie(cookie);
171: }
172:
173: /**
174: * Use a <code>RequestDispatcher</code> to forward to a JSP page. This is
175: * to verify that Cactus supports asserting the result, even in the case
176: * of forwarding to another page.
177: *
178: * @param theRequest the HTTP request
179: * @param theResponse the HTTP response
180: * @param theConfig the servlet config object
181: *
182: * @exception IOException on failure
183: * @exception ServletException on failure
184: */
185: public void doForward(HttpServletRequest theRequest,
186: HttpServletResponse theResponse, ServletConfig theConfig)
187: throws IOException, ServletException {
188: RequestDispatcher rd = theConfig.getServletContext()
189: .getRequestDispatcher("/test/test.jsp");
190:
191: rd.forward(theRequest, theResponse);
192: }
193:
194: /**
195: * Use a <code>RequestDispatcher</code> to include a JSP page. This is
196: * to verify that Cactus supports asserting the result, even in the case
197: * of including another page.
198: *
199: * @param theRequest the HTTP request
200: * @param theResponse the HTTP response
201: * @param theConfig the servlet config object
202: *
203: * @exception IOException on failure
204: * @exception ServletException on failure
205: */
206: public void doInclude(HttpServletRequest theRequest,
207: HttpServletResponse theResponse, ServletConfig theConfig)
208: throws IOException, ServletException {
209: RequestDispatcher rd = theConfig.getServletContext()
210: .getRequestDispatcher("/test/test.jsp");
211:
212: rd.include(theRequest, theResponse);
213: }
214: }
|