| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package org.example; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.io.PrintWriter; |
| 18 |
import java.util.Date; |
| 19 |
import java.util.Enumeration; |
| 20 |
import java.util.UUID; |
| 21 |
|
| 22 |
import jakarta.servlet.RequestDispatcher; |
| 23 |
import jakarta.servlet.ServletConfig; |
| 24 |
import jakarta.servlet.ServletException; |
| 25 |
import jakarta.servlet.http.HttpServlet; |
| 26 |
import jakarta.servlet.http.HttpServletRequest; |
| 27 |
import jakarta.servlet.http.HttpServletResponse; |
| 28 |
import jakarta.servlet.http.HttpSession; |
| 29 |
import org.eclipse.jetty.util.MultiMap; |
| 30 |
|
| 31 |
/** |
| 32 |
* Test Servlet Sessions. |
| 33 |
*/ |
| 34 |
@SuppressWarnings("serial") |
| 35 |
public class SessionDump extends HttpServlet |
| 36 |
{ |
| 37 |
/** |
| 38 |
* Simple object attribute to test serialization |
| 39 |
*/ |
| 40 |
public static class ObjectAttributeValue implements java.io.Serializable |
| 41 |
{ |
| 42 |
long l; |
| 43 |
|
| 44 |
public ObjectAttributeValue(long l) |
| 45 |
{ |
| 46 |
this.l = l; |
| 47 |
} |
| 48 |
|
| 49 |
public long getValue() |
| 50 |
{ |
| 51 |
return l; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
int redirectCount = 0; |
| 56 |
|
| 57 |
@Override |
| 58 |
public void init(ServletConfig config) |
| 59 |
throws ServletException |
| 60 |
{ |
| 61 |
super.init(config); |
| 62 |
} |
| 63 |
|
| 64 |
protected void handleForm(HttpServletRequest request) |
| 65 |
{ |
| 66 |
HttpSession session = request.getSession(false); |
| 67 |
String action = request.getParameter("Action"); |
| 68 |
String name = request.getParameter("Name"); |
| 69 |
String value = request.getParameter("Value"); |
| 70 |
|
| 71 |
if (action != null) |
| 72 |
{ |
| 73 |
if (action.equals("New Session")) |
| 74 |
{ |
| 75 |
session = request.getSession(true); |
| 76 |
session.setAttribute("test", "value"); |
| 77 |
session.setAttribute("obj", new ObjectAttributeValue(System.currentTimeMillis())); |
| 78 |
session.setAttribute("WEBCL", new MultiMap<>()); |
| 79 |
UUID uuid = UUID.randomUUID(); |
| 80 |
session.setAttribute("uuid", uuid); |
| 81 |
} |
| 82 |
else if (session != null) |
| 83 |
{ |
| 84 |
if (action.equals("Invalidate")) |
| 85 |
session.invalidate(); |
| 86 |
else if (action.equals("Set") && name != null && !name.isEmpty()) |
| 87 |
session.setAttribute(name, value); |
| 88 |
else if (action.equals("Remove")) |
| 89 |
session.removeAttribute(name); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void doPost(HttpServletRequest request, |
| 96 |
HttpServletResponse response) |
| 97 |
throws IOException |
| 98 |
{ |
| 99 |
handleForm(request); |
| 100 |
String nextUrl = getURI(request) + "?R=" + redirectCount++; |
| 101 |
String encodedUrl = response.encodeRedirectURL(nextUrl); |
| 102 |
response.sendRedirect(encodedUrl); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void doGet(HttpServletRequest request, |
| 107 |
HttpServletResponse response) |
| 108 |
throws IOException |
| 109 |
{ |
| 110 |
handleForm(request); |
| 111 |
|
| 112 |
response.setContentType("text/html"); |
| 113 |
|
| 114 |
HttpSession session = request.getSession(getURI(request).indexOf("new") > 0); |
| 115 |
try |
| 116 |
{ |
| 117 |
if (session != null) |
| 118 |
session.isNew(); |
| 119 |
} |
| 120 |
catch (IllegalStateException e) |
| 121 |
{ |
| 122 |
log("Session already invalidated", e); |
| 123 |
session = null; |
| 124 |
} |
| 125 |
|
| 126 |
PrintWriter out = response.getWriter(); |
| 127 |
out.println("<h1>Session Dump Servlet:</h1>"); |
| 128 |
out.println("<form action=\"" + response.encodeURL(getURI(request)) + "\" method=\"post\">"); |
| 129 |
|
| 130 |
if (session == null) |
| 131 |
{ |
| 132 |
out.println("<h3>No Session</h3>"); |
| 133 |
out.println("<input type=\"submit\" name=\"Action\" value=\"New Session\"/>"); |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
if (session.getAttribute("WEBCL") == null) |
| 138 |
session.setAttribute("WEBCL", new MultiMap<>()); |
| 139 |
try |
| 140 |
{ |
| 141 |
out.println("<b>ID:</b> " + session.getId() + "<br/>"); |
| 142 |
out.println("<b>New:</b> " + session.isNew() + "<br/>"); |
| 143 |
out.println("<b>Created:</b> " + new Date(session.getCreationTime()) + "<br/>"); |
| 144 |
out.println("<b>Last:</b> " + new Date(session.getLastAccessedTime()) + "<br/>"); |
| 145 |
out.println("<b>Max Inactive:</b> " + session.getMaxInactiveInterval() + "<br/>"); |
| 146 |
out.println("<b>Context:</b> " + session.getServletContext() + "<br/>"); |
| 147 |
|
| 148 |
Enumeration<String> keys = session.getAttributeNames(); |
| 149 |
while (keys.hasMoreElements()) |
| 150 |
{ |
| 151 |
String name = (String)keys.nextElement(); |
| 152 |
String value = "" + session.getAttribute(name); |
| 153 |
|
| 154 |
out.println("<b>" + name + ":</b> " + value + "<br/>"); |
| 155 |
} |
| 156 |
|
| 157 |
out.println("<b>Name:</b><input type=\"text\" name=\"Name\" /><br/>"); |
| 158 |
out.println("<b>Value:</b><input type=\"text\" name=\"Value\" /><br/>"); |
| 159 |
|
| 160 |
out.println("<input type=\"submit\" name=\"Action\" value=\"Set\"/>"); |
| 161 |
out.println("<input type=\"submit\" name=\"Action\" value=\"Remove\"/>"); |
| 162 |
out.println("<input type=\"submit\" name=\"Action\" value=\"Refresh\"/>"); |
| 163 |
out.println("<input type=\"submit\" name=\"Action\" value=\"Invalidate\"/><br/>"); |
| 164 |
|
| 165 |
out.println("</form><br/>"); |
| 166 |
|
| 167 |
if (request.isRequestedSessionIdFromCookie()) |
| 168 |
out.println("<p>Turn off cookies in your browser to try url encoding<BR>"); |
| 169 |
|
| 170 |
if (request.isRequestedSessionIdFromURL()) |
| 171 |
out.println("<p>Turn on cookies in your browser to try cookie encoding<BR>"); |
| 172 |
out.println("<a href=\"" + response.encodeURL(request.getRequestURI() + "?q=0") + "\">Encoded Link</a><BR>"); |
| 173 |
} |
| 174 |
catch (IllegalStateException e) |
| 175 |
{ |
| 176 |
e.printStackTrace(); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public String getServletInfo() |
| 183 |
{ |
| 184 |
return "Session Dump Servlet"; |
| 185 |
} |
| 186 |
|
| 187 |
private String getURI(HttpServletRequest request) |
| 188 |
{ |
| 189 |
String uri = (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI); |
| 190 |
if (uri == null) |
| 191 |
uri = request.getRequestURI(); |
| 192 |
return uri; |
| 193 |
} |
| 194 |
} |