001    /**
002     * Copyright (c) 2000-2012 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.poller.comet;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
021    import com.liferay.portal.kernel.notifications.ChannelListener;
022    import com.liferay.portal.kernel.notifications.UnknownChannelException;
023    import com.liferay.portal.kernel.poller.comet.BaseCometHandler;
024    import com.liferay.portal.kernel.poller.comet.CometHandler;
025    import com.liferay.portal.kernel.poller.comet.CometRequest;
026    import com.liferay.portal.kernel.poller.comet.CometSession;
027    import com.liferay.portal.poller.PollerRequestHandlerUtil;
028    
029    /**
030     * @author Edward Han
031     * @author Brian Wing Shun Chan
032     */
033    public class PollerCometHandler extends BaseCometHandler {
034    
035            @Override
036            public CometHandler clone() {
037                    return new PollerCometHandler();
038            }
039    
040            public void receiveData(String data) {
041            }
042    
043            @Override
044            protected void doDestroy() throws Exception {
045                    if (_channelListener != null) {
046                            try {
047                                    ChannelHubManagerUtil.unregisterChannelListener(
048                                            _companyId, _userId, _channelListener);
049                            }
050                            catch (UnknownChannelException uce) {
051                            }
052                    }
053            }
054    
055            @Override
056            protected void doInit(CometSession cometSession) throws Exception {
057                    CometRequest cometRequest = cometSession.getCometRequest();
058    
059                    _companyId = cometRequest.getCompanyId();
060                    _userId = cometRequest.getUserId();
061    
062                    String pollerRequestString = cometRequest.getParameter("pollerRequest");
063    
064                    JSONObject pollerResponseHeaderJSONObject =
065                            PollerRequestHandlerUtil.processRequest(
066                                    cometRequest.getPathInfo(), pollerRequestString);
067    
068                    if (pollerResponseHeaderJSONObject != null) {
069                             _channelListener = new PollerCometChannelListener(
070                                     cometSession, pollerResponseHeaderJSONObject);
071    
072                            try {
073                                    ChannelHubManagerUtil.registerChannelListener(
074                                            _companyId, _userId, _channelListener);
075                            }
076                            catch (UnknownChannelException uce) {
077                                    if (_log.isDebugEnabled()) {
078                                            _log.debug(
079                                                    "Terminating request for " + _userId +
080                                                            " because user session ended");
081                                    }
082    
083                                    cometSession.close();
084                            }
085                    }
086                    else {
087                            cometSession.close();
088                    }
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(PollerCometHandler.class);
092    
093            private ChannelListener _channelListener;
094            private long _companyId;
095            private long _userId;
096    
097    }