001    /**
002     * Copyright (c) 2000-2010 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.servlet;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.StringServletResponse;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.servlet.filters.gzip.GZipFilter;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.social.util.FacebookUtil;
027    import com.liferay.util.servlet.ServletResponseUtil;
028    
029    import java.io.IOException;
030    
031    import javax.servlet.RequestDispatcher;
032    import javax.servlet.ServletContext;
033    import javax.servlet.ServletException;
034    import javax.servlet.http.HttpServlet;
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class FacebookServlet extends HttpServlet {
042    
043            public void service(
044                            HttpServletRequest request, HttpServletResponse response)
045                    throws IOException, ServletException {
046    
047                    try {
048                            String[] facebookData = FacebookUtil.getFacebookData(request);
049    
050                            if ((facebookData == null) ||
051                                    !PortalUtil.isValidResourceId(facebookData[1])) {
052    
053                                    PortalUtil.sendError(
054                                            HttpServletResponse.SC_NOT_FOUND,
055                                            new NoSuchLayoutException(), request, response);
056                            }
057                            else {
058                                    String facebookCanvasPageURL = facebookData[0];
059                                    String redirect = facebookData[1];
060    
061                                    request.setAttribute(
062                                            WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
063                                    request.setAttribute(GZipFilter.SKIP_FILTER, Boolean.TRUE);
064    
065                                    ServletContext servletContext = getServletContext();
066    
067                                    RequestDispatcher requestDispatcher =
068                                            servletContext.getRequestDispatcher(redirect);
069    
070                                    StringServletResponse stringResponse =
071                                            new StringServletResponse(response);
072    
073                                    requestDispatcher.forward(request, stringResponse);
074    
075                                    String fbml = stringResponse.getString();
076    
077                                    fbml = fixFbml(fbml);
078    
079                                    ServletResponseUtil.write(response, fbml);
080                            }
081                    }
082                    catch (Exception e) {
083                            _log.error(e, e);
084    
085                            PortalUtil.sendError(
086                                    HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
087                                    response);
088                    }
089            }
090    
091            protected String fixFbml(String fbml) {
092                    fbml = StringUtil.replace(
093                            fbml,
094                            new String[] {
095                                    "<nobr>",
096                                    "</nobr>"
097                            },
098                            new String[] {
099                                    StringPool.BLANK,
100                                    StringPool.BLANK
101                            });
102    
103                    return fbml;
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(FacebookServlet.class);
107    
108    }