001    /**
002     * Copyright (c) 2000-2013 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            @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    }