KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > Login


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * Login.java
67  *
68  * 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.ext.taglib;
71
72 import com.jcorporate.expresso.core.controller.ControllerException;
73 import com.jcorporate.expresso.core.controller.NonHandleableException;
74 import com.jcorporate.expresso.core.jsdkapi.GenericDispatcher;
75 import com.jcorporate.expresso.core.misc.ConfigManager;
76 import com.jcorporate.expresso.core.misc.CurrentLogin;
77 import com.jcorporate.expresso.core.misc.StringUtil;
78 import com.jcorporate.expresso.core.security.User;
79 import com.jcorporate.expresso.core.servlet.CheckLogin;
80 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
81 import org.apache.log4j.Logger;
82 import org.apache.struts.config.ActionConfig;
83
84 import javax.servlet.ServletException JavaDoc;
85 import javax.servlet.http.HttpServletRequest JavaDoc;
86 import javax.servlet.http.HttpServletResponse JavaDoc;
87 import javax.servlet.jsp.JspTagException JavaDoc;
88 import java.io.IOException JavaDoc;
89
90
91 /**
92  * Tag to transition between database contexts, either logging the
93  * user into a new context (if possible with cookies, etc),
94  * or taking them to the login screen if it is not possible.
95  *
96  * @author Michael Nash
97  */

98 public class Login
99         extends ExpressoTagSupport {
100     private String JavaDoc db = null;
101     private String JavaDoc forward = null;
102     private static final String JavaDoc defaultLoginUrl = "/Login.do";
103     private static final Logger log = Logger.getLogger(Login.class);
104
105     public Login() {
106         super();
107     }
108
109     public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
110         return EVAL_BODY_INCLUDE;
111     }
112
113     public int doEndTag()
114             throws JspTagException JavaDoc {
115         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
116         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
117
118         //
119
//First perform the checklogin to get everything set proper.
120
//
121
try {
122             CheckLogin chk = CheckLogin.getInstance();
123             chk.checkLogin(request);
124
125             CurrentLogin myLogin = (CurrentLogin) pageContext.getSession().getAttribute(CurrentLogin.LOGIN_KEY);
126
127             if (myLogin == null) {
128                 throw new JspTagException JavaDoc("Unable to estabish session");
129             }
130
131             String JavaDoc userName = myLogin.getUserName();
132             String JavaDoc currentDB = myLogin.getDBName();
133
134             if ((userName.equals("") || userName.equals(User.UNKNOWN_USER))) {
135                 if (log.isDebugEnabled()) {
136                     log.debug("Sending to login");
137                 }
138                 /* send to the login page */
139                 return sendToLogin(request, response, chk);
140             }
141             /* Are we told to force to a specific database? */
142             if (db != null) {
143                 if (!currentDB.equals(db)) {
144                     if (!chk.loginViaCookie(request, db)) {
145                         if (log.isDebugEnabled()) {
146                             log.debug("Failed to log in via cookie, redirecting to login");
147                         }
148                         return sendToLogin(request, response, chk);
149                     }
150                 } /* if not in requested database already */
151
152             } /* if db specified */
153
154
155             if (log.isDebugEnabled()) {
156                 log.debug("getting current login");
157             }
158             myLogin = (CurrentLogin) pageContext.getSession().getAttribute(CurrentLogin.LOGIN_KEY);
159
160             if (myLogin == null) {
161                 return sendToLogin(request, response, chk);
162             }
163
164             if (myLogin.getUserName().equals(User.UNKNOWN_USER)) {
165                 return sendToLogin(request, response, chk);
166             }
167
168             return EVAL_PAGE;
169         } catch (IOException JavaDoc e) {
170             log.error(e);
171             throw new JspTagException JavaDoc(e.getMessage());
172         } catch (ServletException JavaDoc e) {
173             log.error(e);
174             throw new JspTagException JavaDoc(e.getMessage());
175         } catch (NonHandleableException e) {
176             log.error(e);
177             throw new JspTagException JavaDoc(e.getMessage());
178         } catch (Exception JavaDoc e) {
179             log.error(e);
180             throw new JspTagException JavaDoc(e.getMessage());
181         }
182     }
183
184     /**
185      * Private method that sends the user to the login page.
186      *
187      * @param request the HttpServletRequest object
188      * @param response the HttpServletResponse object
189      * @param chk the checklogin object
190      * @return JSP tag value
191      */

192     private int sendToLogin(HttpServletRequest JavaDoc request,
193                             HttpServletResponse JavaDoc response, CheckLogin chk)
194             throws JspTagException JavaDoc {
195         if (forward != null) {
196             if (forward.equals("false")) {
197                 try {
198                     chk.logInAsNone(request, db);
199                 } catch (ServletException JavaDoc se) {
200                     log.error(se);
201                     throw new JspTagException JavaDoc("Could not log in as 'NONE'");
202                 }
203
204                 return EVAL_PAGE;
205             }
206         }
207
208         if (log.isDebugEnabled()) {
209             log.debug("User was not logged in - sending to login page");
210         }
211
212         try {
213             FastStringBuffer fsb = new FastStringBuffer(128);
214             ActionConfig am = ConfigManager.getActionConfig(com.jcorporate.expresso.services.controller
215                     .LoginController.getLoginController().getClass().getName(), null);
216
217             if (am != null) {
218                 fsb.append(StringUtil.notNull(am.getPath()));
219                 fsb.append(".do");
220             } else {
221                 fsb.append(defaultLoginUrl);
222             }
223
224             if (db != null) {
225                 fsb.append("?dbContext=");
226                 fsb.append(db);
227             } else {
228                 fsb.append("&state=promptLogin");
229             }
230             String JavaDoc loginUrl = fsb.toString();
231
232             if (log.isDebugEnabled()) {
233                 log.debug("Forwarding to '" + loginUrl + "'");
234             }
235
236             GenericDispatcher.forward(request, response, loginUrl);
237
238             return SKIP_PAGE;
239         } catch (ServletException JavaDoc se) {
240             log.error(se);
241             throw new JspTagException JavaDoc(se.getMessage());
242         } catch (IOException JavaDoc se) {
243             log.error(se);
244             throw new JspTagException JavaDoc(se.getMessage());
245         } catch (NullPointerException JavaDoc np) {
246             log.error(np);
247             throw new JspTagException JavaDoc(np.getMessage());
248         } catch (ControllerException ce) {
249             log.error("Unable to find appropriate login Controller", ce);
250             throw new JspTagException JavaDoc(ce.getMessage());
251         }
252     }
253
254     public String JavaDoc getDb() {
255         return db;
256     }
257
258     public void setDb(String JavaDoc newDb) {
259         db = newDb;
260     }
261
262     public String JavaDoc getForward() {
263         return forward;
264     }
265
266     public void setForward(String JavaDoc newForward) {
267         forward = newForward;
268     }
269 } /* Login */
270
Popular Tags