KickJava   Java API By Example, From Geeks To Geeks.

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


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.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServlet JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.security.auth.login.LoginContext JavaDoc;
35 import javax.security.auth.login.LoginException JavaDoc;
36
37 import org.jboss.security.auth.callback.UsernamePasswordHandler;
38 import org.jboss.test.web.interfaces.StatelessSession;
39 import org.jboss.test.web.interfaces.StatelessSessionHome;
40
41 /** A servlet that performs a JAAS login to access a secure EJB.
42
43 @author Scott.Stark@jboss.org
44 @version $Revision: 37406 $
45 */

46 public class ClientLoginServlet extends HttpServlet JavaDoc
47 {
48     protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
49         throws ServletException JavaDoc, IOException JavaDoc
50     {
51         LoginContext JavaDoc lc = null;
52         String JavaDoc echoMsg = null;
53         try
54         {
55             lc = doLogin("jduke", "theduke");
56             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
57             StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/SecuredEJB");
58             StatelessSession bean = home.create();
59             echoMsg = bean.echo("ClientLoginServlet called SecuredEJB.echo");
60         }
61         catch(LoginException JavaDoc e)
62         {
63             throw new ServletException JavaDoc("Failed to login to client-login domain as jduke", e);
64         }
65         catch(Exception JavaDoc e)
66         {
67             throw new ServletException JavaDoc("Failed to access SecuredEJB", e);
68         }
69         finally
70         {
71             if( lc != null )
72             {
73                 try
74                 {
75                     lc.logout();
76                 }
77                 catch(LoginException JavaDoc e)
78                 {
79                 }
80             }
81         }
82
83         response.setContentType("text/html");
84         PrintWriter JavaDoc out = response.getWriter();
85         out.println("<html>");
86         out.println("<head><title>ClientLoginServlet</title></head>");
87         out.println("<h1>ClientLoginServlet Accessed</h1>");
88         out.println("<body>Login as user=jduke succeeded.<br>SecuredEJB.echo returned:"+echoMsg+"</body>");
89         out.println("</html>");
90         out.close();
91     }
92
93     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
94         throws ServletException JavaDoc, IOException JavaDoc
95     {
96         processRequest(request, response);
97     }
98
99     protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
100         throws ServletException JavaDoc, IOException JavaDoc
101     {
102         processRequest(request, response);
103     }
104
105     private LoginContext JavaDoc doLogin(String JavaDoc username, String JavaDoc password) throws LoginException JavaDoc
106     {
107         UsernamePasswordHandler handler = new UsernamePasswordHandler(username, password.toCharArray());
108         LoginContext JavaDoc lc = new LoginContext JavaDoc("client-login", handler);
109         lc.login();
110         return lc;
111     }
112 }
113
Popular Tags