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