blob: dbde0cd6473e4860f89602dccab1c6ba460d1eb4 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
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
17package myapp;
18
19import java.io.*;
20import java.net.*;
21import java.security.AccessControlException;
22import java.security.AccessController;
23
Gaurav Guptaed5e7312020-05-01 07:54:26 +053024import jakarta.ejb.EJB;
Vinay Vishal57171472018-09-18 20:22:00 +053025import javax.naming.*;
26
Gaurav Guptaeea72b52020-05-05 18:13:58 +053027import jakarta.servlet.*;
28import jakarta.servlet.http.*;
Vinay Vishal57171472018-09-18 20:22:00 +053029
30public class TestServlet extends HttpServlet {
David Matějčekf4dc06a2021-05-17 12:10:57 +020031
Vinay Vishal57171472018-09-18 20:22:00 +053032 @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čekf4dc06a2021-05-17 12:10:57 +020093
Vinay Vishal57171472018-09-18 20:22:00 +053094 } 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čekf4dc06a2021-05-17 12:10:57 +0200125
126
127 if (hello.equals(hello2) &&
Vinay Vishal57171472018-09-18 20:22:00 +0530128 !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}