1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet;
21  
22  import com.liferay.portal.NoSuchLayoutException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.servlet.StringServletResponse;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.servlet.filters.gzip.GZipFilter;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.social.util.FacebookUtil;
32  import com.liferay.util.servlet.ServletResponseUtil;
33  
34  import java.io.IOException;
35  
36  import javax.servlet.RequestDispatcher;
37  import javax.servlet.ServletContext;
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServlet;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  /**
44   * <a href="FacebookServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class FacebookServlet extends HttpServlet {
50  
51      public void service(
52              HttpServletRequest request, HttpServletResponse response)
53          throws IOException, ServletException {
54  
55          try {
56              String[] facebookData = FacebookUtil.getFacebookData(request);
57  
58              if (facebookData == null) {
59                  PortalUtil.sendError(
60                      HttpServletResponse.SC_NOT_FOUND,
61                      new NoSuchLayoutException(), request, response);
62              }
63              else {
64                  String facebookCanvasPageURL = facebookData[0];
65                  String redirect = facebookData[1];
66  
67                  request.setAttribute(
68                      WebKeys.FACEBOOK_CANVAS_PAGE_URL, facebookCanvasPageURL);
69                  request.setAttribute(GZipFilter.SKIP_FILTER, Boolean.TRUE);
70  
71                  ServletContext servletContext = getServletContext();
72  
73                  RequestDispatcher requestDispatcher =
74                      servletContext.getRequestDispatcher(redirect);
75  
76                  StringServletResponse stringResponse =
77                      new StringServletResponse(response);
78  
79                  requestDispatcher.forward(request, stringResponse);
80  
81                  String fbml = stringResponse.getString();
82  
83                  fbml = fixFbml(fbml);
84  
85                  ServletResponseUtil.write(response, fbml);
86              }
87          }
88          catch (Exception e) {
89              _log.error(e, e);
90  
91              PortalUtil.sendError(
92                  HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e, request,
93                  response);
94          }
95      }
96  
97      protected String fixFbml(String fbml) {
98          fbml = StringUtil.replace(
99              fbml,
100             new String[] {
101                 "<nobr>",
102                 "</nobr>"
103             },
104             new String[] {
105                 StringPool.BLANK,
106                 StringPool.BLANK
107             });
108 
109         return fbml;
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(FacebookServlet.class);
113 
114 }