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.security.Principal;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.servlet.ServletConfig;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: import org.jboss.test.web.interfaces.StatelessSession;
037: import org.jboss.test.web.interfaces.StatelessSessionHome;
038:
039: /**
040: *
041: * @author Scott.Stark@jboss.org
042: * @version $Revision: 57211 $
043: */
044: public class SecureEJBServlet extends HttpServlet {
045: protected void processRequest(HttpServletRequest request,
046: HttpServletResponse response) throws ServletException,
047: IOException {
048: String echoMsg = null;
049: boolean testPropagation = false;
050: boolean includeHead = true;
051: String param = request.getParameter("testPropagation");
052: if (param != null)
053: testPropagation = Boolean.valueOf(param).booleanValue();
054: param = request.getParameter("includeHead");
055: if (param != null)
056: includeHead = Boolean.valueOf(param).booleanValue();
057:
058: try {
059: InitialContext ctx = new InitialContext();
060: StatelessSessionHome home = null;
061: if (testPropagation == true) {
062: home = (StatelessSessionHome) ctx
063: .lookup("java:comp/env/ejb/UnsecuredEJB");
064: StatelessSession bean = home.create();
065: echoMsg = bean
066: .forward("SecureEJBServlet called UnsecuredEJB.forward");
067: } else {
068: home = (StatelessSessionHome) ctx
069: .lookup("java:comp/env/ejb/SecuredEJB");
070: StatelessSession bean = home.create();
071: echoMsg = bean
072: .echo("SecureEJBServlet called SecuredEJB.echo");
073: }
074: } catch (Exception e) {
075: throw new ServletException(
076: "Failed to call SecuredEJB.echo", e);
077: }
078: Principal user = request.getUserPrincipal();
079: PrintWriter out = response.getWriter();
080: if (includeHead == true) {
081: response.setContentType("text/html");
082: out.println("<html>");
083: out.println("<head><title>ENCServlet</title></head><body>");
084: }
085: out.println("<h1>SecureServlet Accessed</h1>");
086: out.println("<pre>You have accessed this servlet as user: "
087: + user);
088: out.println("You have accessed SecuredEJB as user: " + echoMsg);
089: out.println("</pre>");
090: if (includeHead == true)
091: out.println("</pre></body></html>");
092: out.close();
093: }
094:
095: protected void doGet(HttpServletRequest request,
096: HttpServletResponse response) throws ServletException,
097: IOException {
098: processRequest(request, response);
099: }
100:
101: protected void doPost(HttpServletRequest request,
102: HttpServletResponse response) throws ServletException,
103: IOException {
104: processRequest(request, response);
105: }
106:
107: }
|