001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }