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