1
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
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 }