KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > logout > Logout_Engine


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 29-JAN-2001, Xo3 SA : Eric Vassali
37  * 26-JUL-2003, Jahia Solutions Sarl: Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.engines.logout;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Locale JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import org.jahia.data.JahiaData;
50 import org.jahia.data.events.JahiaEvent;
51 import org.jahia.engines.JahiaEngine;
52 import org.jahia.engines.login.Login_Engine;
53 import org.jahia.engines.core.Core_Engine;
54 import org.jahia.exceptions.JahiaException;
55 import org.jahia.params.ParamBean;
56 import org.jahia.registries.EnginesRegistry;
57 import org.jahia.registries.ServicesRegistry;
58 import org.jahia.services.pages.ContentPage;
59 import org.jahia.services.pages.JahiaPage;
60 import org.jahia.services.pages.JahiaPageBaseService;
61 import org.jahia.services.sites.JahiaSite;
62 import org.jahia.services.usermanager.JahiaGroup;
63 import org.jahia.services.usermanager.JahiaGroupManagerService;
64 import org.jahia.services.usermanager.JahiaUser;
65 import org.jahia.services.cache.HtmlCache;
66 import org.jahia.services.cache.CacheFactory;
67 import java.util.Properties JavaDoc;
68 import org.jahia.settings.SettingsBean;
69 import org.jahia.bin.Jahia;
70 import javax.servlet.http.Cookie JavaDoc;
71
72
73 /**
74  * @author Eric Vassalli
75  * @author Khue Nguyen
76  * @author Fulco Houkes
77  * @todo add some nice javadoc comment here
78  * @todo (Fulco) : the toolBox attribute is initializedin the constructor, but never used.
79  */

