| 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.test; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.List; |
| 20 |
import javax.naming.InitialContext; |
| 21 |
import javax.sql.DataSource; |
| 22 |
|
| 23 |
import jakarta.annotation.PostConstruct; |
| 24 |
import jakarta.annotation.PreDestroy; |
| 25 |
import jakarta.annotation.Resource; |
| 26 |
import jakarta.annotation.security.DeclareRoles; |
| 27 |
import jakarta.annotation.security.RunAs; |
| 28 |
import jakarta.servlet.ServletConfig; |
| 29 |
import jakarta.servlet.ServletException; |
| 30 |
import jakarta.servlet.ServletOutputStream; |
| 31 |
import jakarta.servlet.annotation.WebInitParam; |
| 32 |
import jakarta.servlet.annotation.WebServlet; |
| 33 |
import jakarta.servlet.http.HttpServlet; |
| 34 |
import jakarta.servlet.http.HttpServletRequest; |
| 35 |
import jakarta.servlet.http.HttpServletResponse; |
| 36 |
import jakarta.transaction.UserTransaction; |
| 37 |
|
| 38 |
/** |
| 39 |
* AnnotationTest |
| 40 |
* |
| 41 |
* Use Annotations from within Jetty. |
| 42 |
* |
| 43 |
* Also, use servlet spec 2.5 resource injection and lifecycle callbacks from within the web.xml |
| 44 |
* to set up some of the JNDI resources. |
| 45 |
*/ |
| 46 |
|
| 47 |
@RunAs("special") |
| 48 |
@WebServlet(urlPatterns = {"/", "/test/*"}, name = "AnnotationTest", initParams = { |
| 49 |
@WebInitParam(name = "fromAnnotation", value = "xyz") |
| 50 |
}) |
| 51 |
@DeclareRoles({"user", "client"}) |
| 52 |
public class AnnotationTest extends HttpServlet |
| 53 |
{ |
| 54 |
static List<String> __HandlesTypes; |
| 55 |
private String postConstructResult = ""; |
| 56 |
private String dsResult = ""; |
| 57 |
private String envResult = ""; |
| 58 |
private String envLookupResult = ""; |
| 59 |
private String envResult2 = ""; |
| 60 |
private String envLookupResult2 = ""; |
| 61 |
private String envResult3 = ""; |
| 62 |
private String envLookupResult3 = ""; |
| 63 |
private String dsLookupResult = ""; |
| 64 |
private String txResult = ""; |
| 65 |
private String txLookupResult = ""; |
| 66 |
private DataSource myDS; |
| 67 |
private ServletConfig config; |
| 68 |
|
| 69 |
@Resource(mappedName = "UserTransaction") |
| 70 |
private UserTransaction myUserTransaction; |
| 71 |
|
| 72 |
@Resource(mappedName = "maxAmount") |
| 73 |
private Double maxAmount; |
| 74 |
|
| 75 |
@Resource(name = "someAmount") |
| 76 |
private Double minAmount; |
| 77 |
|
| 78 |
@Resource |
| 79 |
private Double avgAmount; |
| 80 |
|
| 81 |
@Resource(mappedName = "jdbc/mydatasource") |
| 82 |
public void setMyDatasource(DataSource ds) |
| 83 |
{ |
| 84 |
myDS = ds; |
| 85 |
} |
| 86 |
|
| 87 |
@PostConstruct |
| 88 |
private void myPostConstructMethod() |
| 89 |
{ |
| 90 |
postConstructResult = "<span class=\"pass\">PASS</span>"; |
| 91 |
try |
| 92 |
{ |
| 93 |
dsResult = (myDS == null ? "<span class=\"fail\">FAIL</span>" : "<span class=\"pass\">myDS=" + myDS.toString() + "</span>"); |
| 94 |
} |
| 95 |
catch (Exception e) |
| 96 |
{ |
| 97 |
dsResult = "<span class=\"fail\">FAIL:</span> " + e; |
| 98 |
} |
| 99 |
|
| 100 |
envResult = (maxAmount == null ? "FAIL</span>" : "<span class=\"pass\">maxAmount=" + maxAmount.toString() + "</span>"); |
| 101 |
|
| 102 |
try |
| 103 |
{ |
| 104 |
InitialContext ic = new InitialContext(); |
| 105 |
envLookupResult = "java:comp/env/org.example.test.AnnotationTest/maxAmount=" + ic.lookup("java:comp/env/org.example.test.AnnotationTest/maxAmount"); |
| 106 |
} |
| 107 |
catch (Exception e) |
| 108 |
{ |
| 109 |
envLookupResult = "<span class=\"fail\">FAIL:</span> " + e; |
| 110 |
} |
| 111 |
|
| 112 |
envResult2 = (minAmount == null ? "<span class=\"fail\">FAIL</span>" : "<span class=\"pass\">minAmount=" + minAmount.toString() + "</span>"); |
| 113 |
try |
| 114 |
{ |
| 115 |
InitialContext ic = new InitialContext(); |
| 116 |
envLookupResult2 = "java:comp/env/someAmount=" + ic.lookup("java:comp/env/someAmount"); |
| 117 |
} |
| 118 |
catch (Exception e) |
| 119 |
{ |
| 120 |
envLookupResult2 = "<span class=\"fail\">FAIL:</span> " + e; |
| 121 |
} |
| 122 |
envResult3 = (minAmount == null ? "<span class=\"fail\">FAIL</span>" : "<span class=\"pass\">avgAmount=" + avgAmount.toString() + "</span>"); |
| 123 |
try |
| 124 |
{ |
| 125 |
InitialContext ic = new InitialContext(); |
| 126 |
envLookupResult3 = "java:comp/env/org.example.test.AnnotationTest/avgAmount=" + ic.lookup("java:comp/env/org.example.test.AnnotationTest/avgAmount"); |
| 127 |
} |
| 128 |
catch (Exception e) |
| 129 |
{ |
| 130 |
envLookupResult3 = "<span class=\"fail\">FAIL:</span> " + e; |
| 131 |
} |
| 132 |
|
| 133 |
try |
| 134 |
{ |
| 135 |
InitialContext ic = new InitialContext(); |
| 136 |
dsLookupResult = "java:comp/env/org.example.test.AnnotationTest/myDatasource=" + ic.lookup("java:comp/env/org.example.test.AnnotationTest/myDatasource"); |
| 137 |
} |
| 138 |
catch (Exception e) |
| 139 |
{ |
| 140 |
dsLookupResult = "<span class=\"fail\">FAIL:</span> " + e; |
| 141 |
} |
| 142 |
|
| 143 |
txResult = (myUserTransaction == null ? "<span class=\"fail\">FAIL</span>" : "<span class=\"pass\">myUserTransaction=" + myUserTransaction + "</span>"); |
| 144 |
try |
| 145 |
{ |
| 146 |
InitialContext ic = new InitialContext(); |
| 147 |
txLookupResult = "java:comp/env/org.example.test.AnnotationTest/myUserTransaction=" + ic.lookup("java:comp/env/org.example.test.AnnotationTest/myUserTransaction"); |
| 148 |
} |
| 149 |
catch (Exception e) |
| 150 |
{ |
| 151 |
txLookupResult = "<span class=\"fail\">FAIL:</span> " + e; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@PreDestroy |
| 156 |
private void myPreDestroyMethod() |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void init(ServletConfig config) throws ServletException |
| 162 |
{ |
| 163 |
super.init(config); |
| 164 |
this.config = config; |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 169 |
{ |
| 170 |
doGet(request, response); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 175 |
{ |
| 176 |
try |
| 177 |
{ |
| 178 |
response.setContentType("text/html"); |
| 179 |
ServletOutputStream out = response.getOutputStream(); |
| 180 |
out.println("<html>"); |
| 181 |
out.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>"); |
| 182 |
out.println("<body>"); |
| 183 |
out.println("<h1>Results</h1>"); |
| 184 |
|
| 185 |
out.println("<h2>Context Defaults</h2>"); |
| 186 |
out.println("<p><b>default-context-path: " + |
| 187 |
(request.getServletContext().getAttribute("default-context-path") != null ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + |
| 188 |
"</span></p>"); |
| 189 |
out.println("<p><b>request-character-encoding: " + |
| 190 |
("utf-8".equals(request.getServletContext().getAttribute("request-character-encoding")) ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + |
| 191 |
"</span></p>"); |
| 192 |
out.println("<p><b>response-character-encoding: " + |
| 193 |
("utf-8".equals(request.getServletContext().getAttribute("response-character-encoding")) ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + |
| 194 |
"</span></p>"); |
| 195 |
|
| 196 |
out.println("<h2>Init Params from Annotation</h2>"); |
| 197 |
out.println("<pre>"); |
| 198 |
out.println("initParams={@WebInitParam(name=\"fromAnnotation\", value=\"xyz\")}"); |
| 199 |
out.println("</pre>"); |
| 200 |
out.println("<p><b>Result: " + ("xyz".equals(config.getInitParameter("fromAnnotation")) ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></p>"); |
| 201 |
|
| 202 |
out.println("<h2>Init Params from web-fragment</h2>"); |
| 203 |
out.println("<pre>"); |
| 204 |
out.println("extra1=123, extra2=345"); |
| 205 |
out.println("</pre>"); |
| 206 |
boolean fragInitParamResult = "123".equals(config.getInitParameter("extra1")) && "345".equals(config.getInitParameter("extra2")); |
| 207 |
out.println("<p><b>Result: " + (fragInitParamResult ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></p>"); |
| 208 |
|
| 209 |
__HandlesTypes = Arrays.asList("jakarta.servlet.GenericServlet", |
| 210 |
"jakarta.servlet.http.HttpServlet", |
| 211 |
"org.example.test.AsyncListenerServlet", |
| 212 |
"org.example.test.ClassLoaderServlet", |
| 213 |
"org.example.test.AnnotationTest", |
| 214 |
"org.example.test.RoleAnnotationTest", |
| 215 |
"org.example.test.MultiPartTest", |
| 216 |
"org.example.fragment.FragmentServlet", |
| 217 |
"org.example.test.TestListener", |
| 218 |
"org.example.test.SecuredServlet", |
| 219 |
"org.example.test.Bar"); |
| 220 |
out.println("<h2>@ContainerInitializer</h2>"); |
| 221 |
out.println("<pre>"); |
| 222 |
out.println("@HandlesTypes({jakarta.servlet.Servlet.class, Foo.class})"); |
| 223 |
out.println("</pre>"); |
| 224 |
out.print("<p><b>Result: "); |
| 225 |
List<Class> classes = (List<Class>)config.getServletContext().getAttribute("org.example.Foo"); |
| 226 |
List<String> classNames = new ArrayList<String>(); |
| 227 |
if (classes != null) |
| 228 |
{ |
| 229 |
for (Class c : classes) |
| 230 |
{ |
| 231 |
classNames.add(c.getName()); |
| 232 |
out.print(c.getName() + " "); |
| 233 |
} |
| 234 |
|
| 235 |
if (classNames.size() != __HandlesTypes.size()) |
| 236 |
out.println("<br/><span class=\"fail\">FAIL</span>"); |
| 237 |
else if (!classNames.containsAll(__HandlesTypes)) |
| 238 |
out.println("<br/><span class=\"fail\">FAIL</span>"); |
| 239 |
else |
| 240 |
out.println("<br/><span class=\"pass\">PASS</span>"); |
| 241 |
} |
| 242 |
else |
| 243 |
out.print("<br/><span class=\"fail\">FAIL</span> (No such attribute org.example.Foo)"); |
| 244 |
out.println("</b></p>"); |
| 245 |
|
| 246 |
out.println("<h2>Complete Servlet Registration</h2>"); |
| 247 |
Boolean complete = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.complete"); |
| 248 |
out.println("<p><b>Result: " + (complete.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 249 |
|
| 250 |
out.println("<h2>ServletContextListener Programmatic Registration from ServletContainerInitializer</h2>"); |
| 251 |
Boolean programmaticListener = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.listenerTest"); |
| 252 |
out.println("<p><b>Result: " + (programmaticListener.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 253 |
|
| 254 |
out.println("<h2>ServletContextListener Programmatic Registration Prevented from ServletContextListener</h2>"); |
| 255 |
Boolean programmaticListenerPrevention = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.listenerRegoTest"); |
| 256 |
out.println("<p><b>Result: " + (programmaticListenerPrevention.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 257 |
|
| 258 |
out.println("<h2>ServletContextListener Registration Prevented from ServletContextListener</h2>"); |
| 259 |
Boolean webListenerPrevention = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.sclFromSclRegoTest"); |
| 260 |
out.println("<p><b>Result: " + (webListenerPrevention.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 261 |
|
| 262 |
out.println("<h2>Add Jsp File Registration</h2>"); |
| 263 |
complete = (Boolean)config.getServletContext().getAttribute("org.example.jsp.file"); |
| 264 |
out.println("<p><b>Result: " + (complete.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 265 |
|
| 266 |
out.println("<h2>ServletContextListener In web.xml Injected</h2>"); |
| 267 |
Boolean listenerInject = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.sclInjectTest"); |
| 268 |
out.println("<p><b>Result: " + (listenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 269 |
|
| 270 |
out.println("<h2>ServletContextListener as @WebListener Injected</h2>"); |
| 271 |
Boolean annotatedListenerInject = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.sclInjectWebListenerTest"); |
| 272 |
out.println("<p><b>Result: " + (annotatedListenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 273 |
|
| 274 |
out.println("<h2>ServletContextListener as @WebListener Get/Set Session Timeout</h2>"); |
| 275 |
out.println("<p><b>getSessionTimeout Result: " + |
| 276 |
((Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.sclGetSessionTimeout") ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 277 |
out.println("<p><b>setSessionTimeout Result: " + |
| 278 |
((Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.sclSetSessionTimeout") ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 279 |
|
| 280 |
out.println("<h2>Programmatic Listener Injected</h2>"); |
| 281 |
Boolean programListenerInject = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.programListenerInjectTest"); |
| 282 |
out.println("<p><b>Result: " + (programListenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 283 |
|
| 284 |
out.println("<h2>Invalid Type for Listener Detection</h2>"); |
| 285 |
Boolean badListener = (Boolean)config.getServletContext().getAttribute("org.example.AnnotationTest.invalidListenerRegoTest"); |
| 286 |
out.println("<p><b>Result: " + (badListener.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>"); |
| 287 |
|
| 288 |
out.println("<h2>@PostConstruct Callback</h2>"); |
| 289 |
out.println("<pre>"); |
| 290 |
out.println("@PostConstruct"); |
| 291 |
out.println("private void myPostConstructMethod ()"); |
| 292 |
out.println("{}"); |
| 293 |
out.println("</pre>"); |
| 294 |
out.println("<p><b>Result: " + postConstructResult + "</b></p>"); |
| 295 |
|
| 296 |
out.println("<h2>@Resource Injection for DataSource</h2>"); |
| 297 |
out.println("<pre>"); |
| 298 |
out.println("@Resource(mappedName=\"jdbc/mydatasource\");"); |
| 299 |
out.println("public void setMyDatasource(DataSource ds)"); |
| 300 |
out.println("{"); |
| 301 |
out.println("myDS=ds;"); |
| 302 |
out.println("}"); |
| 303 |
out.println("</pre>"); |
| 304 |
out.println("<p><b>Result: " + dsResult + "</b>"); |
| 305 |
out.println("<br/><b>JNDI Lookup Result: " + dsLookupResult + "</b></p>"); |
| 306 |
|
| 307 |
out.println("<h2>@Resource Injection for env-entry </h2>"); |
| 308 |
out.println("<pre>"); |
| 309 |
out.println("@Resource(mappedName=\"maxAmount\")"); |
| 310 |
out.println("private Double maxAmount;"); |
| 311 |
out.println("@Resource(name=\"minAmount\")"); |
| 312 |
out.println("private Double minAmount;"); |
| 313 |
out.println("</pre>"); |
| 314 |
if (maxAmount == null) |
| 315 |
out.println("<p><b>Result: " + envResult + ": <span class=\"fail\">FAIL</span>"); |
| 316 |
else |
| 317 |
out.println("<p><b>Result: " + envResult + ": " + (maxAmount.compareTo(55D) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 318 |
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult + "</b>"); |
| 319 |
|
| 320 |
if (minAmount == null) |
| 321 |
out.println("<p><b>Result: " + envResult2 + ": <span class=\"fail\">FAIL</span>"); |
| 322 |
else |
| 323 |
out.println("<br/><b>Result: " + envResult2 + ": " + (minAmount.compareTo(0.99D) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 324 |
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult2 + "</b>"); |
| 325 |
|
| 326 |
if (avgAmount == null) |
| 327 |
out.println("<p><b>Result: " + envResult3 + ": <span class=\"fail\">FAIL</span>"); |
| 328 |
else |
| 329 |
out.println("<br/><b>Result: " + envResult3 + ": " + (avgAmount.compareTo(1.25D) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>"); |
| 330 |
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult3 + "</b></p>"); |
| 331 |
|
| 332 |
out.println("<h2>@Resource Injection for UserTransaction </h2>"); |
| 333 |
out.println("<pre>"); |
| 334 |
out.println("@Resource(mappedName=\"UserTransaction\")"); |
| 335 |
out.println("private UserTransaction myUserTransaction;"); |
| 336 |
out.println("</pre>"); |
| 337 |
out.println("<p><b>Result: " + txResult + "</b>"); |
| 338 |
out.println("<br/><b>JNDI Lookup Result: " + txLookupResult + "</b></p>"); |
| 339 |
|
| 340 |
out.println("</body>"); |
| 341 |
out.println("</html>"); |
| 342 |
out.flush(); |
| 343 |
} |
| 344 |
catch (Exception e) |
| 345 |
{ |
| 346 |
throw new ServletException(e); |
| 347 |
} |
| 348 |
} |
| 349 |
} |