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