1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.isValidResourceId(facebookData[1])) {
54  
55                  PortalUtil.sendError(
56                      HttpServletResponse.SC_NOT_FOUND,
57                      new NoSuchLayoutException(), request, response);
58              }
59              else {
60                  String facebookCanvasPageURL = facebookData[0];
61                  String redirect = facebookData[1];
62  
63                  request.setAttribute(
64                      WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
65                  request.setAttribute(GZipFilter.SKIP_FILTER, Boolean.TRUE);
66  
67                  ServletContext servletContext = getServletContext();
68  
69                  RequestDispatcher requestDispatcher =
70                      servletContext.getRequestDispatcher(redirect);
71  
72                  StringServletResponse stringResponse =
73                      new StringServletResponse(response);
74  
75                  requestDispatcher.forward(request, stringResponse);
76  
77                  String fbml = stringResponse.getString();
78  
79                  fbml = fixFbml(fbml);
80  
81                  ServletResponseUtil.write(response, fbml);
82              }
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86  
87              PortalUtil.sendError(
88                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
89                  response);
90          }
91      }
92  
93      protected String fixFbml(String fbml) {
94          fbml = StringUtil.replace(
95              fbml,
96              new String[] {
97                  "<nobr>",
98                  "</nobr>"
99              },
100             new String[] {
101                 StringPool.BLANK,
102                 StringPool.BLANK
103             });
104 
105         return fbml;
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(FacebookServlet.class);
109 
110 }