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