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.RunAsTargetLocalHome; 35 import org.jboss.test.web.interfaces.RunAsTargetLocal; 36 import org.jboss.logging.Logger; 37 38 44 public class UnsecureRunAsServlet extends HttpServlet 45 { 46 Logger log = Logger.getLogger(UnsecureRunAsServlet.class); 47 48 52 public void init() throws ServletException 53 { 54 String ejbName = super.getInitParameter("ejbName"); 55 try 56 { 57 InitialContext ctx = new InitialContext (); 58 RunAsTargetLocalHome home = null; 59 Context enc = (Context ) ctx.lookup("java:comp/env"); 60 home = (RunAsTargetLocalHome) enc.lookup(ejbName); 61 RunAsTargetLocal bean = home.create(); 62 bean.checkRunAs(); 63 } 64 catch(Exception e) 65 { 66 throw new ServletException ("Access to failed to method: checkRunAs", e); 67 } 68 } 69 70 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 71 throws ServletException , IOException 72 { 73 String ejbName = request.getParameter("ejbName"); 74 try 75 { 76 InitialContext ctx = new InitialContext (); 77 RunAsTargetLocalHome home = null; 78 Context enc = (Context ) ctx.lookup("java:comp/env"); 79 home = (RunAsTargetLocalHome) enc.lookup(ejbName); 80 RunAsTargetLocal bean = home.create(); 81 bean.checkRunAs(); 82 } 83 catch(Exception e) 84 { 85 log.error("Access to checkRunAs failed", e); 86 throw new ServletException ("Access to checkRunAs failed", e); 87 } 88 89 Principal user = request.getUserPrincipal(); 90 PrintWriter out = response.getWriter(); 91 response.setContentType("text/html"); 92 out.println("<html>"); 93 out.println("<head><title>UnsecureRunAsServlet</title></head><body>"); 94 out.println("<h1>UnsecureRunAsServlet Accessed</h1>"); 95 out.println("<pre>You have accessed this servlet as user: "+user+"<br>"); 96 out.println("</pre>"); 97 out.println("</pre></body></html>"); 98 out.close(); 99 } 100 101 protected void doGet(HttpServletRequest request, HttpServletResponse response) 102 throws ServletException , IOException 103 { 104 processRequest(request, response); 105 } 106 107 protected void doPost(HttpServletRequest request, HttpServletResponse response) 108 throws ServletException , IOException 109 { 110 processRequest(request, response); 111 } 112 113 } 114 | Popular Tags |