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