1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.social.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.Http;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.Portal;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  /**
29   * <a href="FacebookUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Jorge Ferrer
32   */
33  public class FacebookUtil {
34  
35      public static final String FACEBOOK_APPS_URL = "http://apps.facebook.com/";
36  
37      public static final String FACEBOOK_SERVLET_PATH = "/facebook/";
38  
39      public static String getCallbackURL(
40          String fbmlPortletURL, String facebookCanvasPageURL) {
41  
42          int pos = fbmlPortletURL.indexOf(
43              StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
44  
45          StringBundler sb = new StringBundler(4);
46  
47          sb.append(fbmlPortletURL.substring(0, pos));
48          sb.append(FACEBOOK_SERVLET_PATH);
49          sb.append(facebookCanvasPageURL);
50          sb.append(fbmlPortletURL.substring(pos));
51  
52          String callbackURL = sb.toString();
53  
54          if (!callbackURL.endsWith(StringPool.SLASH)) {
55              callbackURL += StringPool.SLASH;
56          }
57  
58          return callbackURL;
59      }
60  
61      public static String[] getFacebookData(HttpServletRequest request) {
62          String path = GetterUtil.getString(request.getPathInfo());
63  
64          if (Validator.isNull(path)) {
65              return null;
66          }
67  
68          int pos = path.indexOf(StringPool.SLASH, 1);
69  
70          if (pos == -1) {
71              return null;
72          }
73  
74          String facebookCanvasPageURL = path.substring(1, pos);
75  
76          if (_log.isDebugEnabled()) {
77              _log.debug("Facebook canvas page URL " + facebookCanvasPageURL);
78          }
79  
80          if (Validator.isNull(facebookCanvasPageURL)) {
81              return null;
82          }
83  
84          String redirect = path.substring(pos);
85  
86          if (_log.isDebugEnabled()) {
87              _log.debug("Redirect " + redirect);
88          }
89  
90          if (Validator.isNull(redirect)) {
91              return null;
92          }
93  
94          pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
95  
96          String appPath = StringPool.BLANK;
97  
98          if (pos != -1) {
99              pos = path.indexOf(StringPool.SLASH, pos + 3);
100 
101             if (pos != -1) {
102                 appPath = path.substring(pos);
103             }
104         }
105 
106         return new String[] {facebookCanvasPageURL, redirect, appPath};
107     }
108 
109     public static boolean isFacebook(String currentURL) {
110         String path = currentURL;
111 
112         if (currentURL.startsWith(Http.HTTP)) {
113             int pos = currentURL.indexOf(
114                 StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
115 
116             path = currentURL.substring(pos);
117         }
118 
119         if (path.startsWith(FACEBOOK_SERVLET_PATH)) {
120             return true;
121         }
122         else {
123             return false;
124         }
125     }
126 
127     private static Log _log = LogFactoryUtil.getLog(FacebookUtil.class);
128 
129 }