1 22 package org.jboss.ejb3.test.dd.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.security.Principal ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession; 37 38 43 public class SecureEJBServlet extends HttpServlet 44 { 45 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException , IOException 47 { 48 String echoMsg = null; 49 boolean testPropagation = false; 50 boolean includeHead = true; 51 String param = request.getParameter("testPropagation"); 52 if( param != null ) 53 testPropagation = Boolean.valueOf(param).booleanValue(); 54 param = request.getParameter("includeHead"); 55 if( param != null ) 56 includeHead = Boolean.valueOf(param).booleanValue(); 57 58 try 59 { 60 InitialContext ctx = new InitialContext (); 61 if( testPropagation == true ) 62 { 63 StatelessSession bean = (StatelessSession)ctx.lookup("java:comp/env/ejb/UnsecuredEJB"); 64 echoMsg = bean.forward("SecureEJBServlet called UnsecuredEJB.forward"); 65 } 66 else 67 { 68 StatelessSession bean = (StatelessSession)ctx.lookup("java:comp/env/ejb/SecuredEJB"); 69 echoMsg = bean.echo("SecureEJBServlet called SecuredEJB.echo"); 70 } 71 } 72 catch(Exception e) 73 { 74 throw new ServletException ("Failed to call SecuredEJB.echo", e); 75 } 76 Principal user = request.getUserPrincipal(); 77 PrintWriter out = response.getWriter(); 78 if( includeHead == true ) 79 { 80 response.setContentType("text/html"); 81 out.println("<html>"); 82 out.println("<head><title>ENCServlet</title></head><body>"); 83 } 84 out.println("<h1>SecureServlet Accessed</h1>"); 85 out.println("<pre>You have accessed this servlet as user: "+user); 86 out.println("You have accessed SecuredEJB as user: "+echoMsg); 87 out.println("</pre>"); 88 if( includeHead == true ) 89 out.println("</pre></body></html>"); 90 out.close(); 91 } 92 93 protected void doGet(HttpServletRequest request, HttpServletResponse response) 94 throws ServletException , IOException 95 { 96 processRequest(request, response); 97 } 98 99 protected void doPost(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException , IOException 101 { 102 processRequest(request, response); 103 } 104 105 } 106 | Popular Tags |