View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.actions;
18  
19   
20  // Java Core Classes
21  
22  import javax.servlet.http.Cookie;
23  
24  // Turbine Modules
25  import org.apache.turbine.modules.ActionEvent;
26  import org.apache.turbine.util.RunData;
27  import org.apache.turbine.TurbineConstants;
28  
29  // Jetspeed Stuff
30  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31  import org.apache.jetspeed.services.logging.JetspeedLogger;
32  import org.apache.jetspeed.services.resources.JetspeedResources;
33  import org.apache.jetspeed.services.JetspeedSecurity;
34  import org.apache.jetspeed.util.template.JetspeedLink;
35  import org.apache.jetspeed.util.template.JetspeedLinkFactory;
36  import org.apache.jetspeed.om.security.JetspeedUser;
37  
38  
39  
40  /***
41      This class is responsible for logging a user out of the system.
42  */
43  public class JLogoutUser extends ActionEvent
44  {
45  
46      /***
47       * Static initialization of the logger for this class
48       */
49      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JLogoutUser.class.getName());
50      
51      public void doPerform( RunData data ) throws Exception
52      {    
53          logger.info("Entering action JLogoutUser");
54  
55          // if automatic login is enabled, then remove cookies when user logs out
56          if ( JetspeedResources.getBoolean("automatic.logon.enable", false) )
57          {
58            // remove cookies by re-adding them with zero MaxAge, which deletes them
59            Cookie userName = new Cookie("username","");
60            Cookie loginCookie = new Cookie("logincookie","");
61  
62            String comment = JetspeedResources.getString("automatic.logon.cookie.comment","");
63            String domain = JetspeedResources.getString("automatic.logon.cookie.domain");
64            String path = JetspeedResources.getString("automatic.logon.cookie.path","/");
65  
66            if (domain == null)
67            {
68              String server = data.getServerName();
69              domain = "." + server;
70            }
71  
72            userName.setMaxAge(0);
73            userName.setComment(comment);
74            userName.setDomain(domain);
75            userName.setPath(path);
76  
77            loginCookie.setMaxAge(0);
78            loginCookie.setComment(comment);
79            loginCookie.setDomain(domain);
80            loginCookie.setPath(path);
81  
82            data.getResponse().addCookie(userName);
83            data.getResponse().addCookie(loginCookie);
84  
85  
86            // also need to remove the cookies from the current request - otherwise the session validator will log user in again
87            if ( data.getRequest().getCookies() != null)
88            {
89              data.getCookies().remove("logincookie");
90              data.getCookies().remove("username");
91            }
92          }        
93  
94          // use the standard turbine logout facility
95          if ( JetspeedResources.getBoolean("automatic.logout.save", false) )
96          {
97              JetspeedSecurity.saveUser((JetspeedUser)data.getUserFromSession());
98          }
99  
100         JetspeedSecurity.logout();
101 
102         data.setMessage(JetspeedResources.getString(
103             TurbineConstants.LOGOUT_MESSAGE));
104 
105         JetspeedLink jsLink = null;
106 
107         data.setScreen(JetspeedResources.getString(
108             TurbineConstants.SCREEN_HOMEPAGE));
109 
110         try
111         {
112             jsLink = JetspeedLinkFactory.getInstance(data);
113         } catch (Exception e)
114         {
115             logger.error("Error getting jsLink", e);
116         }
117         data.setRedirectURI(jsLink.getHomePage().toString());
118         JetspeedLinkFactory.putInstance(jsLink);
119         jsLink = null;
120     }
121 
122 }