KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > ex1 > LocalUtilities


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: LocalUtilities.java,v 1.14 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.examples.ex1;
21
22 import java.io.*;
23 import java.util.*;
24 import java.net.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.apache.log4j.*;
29
30 import org.enhydra.barracuda.core.event.*;
31 import org.enhydra.barracuda.plankton.data.*;
32 import org.enhydra.barracuda.plankton.http.*;
33 import org.enhydra.barracuda.examples.ex1.events.*;
34
35
36 /**
37  * General utility event handlers which apply to all screens
38  * in the app. This really provides "master" controller functionality
39  * which gets applied to all events getting dispatched into the system
40  * through the Http gateway.
41  */

42 public class LocalUtilities extends DefaultEventGateway {
43
44     //public constants
45
protected static final Logger logger = Logger.getLogger(LocalUtilities.class.getName());
46
47     //this defines the various event handlers
48
private ListenerFactory localRequestFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new LocalRequestHandler();} public String JavaDoc getListenerID() {return getID(LocalRequestHandler.class);}};
49
50     //private vars
51
private LocalRequestEvent lnkLocalRequestEvent; //added by TJ to show association
52

53     /**
54      * Public constructor
55      */

56     public LocalUtilities() {
57         //specify who's interested in what
58
specifyLocalEventInterests(localRequestFactory, LocalRequestEvent.class);
59     }
60     
61     //------------------------------------------------------------
62
// Model 2 - Controller Event Handlers
63
//------------------------------------------------------------
64
/**
65      * HttpRequest - Here we redirect to the LoginScreen if necessary.
66      * Make sure they aren't trying to dispatch a bogus event, and make sure
67      * they have user/pwd info in their session.
68      *
69      * NOTE: if we were aiming for reusability, we could actually implement
70      * this as multiple listeners in standalone classes, allowing a developer
71      * to pick and choose as needed...for now, we just kept it simple and lumped
72      * it all into one handler.
73      */

74     class LocalRequestHandler extends DefaultBaseEventListener {
75         public void handleControlEvent(ControlEventContext context) throws EventException, ServletException, IOException {
76             //unpack the necessary entities from the context
77
HttpServletRequest req = context.getRequest();
78             DispatchQueue queue = context.getQueue();
79
80             //figure out which event triggered this request
81
logger.debug("Locating the event which triggered this one");
82             List procList = queue.listProcessedEvents();
83             BaseEvent triggerEvent = (BaseEvent) procList.get(procList.size()-1);
84             logger.debug("Trigger event:"+triggerEvent);
85
86             //see if we can validate user/pwd info
87
HttpSession session = req.getSession();
88             int valid = LoginServices.validateUserPwd(session);
89             logger.debug("Checking for valid user/pwd info in the session...result:"+LoginServices.xrefValidity(valid));
90
91             //if the event which triggered this is NOT an instance of
92
//LoginScreen event (meaning they're trying to do something
93
//OTHER than log in) and they don't have valid credentials,
94
//redirect them to the LoginScreen
95
if (valid!=LoginServices.VALID && (triggerEvent==null || (!(triggerEvent instanceof LoginScreenEvent)))) {
96                 logger.debug("Redirecting to the LoginScreen because they lack valid credentials");
97                 throw new ClientSideRedirectException(new GetLoginScreen());
98             }
99             
100             //if the event which triggered this IS a LoginScreen event
101
//(meaning they're trying to log in again) and they already
102
//have valid login information, just redirect them onto the
103
//main screen
104
if (triggerEvent!=null && (triggerEvent instanceof LoginScreenEvent) && valid==LoginServices.VALID) {
105                 logger.debug("Redirecting to MainScreen since they already have valid credentials");
106                 
107 //csc_082302.2_start
108
//rather than store the value in the session, we can put it in the
109
//LocalRepository since that structure is persisted across the multiple
110
//req-resp cycles that happen when we throw a ClientSideRedirectException.
111
//If for some reason the lr and state information stop getting persisted
112
//(ie. if we inadvertantly introdcue a bug), then this auto-login
113
//stuff will stop working
114
// session.setAttribute(LoginServices.AUTO_LOGIN, ""+true);
115
ObjectRepository lr = ObjectRepository.getLocalRepository();
116                 lr.putState(LoginServices.AUTO_LOGIN, Boolean.TRUE);
117 //csc_082302.2_end
118
throw new ClientSideRedirectException(new GetMainScreen());
119             }
120         }
121     }
122 }
123
124
Popular Tags