1   /**
2    * Copyright (c) 2000-2008 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.servlet.StringServletResponse;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.servlet.filters.compression.CompressionFilter;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.social.util.FacebookUtil;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  
35  import java.io.IOException;
36  
37  import javax.servlet.RequestDispatcher;
38  import javax.servlet.ServletContext;
39  import javax.servlet.ServletException;
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="FacebookServlet.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class FacebookServlet extends HttpServlet {
54  
55      public void service(
56              HttpServletRequest request, HttpServletResponse response)
57          throws IOException, ServletException {
58  
59          try {
60              String[] facebookData = FacebookUtil.getFacebookData(request);
61  
62              if (facebookData == null) {
63                  PortalUtil.sendError(
64                      HttpServletResponse.SC_NOT_FOUND,
65                      new NoSuchLayoutException(), request, response);
66              }
67              else {
68                  String facebookCanvasPageURL = facebookData[0];
69                  String redirect = facebookData[1];
70  
71                  request.setAttribute(
72                      WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
73                  request.setAttribute(
74                      CompressionFilter.SKIP_FILTER, Boolean.TRUE);
75  
76                  ServletContext servletContext = getServletContext();
77  
78                  RequestDispatcher requestDispatcher =
79                      servletContext.getRequestDispatcher(redirect);
80  
81                  StringServletResponse stringResponse =
82                      new StringServletResponse(response);
83  
84                  requestDispatcher.forward(request, stringResponse);
85  
86                  String fbml = stringResponse.getString();
87  
88                  fbml = fixFbml(fbml);
89  
90                  ServletResponseUtil.write(response, fbml);
91              }
92          }
93          catch (Exception e) {
94              _log.error(e, e);
95  
96              PortalUtil.sendError(
97                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
98                  response);
99          }
100     }
101 
102     protected String fixFbml(String fbml) {
103         fbml = StringUtil.replace(
104             fbml,
105             new String[] {
106                 "<nobr>",
107                 "</nobr>"
108             },
109             new String[] {
110                 StringPool.BLANK,
111                 StringPool.BLANK
112             });
113 
114         return fbml;
115     }
116 
117     private static Log _log = LogFactory.getLog(FacebookServlet.class);
118 
119 }