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.portlet.social.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.Http;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.Portal;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  /**
36   * <a href="FacebookUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Jorge Ferrer
39   */
40  public class FacebookUtil {
41  
42      public static final String FACEBOOK_APPS_URL = "http://apps.facebook.com/";
43  
44      public static final String FACEBOOK_SERVLET_PATH = "/facebook/";
45  
46      public static String getCallbackURL(
47          String fbmlPortletURL, String facebookCanvasPageURL) {
48  
49          int pos = fbmlPortletURL.indexOf(
50              StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
51  
52          StringBuilder sb = new StringBuilder();
53  
54          sb.append(fbmlPortletURL.substring(0, pos));
55          sb.append(FACEBOOK_SERVLET_PATH);
56          sb.append(facebookCanvasPageURL);
57          sb.append(fbmlPortletURL.substring(pos));
58  
59          String callbackURL = sb.toString();
60  
61          if (!callbackURL.endsWith(StringPool.SLASH)) {
62              callbackURL += StringPool.SLASH;
63          }
64  
65          return callbackURL;
66      }
67  
68      public static String[] getFacebookData(HttpServletRequest request) {
69          String path = GetterUtil.getString(request.getPathInfo());
70  
71          if (Validator.isNull(path)) {
72              return null;
73          }
74  
75          int pos = path.indexOf(StringPool.SLASH, 1);
76  
77          if (pos == -1) {
78              return null;
79          }
80  
81          String facebookCanvasPageURL = path.substring(1, pos);
82  
83          if (_log.isDebugEnabled()) {
84              _log.debug("Facebook canvas page URL " + facebookCanvasPageURL);
85          }
86  
87          if (Validator.isNull(facebookCanvasPageURL)) {
88              return null;
89          }
90  
91          String redirect = path.substring(pos);
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug("Redirect " + redirect);
95          }
96  
97          if (Validator.isNull(redirect)) {
98              return null;
99          }
100 
101         pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
102 
103         String appPath = StringPool.BLANK;
104 
105         if (pos != -1) {
106             pos = path.indexOf(StringPool.SLASH, pos + 3);
107 
108             if (pos != -1) {
109                 appPath = path.substring(pos);
110             }
111         }
112 
113         return new String[] {facebookCanvasPageURL, redirect, appPath};
114     }
115 
116     public static boolean isFacebook(String currentURL) {
117         String path = currentURL;
118 
119         if (currentURL.startsWith(Http.HTTP)) {
120             int pos = currentURL.indexOf(
121                 StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
122 
123             path = currentURL.substring(pos);
124         }
125 
126         if (path.startsWith(FACEBOOK_SERVLET_PATH)) {
127             return true;
128         }
129         else {
130             return false;
131         }
132     }
133 
134     private static Log _log = LogFactoryUtil.getLog(FacebookUtil.class);
135 
136 }