Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.extensions.registration.DynamicItem; 20 : import com.google.gerrit.server.AuditEvent; 21 : import com.google.gerrit.server.CurrentUser; 22 : import com.google.gerrit.server.config.AuthConfig; 23 : import com.google.gerrit.server.config.CanonicalWebUrl; 24 : import com.google.gerrit.server.group.GroupAuditService; 25 : import com.google.gerrit.server.util.time.TimeUtil; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Provider; 28 : import com.google.inject.Singleton; 29 : import java.io.IOException; 30 : import javax.servlet.http.HttpServlet; 31 : import javax.servlet.http.HttpServletRequest; 32 : import javax.servlet.http.HttpServletResponse; 33 : 34 : @Singleton 35 : public class HttpLogoutServlet extends HttpServlet { 36 : private static final long serialVersionUID = 1L; 37 : 38 : private final DynamicItem<WebSession> webSession; 39 : private final Provider<String> urlProvider; 40 : private final String logoutUrl; 41 : private final GroupAuditService audit; 42 : 43 : @Inject 44 : protected HttpLogoutServlet( 45 : AuthConfig authConfig, 46 : DynamicItem<WebSession> webSession, 47 : @CanonicalWebUrl @Nullable Provider<String> urlProvider, 48 99 : GroupAuditService audit) { 49 99 : this.webSession = webSession; 50 99 : this.urlProvider = urlProvider; 51 99 : this.logoutUrl = authConfig.getLogoutURL(); 52 99 : this.audit = audit; 53 99 : } 54 : 55 : protected void doLogout(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 56 1 : webSession.get().logout(); 57 1 : if (logoutUrl != null) { 58 1 : rsp.sendRedirect(logoutUrl); 59 : } else { 60 1 : String url = urlProvider.get(); 61 1 : if (Strings.isNullOrEmpty(url)) { 62 0 : url = req.getContextPath(); 63 : } 64 1 : if (Strings.isNullOrEmpty(url)) { 65 0 : url = "/"; 66 : } 67 1 : if (!url.endsWith("/")) { 68 0 : url += "/"; 69 : } 70 1 : rsp.sendRedirect(url); 71 : } 72 1 : } 73 : 74 : @Override 75 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 76 1 : String sid = webSession.get().getSessionId(); 77 1 : CurrentUser currentUser = webSession.get().getUser(); 78 1 : String what = "sign out"; 79 1 : long when = TimeUtil.nowMs(); 80 : 81 : try { 82 1 : doLogout(req, rsp); 83 : } finally { 84 1 : audit.dispatch(new AuditEvent(sid, currentUser, what, when, null, null)); 85 : } 86 1 : } 87 : }