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