1
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.CharPool;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.Http;
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
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[] getFacebookData(HttpServletRequest request) {
40 String path = GetterUtil.getString(request.getPathInfo());
41
42 if (Validator.isNull(path)) {
43 return null;
44 }
45
46 int pos = path.indexOf(StringPool.SLASH, 1);
47
48 if (pos == -1) {
49 return null;
50 }
51
52 String facebookCanvasPageURL = path.substring(1, pos);
53
54 if (_log.isDebugEnabled()) {
55 _log.debug("Facebook canvas page URL " + facebookCanvasPageURL);
56 }
57
58 if (Validator.isNull(facebookCanvasPageURL)) {
59 return null;
60 }
61
62 String redirect = path.substring(pos);
63
64 if (_log.isDebugEnabled()) {
65 _log.debug("Redirect " + redirect);
66 }
67
68 if (Validator.isNull(redirect)) {
69 return null;
70 }
71
72 pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
73
74 String appPath = StringPool.BLANK;
75
76 if (pos != -1) {
77 pos = path.indexOf(CharPool.SLASH, pos + 3);
78
79 if (pos != -1) {
80 appPath = path.substring(pos);
81 }
82 }
83
84 return new String[] {facebookCanvasPageURL, redirect, appPath};
85 }
86
87 public static boolean isFacebook(String currentURL) {
88 String path = currentURL;
89
90 if (currentURL.startsWith(Http.HTTP)) {
91 int pos = currentURL.indexOf(
92 CharPool.SLASH, Http.HTTPS_WITH_SLASH.length());
93
94 path = currentURL.substring(pos);
95 }
96
97 if (path.startsWith(FACEBOOK_SERVLET_PATH)) {
98 return true;
99 }
100 else {
101 return false;
102 }
103 }
104
105 private static Log _log = LogFactoryUtil.getLog(FacebookUtil.class);
106
107 }