KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Session04


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23
24 /**
25  * Part 4 of Session Tests. Ensures that there is an existing session, and
26  * that the requested session information matches it. Also, ensure that we
27  * can invalidate this session and create a new one (with a different session
28  * identifier) while processing this request.
29  *
30  * @author Craig R. McClanahan
31  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
32  */

33
34 public class Session04 extends HttpServlet {
35
36     public void doGet(HttpServletRequest request, HttpServletResponse response)
37         throws IOException, ServletException {
38
39         log("Session04 - Starting, requestedSessionId = " +
40             request.getRequestedSessionId());
41         response.setContentType("text/plain");
42         PrintWriter writer = response.getWriter();
43
44         // Ensure that there is a current session
45
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
46         HttpSession oldSession = request.getSession(false);
47         if (oldSession == null)
48             results.append(" No existing session/");
49
50         // Acquire the session identifier of the old session
51
String JavaDoc oldSessionId = null;
52         if (oldSession != null) {
53             try {
54                 oldSessionId = oldSession.getId();
55             } catch (IllegalStateException JavaDoc e) {
56                 results.append(" Old session is expired/");
57             }
58         }
59
60         // Match against the requested session identifier
61
String JavaDoc requestedSessionId = null;
62         if (oldSessionId != null) {
63             requestedSessionId = request.getRequestedSessionId();
64             if (requestedSessionId == null) {
65                 results.append(" No requested session id/");
66             } else {
67                 if (!request.isRequestedSessionIdValid())
68                     results.append(" Requested session id is not valid/");
69                 if (!oldSessionId.equals(requestedSessionId)) {
70                     results.append(" Requested session=");
71                     results.append(requestedSessionId);
72                     results.append(" Old session=");
73                     results.append(oldSessionId);
74                     results.append("/");
75                 }
76             }
77         }
78
79         // Verify that we received the requested session identifier correctly
80
if (requestedSessionId != null) {
81             if (!request.isRequestedSessionIdFromCookie())
82                 results.append(" Requested session not from cookie/");
83             if (request.isRequestedSessionIdFromURL())
84                 results.append(" Requested session from URL/");
85         }
86
87         // Verify that we can create an attribute in the old session
88
if (oldSession != null) {
89             SessionBean bean = new SessionBean();
90             bean.setStringProperty("Session04");
91             oldSession.setAttribute("sessionBean", bean);
92         }
93
94         // Verify that we can invalidate the old session
95
if (oldSession != null) {
96             try {
97                 oldSession.invalidate();
98             } catch (IllegalStateException JavaDoc e) {
99                 results.append(" Old session is already invalidated/");
100             }
101         }
102
103         // Verify that we can create a new session
104
HttpSession newSession = request.getSession(true);
105         if (newSession == null) {
106             results.append(" Cannot create new session/");
107         } else {
108             String JavaDoc newSessionId = null;
109             try {
110                 newSessionId = newSession.getId();
111             } catch (IllegalStateException JavaDoc e) {
112                 results.append(" New session is already invalidated/");
113             }
114             if ((oldSession != null) && (newSession != null)) {
115                 if (oldSession == newSession)
116                     results.append(" oldSession == newSession/");
117                 if (oldSession.equals(newSession))
118                     results.append(" oldSession equals newSession/");
119             }
120             if ((oldSessionId != null) && (newSessionId != null) &&
121                 oldSessionId.equals(newSessionId)) {
122                 results.append(" New session id = old session id/");
123             }
124         }
125
126         // Verify that the old session's attribute did not carry forward
127
if (newSession != null) {
128             SessionBean bean =
129                 (SessionBean) newSession.getAttribute("sessionBean");
130             if (bean != null)
131                 results.append(" New session has attribute already/");
132         }
133
134         // Store an activation event listener in the session
135
newSession.setAttribute("activationListener",
136                                     new SessionListener03());
137
138         // Report success if everything is still ok
139
if (results.length() == 0)
140             writer.println("Session04 PASSED");
141         else {
142             writer.print("Session04 FAILED -");
143             writer.println(results.toString());
144         }
145         while (true) {
146             String JavaDoc message = StaticLogger.read();
147             if (message == null)
148                 break;
149             writer.println(message);
150         }
151         StaticLogger.reset();
152         log("Session04 - Stopping");
153
154     }
155
156 }
157
Popular Tags