1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 23 package org.apache.struts.webapp.example; 24 25 26 import jakarta.servlet.http.HttpServletRequest; 27 import jakarta.servlet.http.HttpServletResponse; 28 import jakarta.servlet.http.HttpSession; 29 30 import org.apache.struts.action.Action; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.apps.mailreader.dao.User; 35 import org.slf4j.Logger; 36 import org.slf4j.LoggerFactory; 37 38 39 /** 40 * Implementation of <strong>Action</strong> that processes a 41 * user logoff. 42 * 43 * @author Craig R. McClanahan 44 * @version $Rev$ $Date$ 45 */ 46 47 public final class LogoffAction extends Action { 48 private static final long serialVersionUID = 7989466402657480736L; 49 50 51 // ----------------------------------------------------- Instance Variables 52 53 54 /** 55 * The {@code Log} instance for this class. 56 */ 57 private final static Logger LOG = 58 LoggerFactory.getLogger(LogoffAction.class); 59 60 61 // --------------------------------------------------------- Public Methods 62 63 64 /** 65 * Process the specified HTTP request, and create the corresponding HTTP 66 * response (or forward to another web component that will create it). 67 * Return an <code>ActionForward</code> instance describing where and how 68 * control should be forwarded, or <code>null</code> if the response has 69 * already been completed. 70 * 71 * @param mapping The ActionMapping used to select this instance 72 * @param form The optional ActionForm bean for this request (if any) 73 * @param request The HTTP request we are processing 74 * @param response The HTTP response we are creating 75 * 76 * @exception Exception if business logic throws an exception 77 */ 78 public ActionForward execute(ActionMapping mapping, 79 ActionForm form, 80 HttpServletRequest request, 81 HttpServletResponse response) 82 throws Exception { 83 84 // Extract attributes we will need 85 HttpSession session = request.getSession(); 86 User user = (User) session.getAttribute(Constants.USER_KEY); 87 88 // Process this user logoff 89 if (user != null) { 90 LOG.debug("LogoffAction: User '{}' logged off in session {}", 91 user.getUsername(), session.getId()); 92 } else { 93 LOG.debug("LogoffActon: User logged off in session {}", 94 session.getId()); 95 } 96 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 97 session.removeAttribute(Constants.USER_KEY); 98 session.invalidate(); 99 100 // Forward control to the specified success URI 101 return (mapping.findForward("success")); 102 103 } 104 }