1 package com.atlassian.seraph.logout;
2
3 import com.atlassian.seraph.auth.Authenticator;
4 import com.atlassian.seraph.auth.AuthenticatorException;
5 import com.atlassian.seraph.config.SecurityConfig;
6 import com.atlassian.seraph.config.SecurityConfigFactory;
7 import com.atlassian.seraph.config.SecurityConfigImpl;
8
9 import java.io.IOException;
10
11 import javax.servlet.ServletConfig;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16
17
18
19
20
21
22
23
24
25
26 public class LogoutServlet extends HttpServlet
27 {
28 private SecurityConfig securityConfig;
29
30 public void init() throws ServletException
31 {
32 super.init();
33 securityConfig = SecurityConfigFactory.getInstance();
34 }
35
36 public void init(final ServletConfig servletConfig) throws ServletException
37 {
38 super.init(servletConfig);
39 securityConfig = (SecurityConfig) servletConfig.getServletContext().getAttribute(SecurityConfigImpl.STORAGE_KEY);
40 }
41
42 protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
43 {
44 if (isRelativeRedirect())
45 {
46
47 response.sendRedirect(request.getContextPath() + getSecurityConfig().getLogoutURL());
48 }
49 else
50 {
51
52 try
53 {
54 final Authenticator authenticator = getAuthenticator();
55 authenticator.logout(request, response);
56 }
57 catch (final AuthenticatorException e)
58 {
59 throw new ServletException("Seraph authenticator couldn't log out", e);
60 }
61 response.sendRedirect(getSecurityConfig().getLogoutURL());
62 }
63 }
64
65 private boolean isRelativeRedirect()
66 {
67 return getSecurityConfig().getLogoutURL().indexOf("://") == -1;
68 }
69
70 protected SecurityConfig getSecurityConfig()
71 {
72 return securityConfig;
73 }
74
75 protected Authenticator getAuthenticator()
76 {
77 return getSecurityConfig().getAuthenticator();
78 }
79 }