001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.SessionAction;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.notifications.ChannelException;
021    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    
026    import javax.servlet.http.HttpSession;
027    
028    /**
029     * @author Edward Han
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class ChannelSessionDestroyAction extends SessionAction {
034    
035            @Override
036            public void run(HttpSession session) {
037                    User user = null;
038    
039                    try {
040                            user = (User)session.getAttribute(WebKeys.USER);
041                    }
042                    catch (IllegalStateException ise) {
043                            return;
044                    }
045    
046                    try {
047                            if (user == null) {
048                                    Long userId = (Long)session.getAttribute(WebKeys.USER_ID);
049    
050                                    if (userId != null) {
051                                            user = UserLocalServiceUtil.getUser(userId);
052                                    }
053                            }
054    
055                            if ((user == null) || user.isDefaultUser()) {
056                                    return;
057                            }
058    
059                            if (_log.isDebugEnabled()) {
060                                    _log.debug("Destroying channel " + user.getUserId());
061                            }
062    
063                            try {
064                                    ChannelHubManagerUtil.destroyChannel(
065                                            user.getCompanyId(), user.getUserId());
066                            }
067                            catch (ChannelException ce) {
068                                    if (_log.isDebugEnabled()) {
069                                            _log.debug(
070                                                    "User channel " + user.getUserId() +
071                                                            " is already unregistered",
072                                                    ce);
073                                    }
074                            }
075                    }
076                    catch (Exception e) {
077                            _log.error(e, e);
078                    }
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(
082                    ChannelSessionDestroyAction.class);
083    
084    }