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.InitialContext;
028: import javax.naming.Context;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.jboss.test.web.interfaces.RunAsTargetLocalHome;
035: import org.jboss.test.web.interfaces.RunAsTargetLocal;
036: import org.jboss.logging.Logger;
037:
038: /** A servlet deployed under an unrestricted path that invokes the checkRunAs
039: * method on a secured RunAsTargetLocal EJB.
040: *
041: * @author Scott.Stark@jboss.org
042: * @version $Revision: 57211 $
043: */
044: public class UnsecureRunAsServlet extends HttpServlet {
045: Logger log = Logger.getLogger(UnsecureRunAsServlet.class);
046:
047: /**
048: * Test that init sees the run-as role
049: * @throws ServletException
050: */
051: public void init() throws ServletException {
052: String ejbName = super .getInitParameter("ejbName");
053: try {
054: InitialContext ctx = new InitialContext();
055: RunAsTargetLocalHome home = null;
056: Context enc = (Context) ctx.lookup("java:comp/env");
057: home = (RunAsTargetLocalHome) enc.lookup(ejbName);
058: RunAsTargetLocal bean = home.create();
059: bean.checkRunAs();
060: } catch (Exception e) {
061: throw new ServletException(
062: "Access to failed to method: checkRunAs", e);
063: }
064: }
065:
066: protected void processRequest(HttpServletRequest request,
067: HttpServletResponse response) throws ServletException,
068: IOException {
069: String ejbName = request.getParameter("ejbName");
070: try {
071: InitialContext ctx = new InitialContext();
072: RunAsTargetLocalHome home = null;
073: Context enc = (Context) ctx.lookup("java:comp/env");
074: home = (RunAsTargetLocalHome) enc.lookup(ejbName);
075: RunAsTargetLocal bean = home.create();
076: bean.checkRunAs();
077: } catch (Exception e) {
078: log.error("Access to checkRunAs failed", e);
079: throw new ServletException("Access to checkRunAs failed", e);
080: }
081:
082: Principal user = request.getUserPrincipal();
083: PrintWriter out = response.getWriter();
084: response.setContentType("text/html");
085: out.println("<html>");
086: out
087: .println("<head><title>UnsecureRunAsServlet</title></head><body>");
088: out.println("<h1>UnsecureRunAsServlet Accessed</h1>");
089: out.println("<pre>You have accessed this servlet as user: "
090: + user + "<br>");
091: out.println("</pre>");
092: out.println("</pre></body></html>");
093: out.close();
094: }
095:
096: protected void doGet(HttpServletRequest request,
097: HttpServletResponse response) throws ServletException,
098: IOException {
099: processRequest(request, response);
100: }
101:
102: protected void doPost(HttpServletRequest request,
103: HttpServletResponse response) throws ServletException,
104: IOException {
105: processRequest(request, response);
106: }
107:
108: }
|