80 public class Logout_Engine implements JahiaEngine {
81
82     /** unique instance of this class */
83     private static Logout_Engine instance = null;
84
85     /** Engine's name. */
86     public static final String JavaDoc ENGINE_NAME = "logout";
87
88     /** logging */
89     private static final org.apache.log4j.Logger logger =
90             org.apache.log4j.Logger.getLogger (Logout_Engine.class);
91
92
93     /**
94      * Default constructor, creates a new <code>Logout_Engin</code> instance.
95      */

96     private Logout_Engine () {
97         logger.debug ("***** Starting " + Logout_Engine.class.getName () + " engine *****");
98     }
99
100
101     /**
102      * Returns the unique instance of this class
103      *
104      * @return the unique instance of this class
105      */

106     public static synchronized Logout_Engine getInstance () {
107
108         if (instance == null) {
109             instance = new Logout_Engine ();
110         }
111         return instance;
112     }
113
114
115     /**
116      * Return true if the engine is authorise to render the logout.
117      *
118      * @param jParams the parameter bean
119      *
120      * @return <code>true</code> when the render is authorised, otherwise <code>false</code>.
121      */

122     public boolean authoriseRender (ParamBean jParams) {
123         return true; // always allowed to render logout!
124
}
125
126
127     /**
128      * Renders link to pop-up window
129      *
130      * @param jParams
131      * @param theObj
132      *
133      * @return
134      *
135      * @throws JahiaException
136      */

137     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
138             throws JahiaException {
139         /**
140          * @todo This is very ugly... what are we doing here exactly ?
141          * Seems like we are replacing the page ID, but it usually is the
142          * same. What is the case where we generate a logout URL for a
143          * different page ?
144          */

145         Properties JavaDoc extraProps = new Properties JavaDoc();
146         if (theObj != null) {
147             Integer JavaDoc pageID = (Integer JavaDoc) theObj;
148             extraProps.setProperty("pid", pageID.toString());
149         }
150
151         String JavaDoc theUrl = jParams.composeEngineUrl (ENGINE_NAME, extraProps, "?engine_params=logout");
152
153         return jParams.getResponse ().encodeURL (theUrl);
154     }
155
156
157     /**
158      * Returns <code>true</code> if the engine needs a <code>JahiaData</code> instance.
159      *
160      * @param jParams the parameter bean instance
161      *
162      * @return <code>true</code> if the engine needs a <code>JahiaData</code>, otherwise
163      * <code>false</code>.
164      */

165     public boolean needsJahiaData (ParamBean jParams) {
166         return false; // we need the jData because we are forwarding the request
167
// to the "core" engine!
168
}
169
170
171     /**
172      * handles the engine actions
173      *
174      * @param jParams a ParamBean object
175      * @param jData a JahiaData object (not mandatory)
176      */

177     public void handleActions (ParamBean jParams, JahiaData jData)
178             throws JahiaException {
179         String JavaDoc engineParams = jParams.getRequest ().getParameter ("engine_params");
180
181         // change mode to normal
182
jParams.setOperationMode (ParamBean.NORMAL);
183
184         JahiaPage logoutPage = null;
185
186         // if there is no engineparams, it just means that the engine is still set to
187
// logout, but in fact the user wants to call "core". This is because a user
188
// logs outs, the page is reloaded but still with engine "logout".
189
if ((engineParams != null) && (engineParams.equals ("logout"))) {
190             logger.debug ("User [" + jParams.getUser ().getName () + "] logged out.");
191
192             // send a new logout event !
193
JahiaEvent theEvent = new JahiaEvent (this, jParams, jParams.getUser ());
194             ServicesRegistry.getInstance ().getJahiaEventService ().fireLogout (theEvent);
195
196             logoutPage = getLogoutPage (jData);
197
198             // now let's destroy the cookie authentification if there was one
199
// set for this user.
200
JahiaUser curUser = jParams.getUser();
201             SettingsBean settingsBean = Jahia.getSettings();
202             String JavaDoc cookieAuthKey = curUser.getProperty(settingsBean.getCookieAuthUserPropertyName());
203             Cookie JavaDoc authCookie = new Cookie JavaDoc(settingsBean.getCookieAuthCookieName(), cookieAuthKey);
204             authCookie.setPath(jParams.getRequest().getContextPath());
205             authCookie.setMaxAge(0); // means we want it deleted now !
206
jParams.getRealResponse().addCookie(authCookie);
207             curUser.removeProperty(settingsBean.getCookieAuthUserPropertyName());
208
209             // if we were in edit mode, let's destroy the cache's entry for this page and this user.
210
if (ParamBean.EDIT.equals (jParams.getOperationMode ())) {
211                 // Get the HTML cache instance
212
HtmlCache htmlCache = CacheFactory.getHtmlCache ();
213                 if (htmlCache != null) {
214                     // invalidate the user entries
215
htmlCache.invalidateUserEntries (jParams.getUser ().getUsername ());
216
217                 } else {
218                     logger.warn ("Could not get the HTML cache instance!!");
219                 }
220             }
221
222             ServicesRegistry.getInstance ().getJahiaApplicationsDispatchingService ()
223                     .flushAllSessionsCaches (jParams.getSession ());
224             // destroys all the session objects and creates a new one
225
// jParams.invalidateSession();
226
jParams.purgeSession ();
227             // set current user to guest
228
jParams.setUserGuest (jParams.getSiteID ());
229         }
230
231         if (logoutPage == null) {
232             // current page do not have read
233
throw new JahiaException ("403 Forbidden - Page:" + jParams.getPageID (),
234                     "No read access for page " + jParams.getPageID (),
235                     JahiaException.SECURITY_ERROR,
236                     JahiaException.ERROR_SEVERITY);
237         } else {
238
239             if (!logoutPage.hasEntry (ContentPage.ACTIVE_PAGE_INFOS,
240                     jParams.getEntryLoadRequest ()
241                     .getFirstLocale (true).toString ())) {
242                 ArrayList JavaDoc siteLanguages =
243                         jParams.getSite ().getLanguageSettingsAsLocales (true);
244                 boolean skip = false;
245                 for (int i = 0; i < siteLanguages.size (); i++) {
246                     Locale JavaDoc locale = (Locale JavaDoc) siteLanguages.get (i);
247                     if (!skip && logoutPage.hasEntry (ContentPage.ACTIVE_PAGE_INFOS,
248                             locale.toString ())) {
249                         jParams.changeLanguage (locale);
250                         skip = true;
251                     }
252                 }
253             }
254
255             // get the logout page with template
256
try {
257                 if (logoutPage.hasActiveEntries ()) {
258                     JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
259                     logoutPage = pageService.lookupPage (logoutPage.getID (), jParams);
260                 }
261             } catch (Throwable JavaDoc t) {
262                 t.printStackTrace ();
263             }
264
265             if (logoutPage == null) {
266                 throw new JahiaException ("404 Page not found ",
267                         "Logout page not found ",
268                         JahiaException.SECURITY_ERROR,
269                         JahiaException.ERROR_SEVERITY);
270             }
271
272             // change the page
273
ContentPage contentPage = ServicesRegistry.getInstance ()
274                     .getJahiaPageService ().lookupContentPage (logoutPage.getID (),
275                             jParams.getEntryLoadRequest (), true);
276             jParams.changePage (contentPage);
277
278         }
279         if (logoutPage.hasActiveEntries ()
280                 && logoutPage.checkGuestAccess (jParams.getSiteID ())) {
281             JahiaData jData2 = new JahiaData (jParams, true);
282             ((JahiaEngine) EnginesRegistry.getInstance ().getEngine (Core_Engine.ENGINE_NAME)).handleActions (
283                     jParams, jData2);
284
285         } else {
286             jParams.setUserGuest (jParams.getSiteID ());
287             JahiaData jData2 = new JahiaData (jParams, false);
288             ((JahiaEngine) EnginesRegistry.getInstance ().getEngine (Login_Engine.ENGINE_NAME)).handleActions (
289                     jParams, jData2);
290         }
291     }
292
293     /**
294      * Retrieve the engine name.
295      *
296      * @return the engine name.
297      */

298     public final String JavaDoc getName () {
299         return ENGINE_NAME;
300     }
301
302
303     // NK
304
/**
305      * Check for a "friendly" logout page instead of a "403 Forbidden " page, when the user
306      * loggout.
307      *
308      * @param jData a page, can be null if no page available.
309      */

310     private JahiaPage getLogoutPage (JahiaData jData) {
311
312         JahiaUser user = jData.params ().getUser ();
313         JahiaPage page = jData.params ().getPage ();
314         JahiaSite site = jData.params ().getSite ();
315
316         if (page == null || user == null || site == null)
317             return null;
318
319         if (page.hasActiveEntries () && page.checkGuestAccess (site.getID ()))
320         // he can stay at current page.
321
return page;
322
323         // look at the user homepages
324
try {
325
326             JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
327
328             // get user home page
329
if (user.getHomepageID () >= 0) {
330
331                 try {
332                     page = pageService.lookupPage (user.getHomepageID (),
333                             jData.params ().getEntryLoadRequest (), false);
334                     if ((page != null) && page.hasActiveEntries ()
335                             && page.checkGuestAccess (site.getID ()))
336                         return page; // return the user home page
337
} catch (Throwable JavaDoc t) {
338                     t.printStackTrace ();
339                 }
340             }
341
342             // get group homepages
343
JahiaGroupManagerService grpServ =
344                     ServicesRegistry.getInstance ().getJahiaGroupManagerService ();
345
346             Vector JavaDoc v = grpServ.getUserMembership (user);
347             int size = v.size ();
348             String JavaDoc grpKey;
349             JahiaGroup grp;
350             for (int i = 0; i < size; i++) {
351                 grpKey = (String JavaDoc) v.get (i);
352                 grp = grpServ.lookupGroup (grpKey);
353                 if (grp != null
354                         && grp.getSiteID () == site.getID ()
355                         && grp.getHomepageID () >= 0) {
356
357                     try {
358                         page = pageService.lookupPage (grp.getHomepageID (),
359                                 jData.params ().getEntryLoadRequest (), false);
360                         if ((page != null) && page.hasActiveEntries ()
361                                 && page.checkGuestAccess (site.getID ()))
362                             return page;// return the user first available group home page
363
// TODO -> have a default homepage per user !!! Pol is doing it
364

365                     } catch (Throwable JavaDoc t) {
366                         t.printStackTrace ();
367                     }
368                 }
369             }
370             // if none available check if user has guest access on the site's homepage
371
page = pageService.lookupPage (site.getHomePageID (),
372                     jData.params ().getEntryLoadRequest (), false);
373             if ((page != null) && page.hasActiveEntries ()
374                     && page.checkGuestAccess (site.getID ())) {
375                 return page;
376             } else if ((page != null) && !page.hasActiveEntries ()) {
377                 return page;
378             }
379         } catch (Throwable JavaDoc t) {
380             t.printStackTrace ();
381         }
382         return null; // no page available...
383
}
384 }
385
Popular Tags