1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.StringWriter ; 27 import java.security.Principal ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.servlet.ServletConfig ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 38 import org.jboss.test.web.interfaces.StatelessSession; 39 import org.jboss.test.web.interfaces.StatelessSessionHome; 40 41 47 public class SecureEJBServletMT extends HttpServlet 48 { 49 static org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(SecureEJBServletMT.class); 50 51 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 52 throws ServletException , IOException 53 { 54 HttpSession session = request.getSession(); 55 Principal user = request.getUserPrincipal(); 56 Object result = session.getAttribute("request.result"); 57 58 response.setContentType("text/html"); 59 PrintWriter out = response.getWriter(); 60 out.println("<html>"); 61 out.println("<head><title>SecureEJBServletMT</title></head>"); 62 if( result == null ) 63 out.println("<meta http-equiv='refresh' content='5'>"); 64 out.println("<h1>SecureEJBServletMT Accessed</h1>"); 65 out.println("<body><pre>You have accessed this servlet as user: "+user); 66 67 if( result == null ) 68 { 69 Worker worker = new Worker(session); 70 out.println("Started worker thread..."); 71 Thread t = new Thread (worker, "Worker"); 72 t.start(); 73 } 74 else if( result instanceof Exception ) 75 { 76 StringWriter sw = new StringWriter (); 77 PrintWriter pw = new PrintWriter (sw); 78 Exception e = (Exception ) result; 79 e.printStackTrace(pw); 80 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, sw.toString()); 81 } 82 else 83 { 84 out.println("Finished request, result = "+result); 85 } 86 out.println("</pre></body></html>"); 87 out.close(); 88 } 89 90 protected void doGet(HttpServletRequest request, HttpServletResponse response) 91 throws ServletException , IOException 92 { 93 processRequest(request, response); 94 } 95 96 protected void doPost(HttpServletRequest request, HttpServletResponse response) 97 throws ServletException , IOException 98 { 99 processRequest(request, response); 100 } 101 102 static class Worker implements Runnable 103 { 104 HttpSession session; 105 Worker(HttpSession session) 106 { 107 this.session = session; 108 } 109 public void run() 110 { 111 try 112 { 113 log.debug("Worker, start: "+System.currentTimeMillis()); 114 Thread.currentThread().sleep(2500); 115 InitialContext ctx = new InitialContext (); 116 StatelessSessionHome home = home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/SecuredEJB"); 117 StatelessSession bean = home.create(); 118 String echoMsg = bean.echo("SecureEJBServlet called SecuredEJB.echo"); 119 session.setAttribute("request.result", echoMsg); 120 } 121 catch(Exception e) 122 { 123 session.setAttribute("request.result", e); 124 } 125 finally 126 { 127 log.debug("Worker, end: "+System.currentTimeMillis()); 128 } 129 } 130 } 131 } 132 | Popular Tags |