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