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