1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.servlet.StringServletResponse;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.servlet.filters.gzip.GZipFilter;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.social.util.FacebookUtil;
35  import com.liferay.util.servlet.ServletResponseUtil;
36  
37  import java.io.IOException;
38  
39  import javax.servlet.RequestDispatcher;
40  import javax.servlet.ServletContext;
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServlet;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  /**
47   * <a href="FacebookServlet.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class FacebookServlet extends HttpServlet {
53  
54      public void service(
55              HttpServletRequest request, HttpServletResponse response)
56          throws IOException, ServletException {
57  
58          try {
59              String[] facebookData = FacebookUtil.getFacebookData(request);
60  
61              if (facebookData == null) {
62                  PortalUtil.sendError(
63                      HttpServletResponse.SC_NOT_FOUND,
64                      new NoSuchLayoutException(), request, response);
65              }
66              else {
67                  String facebookCanvasPageURL = facebookData[0];
68                  String redirect = facebookData[1];
69  
70                  request.setAttribute(
71                      WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
72                  request.setAttribute(GZipFilter.SKIP_FILTER, Boolean.TRUE);
73  
74                  ServletContext servletContext = getServletContext();
75  
76                  RequestDispatcher requestDispatcher =
77                      servletContext.getRequestDispatcher(redirect);
78  
79                  StringServletResponse stringResponse =
80                      new StringServletResponse(response);
81  
82                  requestDispatcher.forward(request, stringResponse);
83  
84                  String fbml = stringResponse.getString();
85  
86                  fbml = fixFbml(fbml);
87  
88                  ServletResponseUtil.write(response, fbml);
89              }
90          }
91          catch (Exception e) {
92              _log.error(e, e);
93  
94              PortalUtil.sendError(
95                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
96                  response);
97          }
98      }
99  
100     protected String fixFbml(String fbml) {
101         fbml = StringUtil.replace(
102             fbml,
103             new String[] {
104                 "<nobr>",
105                 "</nobr>"
106             },
107             new String[] {
108                 StringPool.BLANK,
109                 StringPool.BLANK
110             });
111 
112         return fbml;
113     }
114 
115     private static Log _log = LogFactoryUtil.getLog(FacebookServlet.class);
116 
117 }