KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > SecureEJBServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.security.Principal JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.servlet.ServletConfig JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServlet JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.jboss.test.web.interfaces.StatelessSession;
37 import org.jboss.test.web.interfaces.StatelessSessionHome;
38
39 /**
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 37406 $
43  */

44 public class SecureEJBServlet extends HttpServlet JavaDoc
45 {
46     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
47         throws ServletException JavaDoc, IOException JavaDoc
48     {
49         String JavaDoc echoMsg = null;
50         boolean testPropagation = false;
51         boolean includeHead = true;
52         String JavaDoc param = request.getParameter("testPropagation");
53         if( param != null )
54             testPropagation = Boolean.valueOf(param).booleanValue();
55         param = request.getParameter("includeHead");
56         if( param != null )
57             includeHead = Boolean.valueOf(param).booleanValue();
58
59         try
60         {
61             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
62             StatelessSessionHome home = null;
63             if( testPropagation == true )
64             {
65                 home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/UnsecuredEJB");
66                 StatelessSession bean = home.create();
67                 echoMsg = bean.forward("SecureEJBServlet called UnsecuredEJB.forward");
68             }
69             else
70             {
71                 home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/SecuredEJB");
72                 StatelessSession bean = home.create();
73                 echoMsg = bean.echo("SecureEJBServlet called SecuredEJB.echo");
74             }
75         }
76         catch(Exception JavaDoc e)
77         {
78             throw new ServletException JavaDoc("Failed to call SecuredEJB.echo", e);
79         }
80         Principal JavaDoc user = request.getUserPrincipal();
81         PrintWriter JavaDoc out = response.getWriter();
82         if( includeHead == true )
83         {
84            response.setContentType("text/html");
85            out.println("<html>");
86            out.println("<head><title>ENCServlet</title></head><body>");
87         }
88         out.println("<h1>SecureServlet Accessed</h1>");
89         out.println("<pre>You have accessed this servlet as user: "+user);
90         out.println("You have accessed SecuredEJB as user: "+echoMsg);
91         out.println("</pre>");
92         if( includeHead == true )
93            out.println("</pre></body></html>");
94         out.close();
95     }
96
97     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
98         throws ServletException JavaDoc, IOException JavaDoc
99     {
100         processRequest(request, response);
101     }
102
103     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
104         throws ServletException JavaDoc, IOException JavaDoc
105     {
106         processRequest(request, response);
107     }
108
109 }
110
Popular Tags