001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.webapp.servlets;
006:
007: import java.io.IOException;
008: import java.io.PrintWriter;
009:
010: import javax.servlet.http.HttpServlet;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013: import javax.servlet.http.HttpSession;
014:
015: public final class ServerHopCookieRewriteTestServlet extends
016: HttpServlet {
017:
018: private static final String ATTR_NAME = "test-attribute";
019: private static final String SESS_ID = "session-id";
020:
021: public static final String DLM = "!";
022:
023: public void doGet(HttpServletRequest request,
024: HttpServletResponse response) throws IOException {
025: HttpSession session = request.getSession(true);
026: response.setContentType("text/html");
027: PrintWriter out = response.getWriter();
028:
029: String serverParam = request.getParameter("server");
030: if ("0".equals(serverParam)) {
031: hit0(request, session, out);
032: } else if ("1".equals(serverParam)) {
033: hit1(request, session, out);
034: } else if ("2".equals(serverParam)) {
035: hit2(request, session, out);
036: } else if ("3".equals(serverParam)) {
037: hit3(request, session, out);
038: } else {
039: out.print("unknown value for server param: " + serverParam);
040: }
041: }
042:
043: private void hit3(HttpServletRequest request, HttpSession session,
044: PrintWriter out) {
045: if (session.isNew()) {
046: out.print("session is new for server 2; sessionId="
047: + session.getId());
048: } else if (isServerHop(session)) {
049: out.print("is a server hop");
050: } else {
051: out.println("OK");
052: }
053: }
054:
055: private void hit1(HttpServletRequest req, HttpSession session,
056: PrintWriter out) {
057: if (session.isNew()) {
058: out.print("session is new for server 1; sessionId="
059: + session.getId());
060: } else if (!isServerHop(session)) {
061: out.print("not a server hop");
062: } else {
063: String value = (String) session.getAttribute(ATTR_NAME);
064: if (value == null) {
065: out.print("attribute is null");
066: } else {
067: if (!value.equals("0")) {
068: out.print("unexpected value: " + value);
069: }
070: final String oldKey = getKey((String) session
071: .getAttribute(SESS_ID));
072: final String newKey = getKey(session.getId());
073: final String oldServerId = getServerId((String) session
074: .getAttribute(SESS_ID));
075: final String newServerId = getServerId(session.getId());
076: if (!session.getId()
077: .equals(req.getRequestedSessionId())
078: && newKey.equals(oldKey)
079: && !newServerId.equals(oldServerId)) {
080: out.print("OK");
081: } else {
082: out.print("oldKey=" + oldKey + ",newKey=" + newKey
083: + ",oldServerId=" + oldServerId
084: + ",newServerId=" + newServerId);
085: }
086: }
087: }
088: }
089:
090: private String getServerId(String s) {
091: if (s == null || s.trim().length() == 0 || s.indexOf(DLM) == -1)
092: return null;
093: return s.substring(s.indexOf(DLM) + 1);
094: }
095:
096: private String getKey(String s) {
097: if (s == null || s.trim().length() == 0 || s.indexOf(DLM) == -1)
098: return null;
099: return s.substring(0, s.indexOf(DLM));
100: }
101:
102: private void hit2(HttpServletRequest req, HttpSession session,
103: PrintWriter out) {
104: if (session.isNew()) {
105: out.print("session is new for server 2; sessionId="
106: + session.getId());
107: } else if (!isServerHop(session)) {
108: out.print("not a server hop");
109: } else {
110: String value = (String) session.getAttribute(ATTR_NAME);
111: if (value == null) {
112: out.print("missing attribute");
113: }
114: value = (String) session.getAttribute(SESS_ID);
115: if (!session.getId().equals(req.getRequestedSessionId())
116: && session.getId().equals(value)) {
117: out.print("OK");
118: } else {
119: out.print("expected=" + session.getId() + ",got="
120: + value);
121: }
122: }
123: }
124:
125: private void hit0(HttpServletRequest req, HttpSession session,
126: PrintWriter out) {
127: if (!session.isNew()) {
128: out.print("session is not new for server 0; sessionId="
129: + session.getId());
130: return;
131: }
132:
133: String value = (String) session.getAttribute(ATTR_NAME);
134: if (!session.getId().equals(req.getRequestedSessionId())
135: && value == null) {
136: out.print("OK");
137: session.setAttribute(ATTR_NAME, "0");
138: session.setAttribute(SESS_ID, session.getId());
139: } else {
140: out.print("attribute already exists: " + value);
141: }
142: }
143:
144: private static boolean isServerHop(HttpSession session) {
145: try {
146: Object sessionId = session.getClass().getMethod(
147: "getSessionId", new Class[] {}).invoke(session,
148: new Object[] {});
149: Boolean b = (Boolean) sessionId.getClass().getMethod(
150: "isServerHop", new Class[] {}).invoke(sessionId,
151: new Object[] {});
152: return b.booleanValue();
153: } catch (Exception e) {
154: throw new RuntimeException(e);
155: }
156:
157: }
158:
159: // if (!((SessionData) session).getSessionId().isServerHop()) {
160:
161: }
|