| 1 |
|
package org.trails.security; |
| 2 |
|
|
| 3 |
|
import java.io.IOException; |
| 4 |
|
import java.util.HashMap; |
| 5 |
|
import java.util.List; |
| 6 |
|
|
| 7 |
|
import javax.servlet.http.Cookie; |
| 8 |
|
import javax.servlet.http.HttpServletResponse; |
| 9 |
|
|
| 10 |
|
import org.apache.log4j.Logger; |
| 11 |
|
import org.apache.tapestry.IRequestCycle; |
| 12 |
|
import org.apache.tapestry.engine.IEngineService; |
| 13 |
|
import org.apache.tapestry.engine.ILink; |
| 14 |
|
import org.apache.tapestry.services.LinkFactory; |
| 15 |
|
import org.hibernate.criterion.DetachedCriteria; |
| 16 |
|
import org.hibernate.criterion.Restrictions; |
| 17 |
|
import org.trails.persistence.HibernatePersistenceService; |
| 18 |
|
|
| 19 |
0 |
public class LogoutService implements IEngineService { |
| 20 |
0 |
private static final Logger log = Logger.getLogger(LogoutService.class); |
| 21 |
|
|
| 22 |
|
private HibernatePersistenceService persistenceService; |
| 23 |
|
private IEngineService restartService; |
| 24 |
|
private HttpServletResponse response; |
| 25 |
|
|
| 26 |
|
private LinkFactory linkFactory; |
| 27 |
|
|
| 28 |
|
public ILink getLink(boolean post, Object parameter) { |
| 29 |
0 |
return linkFactory.constructLink(this, post, new HashMap(), false); |
| 30 |
|
} |
| 31 |
|
|
| 32 |
|
public void service(IRequestCycle cycle) throws IOException { |
| 33 |
0 |
String username = cycle.getInfrastructure().getRequest().getRemoteUser(); |
| 34 |
0 |
if (username != null) |
| 35 |
|
{ |
| 36 |
0 |
Cookie cookie = new Cookie("remembermetoken", ""); |
| 37 |
0 |
cookie.setPath("/"); |
| 38 |
0 |
cookie.setMaxAge(0); |
| 39 |
0 |
response.addCookie(cookie); |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
try |
| 44 |
|
{ |
| 45 |
0 |
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ExpiringKey.class); |
| 46 |
0 |
detachedCriteria.add(Restrictions.eq("name", username)); |
| 47 |
0 |
List<ExpiringKey> credentials = persistenceService.getInstances(ExpiringKey.class, detachedCriteria); |
| 48 |
0 |
if (credentials.size() > 0) persistenceService.removeAll(credentials); |
| 49 |
|
} |
| 50 |
0 |
catch (Exception e) |
| 51 |
|
{ |
| 52 |
0 |
log.warn("Couldn't clean up persistent credentials because of: " + e.getMessage()); |
| 53 |
0 |
} |
| 54 |
|
} |
| 55 |
|
|
| 56 |
0 |
restartService.service(cycle); |
| 57 |
0 |
} |
| 58 |
|
|
| 59 |
|
public void setLinkFactory(LinkFactory factory) { |
| 60 |
0 |
linkFactory = factory; |
| 61 |
0 |
} |
| 62 |
|
|
| 63 |
|
public void setPersistenceService(HibernatePersistenceService persistenceService) { |
| 64 |
0 |
this.persistenceService = persistenceService; |
| 65 |
0 |
} |
| 66 |
|
|
| 67 |
|
public void setRestartService(IEngineService restartService) { |
| 68 |
0 |
this.restartService = restartService; |
| 69 |
0 |
} |
| 70 |
|
|
| 71 |
|
public String getName() { |
| 72 |
0 |
return "logout"; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
public void setResponse(HttpServletResponse response) { |
| 76 |
0 |
this.response = response; |
| 77 |
0 |
} |
| 78 |
|
} |