001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027: import java.security.Principal;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.servlet.ServletConfig;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpSession;
034: import javax.servlet.http.HttpServlet;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import org.jboss.test.web.interfaces.StatelessSession;
039: import org.jboss.test.web.interfaces.StatelessSessionHome;
040:
041: /** A servlet that spawns a thread to perform a long running task that
042: interacts with a secure EJB.
043:
044: @author Scott.Stark@jboss.org
045: @version $Revision: 57211 $
046: */
047: public class SecureEJBServletMT extends HttpServlet {
048: static org.apache.log4j.Category log = org.apache.log4j.Category
049: .getInstance(SecureEJBServletMT.class);
050:
051: protected void processRequest(HttpServletRequest request,
052: HttpServletResponse response) throws ServletException,
053: IOException {
054: HttpSession session = request.getSession();
055: Principal user = request.getUserPrincipal();
056: Object result = session.getAttribute("request.result");
057:
058: response.setContentType("text/html");
059: PrintWriter out = response.getWriter();
060: out.println("<html>");
061: out.println("<head><title>SecureEJBServletMT</title></head>");
062: if (result == null)
063: out.println("<meta http-equiv='refresh' content='5'>");
064: out.println("<h1>SecureEJBServletMT Accessed</h1>");
065: out
066: .println("<body><pre>You have accessed this servlet as user: "
067: + user);
068:
069: if (result == null) {
070: Worker worker = new Worker(session);
071: out.println("Started worker thread...");
072: Thread t = new Thread(worker, "Worker");
073: t.start();
074: } else if (result instanceof Exception) {
075: StringWriter sw = new StringWriter();
076: PrintWriter pw = new PrintWriter(sw);
077: Exception e = (Exception) result;
078: e.printStackTrace(pw);
079: response.sendError(
080: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, sw
081: .toString());
082: } else {
083: out.println("Finished request, result = " + result);
084: }
085: out.println("</pre></body></html>");
086: out.close();
087: }
088:
089: protected void doGet(HttpServletRequest request,
090: HttpServletResponse response) throws ServletException,
091: IOException {
092: processRequest(request, response);
093: }
094:
095: protected void doPost(HttpServletRequest request,
096: HttpServletResponse response) throws ServletException,
097: IOException {
098: processRequest(request, response);
099: }
100:
101: static class Worker implements Runnable {
102: HttpSession session;
103:
104: Worker(HttpSession session) {
105: this .session = session;
106: }
107:
108: public void run() {
109: try {
110: log.debug("Worker, start: "
111: + System.currentTimeMillis());
112: Thread.currentThread().sleep(2500);
113: InitialContext ctx = new InitialContext();
114: StatelessSessionHome home = home = (StatelessSessionHome) ctx
115: .lookup("java:comp/env/ejb/SecuredEJB");
116: StatelessSession bean = home.create();
117: String echoMsg = bean
118: .echo("SecureEJBServlet called SecuredEJB.echo");
119: session.setAttribute("request.result", echoMsg);
120: } catch (Exception e) {
121: session.setAttribute("request.result", e);
122: } finally {
123: log.debug("Worker, end: " + System.currentTimeMillis());
124: }
125: }
126: }
127: }
|