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;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
022    import com.liferay.portal.kernel.poller.PollerHeader;
023    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
024    import com.liferay.portal.kernel.util.ContentTypes;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.io.IOException;
031    
032    import javax.servlet.ServletException;
033    import javax.servlet.http.HttpServlet;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class PollerServlet extends HttpServlet {
041    
042            @Override
043            public void service(
044                            HttpServletRequest request, HttpServletResponse response)
045                    throws IOException, ServletException {
046    
047                    try {
048                            String content = getContent(request);
049    
050                            if (content == null) {
051                                    PortalUtil.sendError(
052                                            HttpServletResponse.SC_NOT_FOUND,
053                                            new NoSuchLayoutException(), request, response);
054                            }
055                            else {
056                                    response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);
057    
058                                    ServletResponseUtil.write(
059                                            response, content.getBytes(StringPool.UTF8));
060                            }
061                    }
062                    catch (Exception e) {
063                            _log.error(e.getMessage());
064    
065                            PortalUtil.sendError(
066                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
067                                    response);
068                    }
069            }
070    
071            protected String getContent(HttpServletRequest request) throws Exception {
072                    long companyId = PortalUtil.getCompanyId(request);
073                    long userId = PortalUtil.getUserId(request);
074    
075                    if (userId == 0) {
076                            return StringPool.BLANK;
077                    }
078    
079                    String pollerRequestString = ParamUtil.getString(
080                            request, "pollerRequest");
081    
082                    PollerHeader pollerHeader = PollerRequestHandlerUtil.getPollerHeader(
083                            pollerRequestString);
084    
085                    if (pollerHeader == null) {
086                            return StringPool.BLANK;
087                    }
088    
089                    if (userId != pollerHeader.getUserId()) {
090                            return StringPool.BLANK;
091                    }
092    
093                    SynchronousPollerChannelListener synchronousPollerChannelListener =
094                            new SynchronousPollerChannelListener();
095    
096                    ChannelHubManagerUtil.getChannel(companyId, userId, true);
097    
098                    ChannelHubManagerUtil.registerChannelListener(
099                            companyId, userId, synchronousPollerChannelListener);
100    
101                    try {
102                            JSONObject pollerResponseHeaderJSONObject =
103                                    PollerRequestHandlerUtil.processRequest(
104                                            request, pollerRequestString);
105    
106                            if (pollerResponseHeaderJSONObject == null) {
107                                    return StringPool.BLANK;
108                            }
109    
110                            return synchronousPollerChannelListener.getNotificationEvents(
111                                    companyId, userId, pollerResponseHeaderJSONObject,
112                                    PropsValues.POLLER_REQUEST_TIMEOUT);
113                    }
114                    finally {
115                            ChannelHubManagerUtil.unregisterChannelListener(
116                                    companyId, userId, synchronousPollerChannelListener);
117                    }
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(PollerServlet.class);
121    
122    }