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.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            @Override
041            public void receiveData(String data) {
042            }
043    
044            @Override
045            protected void doDestroy() throws Exception {
046                    if (_channelListener != null) {
047                            try {
048                                    ChannelHubManagerUtil.unregisterChannelListener(
049                                            _companyId, _userId, _channelListener);
050                            }
051                            catch (UnknownChannelException uce) {
052                            }
053                    }
054            }
055    
056            @Override
057            protected void doInit(CometSession cometSession) throws Exception {
058                    CometRequest cometRequest = cometSession.getCometRequest();
059    
060                    _companyId = cometRequest.getCompanyId();
061                    _userId = cometRequest.getUserId();
062    
063                    String pollerRequestString = cometRequest.getParameter("pollerRequest");
064    
065                    JSONObject pollerResponseHeaderJSONObject =
066                            PollerRequestHandlerUtil.processRequest(
067                                    cometRequest.getRequest(), pollerRequestString);
068    
069                    if (pollerResponseHeaderJSONObject != null) {
070                            _channelListener = new PollerCometChannelListener(
071                                    cometSession, pollerResponseHeaderJSONObject);
072    
073                            try {
074                                    ChannelHubManagerUtil.registerChannelListener(
075                                            _companyId, _userId, _channelListener);
076                            }
077                            catch (UnknownChannelException uce) {
078                                    if (_log.isDebugEnabled()) {
079                                            _log.debug(
080                                                    "Terminating request for " + _userId +
081                                                            " because user session ended");
082                                    }
083    
084                                    cometSession.close();
085                            }
086                    }
087                    else {
088                            cometSession.close();
089                    }
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(PollerCometHandler.class);
093    
094            private ChannelListener _channelListener;
095            private long _companyId;
096            private long _userId;
097    
098    }