001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Http;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.util.Portal;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class FacebookUtil {
032    
033            public static final String FACEBOOK_APPS_URL = "http://apps.facebook.com/";
034    
035            public static final String FACEBOOK_SERVLET_PATH = "/facebook/";
036    
037            public static String getCallbackURL(
038                    String fbmlPortletURL, String facebookCanvasPageURL) {
039    
040                    int pos = fbmlPortletURL.indexOf(
041                            StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
042    
043                    StringBundler sb = new StringBundler(4);
044    
045                    sb.append(fbmlPortletURL.substring(0, pos));
046                    sb.append(FACEBOOK_SERVLET_PATH);
047                    sb.append(facebookCanvasPageURL);
048                    sb.append(fbmlPortletURL.substring(pos));
049    
050                    String callbackURL = sb.toString();
051    
052                    if (!callbackURL.endsWith(StringPool.SLASH)) {
053                            callbackURL += StringPool.SLASH;
054                    }
055    
056                    return callbackURL;
057            }
058    
059            public static String[] getFacebookData(HttpServletRequest request) {
060                    String path = GetterUtil.getString(request.getPathInfo());
061    
062                    if (Validator.isNull(path)) {
063                            return null;
064                    }
065    
066                    int pos = path.indexOf(StringPool.SLASH, 1);
067    
068                    if (pos == -1) {
069                            return null;
070                    }
071    
072                    String facebookCanvasPageURL = path.substring(1, pos);
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug("Facebook canvas page URL " + facebookCanvasPageURL);
076                    }
077    
078                    if (Validator.isNull(facebookCanvasPageURL)) {
079                            return null;
080                    }
081    
082                    String redirect = path.substring(pos);
083    
084                    if (_log.isDebugEnabled()) {
085                            _log.debug("Redirect " + redirect);
086                    }
087    
088                    if (Validator.isNull(redirect)) {
089                            return null;
090                    }
091    
092                    pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
093    
094                    String appPath = StringPool.BLANK;
095    
096                    if (pos != -1) {
097                            pos = path.indexOf(StringPool.SLASH, pos + 3);
098    
099                            if (pos != -1) {
100                                    appPath = path.substring(pos);
101                            }
102                    }
103    
104                    return new String[] {facebookCanvasPageURL, redirect, appPath};
105            }
106    
107            public static boolean isFacebook(String currentURL) {
108                    String path = currentURL;
109    
110                    if (currentURL.startsWith(Http.HTTP)) {
111                            int pos = currentURL.indexOf(
112                                    StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
113    
114                            path = currentURL.substring(pos);
115                    }
116    
117                    if (path.startsWith(FACEBOOK_SERVLET_PATH)) {
118                            return true;
119                    }
120                    else {
121                            return false;
122                    }
123            }
124    
125            private static Log _log = LogFactoryUtil.getLog(FacebookUtil.class);
126    
127    }