| 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.OutputStreamWriter; |
| 18 |
import java.io.PrintWriter; |
| 19 |
|
| 20 |
import jakarta.servlet.ServletConfig; |
| 21 |
import jakarta.servlet.ServletException; |
| 22 |
import jakarta.servlet.http.HttpServlet; |
| 23 |
import jakarta.servlet.http.HttpServletRequest; |
| 24 |
import jakarta.servlet.http.HttpServletResponse; |
| 25 |
import org.eclipse.jetty.util.StringUtil; |
| 26 |
|
| 27 |
/** |
| 28 |
* Rego Servlet - tests being accessed from servlet 3.0 programmatic |
| 29 |
* configuration. |
| 30 |
*/ |
| 31 |
public class RegTest extends HttpServlet |
| 32 |
{ |
| 33 |
|
| 34 |
@Override |
| 35 |
public void init(ServletConfig config) throws ServletException |
| 36 |
{ |
| 37 |
super.init(config); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 42 |
{ |
| 43 |
doGet(request, response); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException |
| 48 |
{ |
| 49 |
request.setCharacterEncoding("UTF-8"); |
| 50 |
PrintWriter pout = null; |
| 51 |
|
| 52 |
try |
| 53 |
{ |
| 54 |
pout = response.getWriter(); |
| 55 |
} |
| 56 |
catch (IllegalStateException e) |
| 57 |
{ |
| 58 |
pout = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")); |
| 59 |
} |
| 60 |
|
| 61 |
try |
| 62 |
{ |
| 63 |
pout.write("<html>\n<body>\n"); |
| 64 |
pout.write("<h1>Rego Servlet</h1>\n"); |
| 65 |
pout.write("<table width=\"95%\">"); |
| 66 |
pout.write("<tr>\n"); |
| 67 |
pout.write("<th align=\"right\">getMethod: </th>"); |
| 68 |
pout.write("<td>" + notag(request.getMethod()) + "</td>"); |
| 69 |
pout.write("</tr><tr>\n"); |
| 70 |
pout.write("<th align=\"right\">getContentLength: </th>"); |
| 71 |
pout.write("<td>" + Integer.toString(request.getContentLength()) + "</td>"); |
| 72 |
pout.write("</tr><tr>\n"); |
| 73 |
pout.write("<th align=\"right\">getContentType: </th>"); |
| 74 |
pout.write("<td>" + notag(request.getContentType()) + "</td>"); |
| 75 |
pout.write("</tr><tr>\n"); |
| 76 |
pout.write("<th align=\"right\">getRequestURI: </th>"); |
| 77 |
pout.write("<td>" + notag(request.getRequestURI()) + "</td>"); |
| 78 |
pout.write("</tr><tr>\n"); |
| 79 |
pout.write("<th align=\"right\">getRequestURL: </th>"); |
| 80 |
pout.write("<td>" + notag(request.getRequestURL().toString()) + "</td>"); |
| 81 |
pout.write("</tr><tr>\n"); |
| 82 |
pout.write("<th align=\"right\">getContextPath: </th>"); |
| 83 |
pout.write("<td>" + request.getContextPath() + "</td>"); |
| 84 |
pout.write("</tr><tr>\n"); |
| 85 |
pout.write("<th align=\"right\">getServletPath: </th>"); |
| 86 |
pout.write("<td>" + notag(request.getServletPath()) + "</td>"); |
| 87 |
pout.write("</tr><tr>\n"); |
| 88 |
pout.write("<th align=\"right\">getPathInfo: </th>"); |
| 89 |
pout.write("<td>" + notag(request.getPathInfo()) + "</td>"); |
| 90 |
pout.write("</tr><tr>\n"); |
| 91 |
pout.write("<th align=\"right\">getPathTranslated: </th>"); |
| 92 |
pout.write("<td>" + notag(request.getPathTranslated()) + "</td>"); |
| 93 |
pout.write("</tr><tr>\n"); |
| 94 |
pout.write("<th align=\"right\">getQueryString: </th>"); |
| 95 |
pout.write("<td>" + notag(request.getQueryString()) + "</td>"); |
| 96 |
pout.write("</tr><tr>\n"); |
| 97 |
|
| 98 |
pout.write("<th align=\"right\">getProtocol: </th>"); |
| 99 |
pout.write("<td>" + request.getProtocol() + "</td>"); |
| 100 |
pout.write("</tr><tr>\n"); |
| 101 |
pout.write("<th align=\"right\">getScheme: </th>"); |
| 102 |
pout.write("<td>" + request.getScheme() + "</td>"); |
| 103 |
pout.write("</tr><tr>\n"); |
| 104 |
pout.write("<th align=\"right\">getServerName: </th>"); |
| 105 |
pout.write("<td>" + notag(request.getServerName()) + "</td>"); |
| 106 |
pout.write("</tr><tr>\n"); |
| 107 |
pout.write("<th align=\"right\">getServerPort: </th>"); |
| 108 |
pout.write("<td>" + Integer.toString(request.getServerPort()) + "</td>"); |
| 109 |
pout.write("</tr><tr>\n"); |
| 110 |
pout.write("<th align=\"right\">getLocalName: </th>"); |
| 111 |
pout.write("<td>" + request.getLocalName() + "</td>"); |
| 112 |
pout.write("</tr><tr>\n"); |
| 113 |
pout.write("<th align=\"right\">getLocalAddr: </th>"); |
| 114 |
pout.write("<td>" + request.getLocalAddr() + "</td>"); |
| 115 |
pout.write("</tr><tr>\n"); |
| 116 |
pout.write("<th align=\"right\">getLocalPort: </th>"); |
| 117 |
pout.write("<td>" + Integer.toString(request.getLocalPort()) + "</td>"); |
| 118 |
pout.write("</tr><tr>\n"); |
| 119 |
pout.write("<th align=\"right\">getRemoteUser: </th>"); |
| 120 |
pout.write("<td>" + request.getRemoteUser() + "</td>"); |
| 121 |
pout.write("</tr><tr>\n"); |
| 122 |
pout.write("<th align=\"right\">getUserPrincipal: </th>"); |
| 123 |
pout.write("<td>" + request.getUserPrincipal() + "</td>"); |
| 124 |
pout.write("</tr><tr>\n"); |
| 125 |
pout.write("<th align=\"right\">getRemoteAddr: </th>"); |
| 126 |
pout.write("<td>" + request.getRemoteAddr() + "</td>"); |
| 127 |
pout.write("</tr><tr>\n"); |
| 128 |
pout.write("<th align=\"right\">getRemoteHost: </th>"); |
| 129 |
pout.write("<td>" + request.getRemoteHost() + "</td>"); |
| 130 |
pout.write("</tr><tr>\n"); |
| 131 |
pout.write("<th align=\"right\">getRemotePort: </th>"); |
| 132 |
pout.write("<td>" + request.getRemotePort() + "</td>"); |
| 133 |
pout.write("</tr><tr>\n"); |
| 134 |
pout.write("<th align=\"right\">getRequestedSessionId: </th>"); |
| 135 |
pout.write("<td>" + request.getRequestedSessionId() + "</td>"); |
| 136 |
pout.write("</tr><tr>\n"); |
| 137 |
pout.write("<th align=\"right\">isSecure(): </th>"); |
| 138 |
pout.write("<td>" + request.isSecure() + "</td>"); |
| 139 |
|
| 140 |
pout.write("</tr><tr>\n"); |
| 141 |
pout.write("<th align=\"right\">isUserInRole(admin): </th>"); |
| 142 |
pout.write("<td>" + request.isUserInRole("admin") + "</td>"); |
| 143 |
|
| 144 |
pout.write("</tr></table>"); |
| 145 |
} |
| 146 |
catch (Exception e) |
| 147 |
{ |
| 148 |
getServletContext().log("dump " + e); |
| 149 |
} |
| 150 |
|
| 151 |
pout.write("</body>\n</html>\n"); |
| 152 |
|
| 153 |
pout.close(); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public String getServletInfo() |
| 158 |
{ |
| 159 |
return "Rego Servlet"; |
| 160 |
} |
| 161 |
|
| 162 |
private String notag(String s) |
| 163 |
{ |
| 164 |
if (s == null) |
| 165 |
return "null"; |
| 166 |
s = StringUtil.replace(s, "&", "&"); |
| 167 |
s = StringUtil.replace(s, "<", "<"); |
| 168 |
s = StringUtil.replace(s, ">", ">"); |
| 169 |
return s; |
| 170 |
} |
| 171 |
} |