Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package myapp; |
| 18 | |
| 19 | import java.io.*; |
| 20 | import java.net.*; |
| 21 | import java.security.AccessControlException; |
| 22 | import java.security.AccessController; |
| 23 | |
Gaurav Gupta | ed5e731 | 2020-05-01 07:54:26 +0530 | [diff] [blame] | 24 | import jakarta.ejb.EJB; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 25 | import javax.naming.*; |
| 26 | |
Gaurav Gupta | eea72b5 | 2020-05-05 18:13:58 +0530 | [diff] [blame] | 27 | import jakarta.servlet.*; |
| 28 | import jakarta.servlet.http.*; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 29 | |
| 30 | public class TestServlet extends HttpServlet { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 31 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 32 | @EJB |
| 33 | private BeanRootInterface root; |
| 34 | |
| 35 | @EJB |
| 36 | private BeanMessageInterface msg; |
| 37 | |
| 38 | private ServletContext sc; |
| 39 | |
| 40 | private String message; |
| 41 | |
| 42 | public void init(ServletConfig config) throws ServletException { |
| 43 | super.init(config); |
| 44 | sc = config.getServletContext(); |
| 45 | message = msg.getMessage(); |
| 46 | System.out.println("servlet init: message="+message); |
| 47 | } |
| 48 | |
| 49 | protected void processRequest(HttpServletRequest request, |
| 50 | HttpServletResponse response) throws ServletException, IOException { |
| 51 | response.setContentType("text/plain"); |
| 52 | PrintWriter out = response.getWriter(); |
| 53 | String EXPECTED_RESULT = "PostBeanRootPostBeanLeafHelloBeanLeaf"; |
| 54 | boolean status = false; |
| 55 | |
| 56 | try { |
| 57 | |
| 58 | String testcase = request.getParameter("tc"); |
| 59 | out.println("testcase = " + testcase); |
| 60 | out.println("TestServlet"); |
| 61 | out.println("contextPath=" + request.getContextPath()); |
| 62 | |
| 63 | if (testcase != null) { |
| 64 | |
| 65 | if ("InjectLookup".equals(testcase)) { |
| 66 | // EJB injection check |
| 67 | // out.println("injected root: " + root); |
| 68 | String hello = root.sayHello(); |
| 69 | out.println("Hello from injected bean: " + hello); |
| 70 | |
| 71 | // EJB lookup check |
| 72 | InitialContext ic = new InitialContext(); |
| 73 | // "java"glabal[/<app-name>]/<module-name>/<bean-name>" |
| 74 | // app-name -- name of ear file (option) |
| 75 | // module-name -- name of war or jar file |
| 76 | // bean-name -- name of ejb |
| 77 | BeanRootInterface root2 = (BeanRootInterface) ic |
| 78 | .lookup("java:global/appperms/apppermsEJB/BeanRoot"); |
| 79 | |
| 80 | // out.println("global root: " + root2); |
| 81 | String hello2 = root2.sayHello(); |
| 82 | out.println("Hello from lookup bean: " + hello2); |
| 83 | |
| 84 | StringBuffer checkReport = new StringBuffer(" -Servlet test- "); |
| 85 | FilePermission fp = new FilePermission( |
| 86 | "/scratch/spei/bug/test/war.txt", "delete"); |
| 87 | try { |
| 88 | if (System.getSecurityManager() != null) { |
| 89 | AccessController.checkPermission(fp); |
| 90 | checkReport.append("servlet - success for WAR.txt; "); |
| 91 | } else |
| 92 | checkReport.append("servlet - bypass for WAR.txt; "); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 93 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 94 | } catch (AccessControlException e) { |
| 95 | checkReport.append("servlet - failed for WAR.txt; "); |
| 96 | } |
| 97 | |
| 98 | fp = new FilePermission("/scratch/spei/bug/test/ear.txt", |
| 99 | "delete"); |
| 100 | try { |
| 101 | if (System.getSecurityManager() != null) { |
| 102 | AccessController.checkPermission(fp); |
| 103 | checkReport.append("servlet - success for EAR.txt; "); |
| 104 | } else |
| 105 | checkReport.append("servlet - bypass for EAR.txt; "); |
| 106 | } catch (AccessControlException e) { |
| 107 | checkReport.append("servlet - failed for EAR.txt; "); |
| 108 | } |
| 109 | |
| 110 | fp = new FilePermission("/scratch/spei/bug/test/ejb.txt", |
| 111 | "delete"); |
| 112 | try { |
| 113 | if (System.getSecurityManager() != null) { |
| 114 | AccessController.checkPermission(fp); |
| 115 | checkReport.append("servlet - success for EJB.txt; "); |
| 116 | } else |
| 117 | checkReport.append("servlet - bypass for EJB.txt; "); |
| 118 | } catch (AccessControlException e) { |
| 119 | checkReport.append("servlet - failed for EJB.txt; "); |
| 120 | } |
| 121 | |
| 122 | String crStr = checkReport.toString(); |
| 123 | out.println("test: " + crStr); |
| 124 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 125 | |
| 126 | |
| 127 | if (hello.equals(hello2) && |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 128 | !crStr.contains("failed") && |
| 129 | !hello.contains("failed")) { |
| 130 | status = true; |
| 131 | } |
| 132 | } else if ("Startup".equals(testcase)) { |
| 133 | // deployment check for startup |
| 134 | out.println("message by deployment: " + message); |
| 135 | if (message != null && message.equals(EXPECTED_RESULT)) { |
| 136 | status = true; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } catch (Throwable th) { |
| 142 | th.printStackTrace(out); |
| 143 | } finally { |
| 144 | if (status) { |
| 145 | out.println("Test:Pass"); |
| 146 | } else { |
| 147 | out.println("Test:Fail"); |
| 148 | } |
| 149 | out.close(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | protected void doGet(HttpServletRequest request, |
| 154 | HttpServletResponse response) throws ServletException, IOException { |
| 155 | processRequest(request, response); |
| 156 | } |
| 157 | |
| 158 | protected void doPost(HttpServletRequest request, |
| 159 | HttpServletResponse response) throws ServletException, IOException { |
| 160 | processRequest(request, response); |
| 161 | } |
| 162 | |
| 163 | public String getServletInfo() { |
| 164 | return "Short description"; |
| 165 | } |
| 166 | |
| 167 | } |