1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.servlet.StringServletResponse;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.servlet.filters.gzip.GZipFilter;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.social.util.FacebookUtil;
27  import com.liferay.util.servlet.ServletResponseUtil;
28  
29  import java.io.IOException;
30  
31  import javax.servlet.RequestDispatcher;
32  import javax.servlet.ServletContext;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="FacebookServlet.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class FacebookServlet extends HttpServlet {
44  
45      public void service(
46              HttpServletRequest request, HttpServletResponse response)
47          throws IOException, ServletException {
48  
49          try {
50              String[] facebookData = FacebookUtil.getFacebookData(request);
51  
52              if (facebookData == null) {
53                  PortalUtil.sendError(
54                      HttpServletResponse.SC_NOT_FOUND,
55                      new NoSuchLayoutException(), request, response);
56              }
57              else {
58                  String facebookCanvasPageURL = facebookData[0];
59                  String redirect = facebookData[1];
60  
61                  request.setAttribute(
62                      WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
63                  request.setAttribute(GZipFilter.SKIP_FILTER, Boolean.TRUE);
64  
65                  ServletContext servletContext = getServletContext();
66  
67                  RequestDispatcher requestDispatcher =
68                      servletContext.getRequestDispatcher(redirect);
69  
70                  StringServletResponse stringResponse =
71                      new StringServletResponse(response);
72  
73                  requestDispatcher.forward(request, stringResponse);
74  
75                  String fbml = stringResponse.getString();
76  
77                  fbml = fixFbml(fbml);
78  
79                  ServletResponseUtil.write(response, fbml);
80              }
81          }
82          catch (Exception e) {
83              _log.error(e, e);
84  
85              PortalUtil.sendError(
86                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
87                  response);
88          }
89      }
90  
91      protected String fixFbml(String fbml) {
92          fbml = StringUtil.replace(
93              fbml,
94              new String[] {
95                  "<nobr>",
96                  "</nobr>"
97              },
98              new String[] {
99                  StringPool.BLANK,
100                 StringPool.BLANK
101             });
102 
103         return fbml;
104     }
105 
106     private static Log _log = LogFactoryUtil.getLog(FacebookServlet.class);
107 
108 }