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.InitialContext ; 28 import javax.naming.Context ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.jboss.test.web.interfaces.StatelessSessionLocalHome; 35 import org.jboss.test.web.interfaces.StatelessSessionLocal; 36 import org.jboss.logging.Logger; 37 38 44 public class UnsecureEJBServlet extends HttpServlet 45 { 46 Logger log = Logger.getLogger(UnsecureEJBServlet.class); 47 48 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 49 throws ServletException , IOException 50 { 51 boolean includeHead = true; 52 String param = request.getParameter("includeHead"); 53 if( param != null ) 54 includeHead = Boolean.valueOf(param).booleanValue(); 55 String method = request.getParameter("method"); 56 if( method == null ) 57 method = "echo"; 58 59 try 60 { 61 InitialContext ctx = new InitialContext (); 62 StatelessSessionLocalHome home = null; 63 Context enc = (Context ) ctx.lookup("java:comp/env"); 64 home = (StatelessSessionLocalHome) enc.lookup("ejb/local/SecuredEJB"); 65 StatelessSessionLocal bean = home.create(); 66 if( method.equals("echo") ) 67 bean.echo("UnsecureEJBServlet called SecuredEJB.echo"); 68 else if( method.equals("unchecked") ) 69 bean.unchecked(); 70 else if( method.equals("checkRunAs") ) 71 bean.checkRunAs(); 72 else 73 throw new IllegalArgumentException ("method must be one of: echo, unchecked, checkRunAs"); 74 } 75 catch(Exception e) 76 { 77 log.error("Access to failed to method: "+method, e); 78 throw new ServletException ("Access to failed to method: "+method, e); 79 } 80 81 Principal user = request.getUserPrincipal(); 82 PrintWriter out = response.getWriter(); 83 if( includeHead == true ) 84 { 85 response.setContentType("text/html"); 86 out.println("<html>"); 87 out.println("<head><title>UnsecureEJBServlet</title></head><body>"); 88 } 89 out.println("<h1>UnsecureEJBServlet Accessed</h1>"); 90 out.println("<pre>You have accessed this servlet as user: "+user+"<br>"); 91 out.println("You have accessed SecuredEJB as user: "+user); 92 out.println("You have invoked SecuredEJB."+method); 93 out.println("</pre>"); 94 if( includeHead == true ) 95 out.println("</pre></body></html>"); 96 out.close(); 97 } 98 99 protected void doGet(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException , IOException 101 { 102 processRequest(request, response); 103 } 104 105 protected void doPost(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException , IOException 107 { 108 processRequest(request, response); 109 } 110 111 } 112 | Popular Tags |