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.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.Portal;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    public class FacebookUtil {
031    
032            public static final String FACEBOOK_APPS_URL = "http://apps.facebook.com/";
033    
034            public static final String FACEBOOK_SERVLET_PATH = "/facebook/";
035    
036            public static String[] getFacebookData(HttpServletRequest request) {
037                    String path = GetterUtil.getString(request.getPathInfo());
038    
039                    if (Validator.isNull(path)) {
040                            return null;
041                    }
042    
043                    int pos = path.indexOf(StringPool.SLASH, 1);
044    
045                    if (pos == -1) {
046                            return null;
047                    }
048    
049                    String facebookCanvasPageURL = path.substring(1, pos);
050    
051                    if (_log.isDebugEnabled()) {
052                            _log.debug("Facebook canvas page URL " + facebookCanvasPageURL);
053                    }
054    
055                    if (Validator.isNull(facebookCanvasPageURL)) {
056                            return null;
057                    }
058    
059                    String redirect = path.substring(pos);
060    
061                    if (_log.isDebugEnabled()) {
062                            _log.debug("Redirect " + redirect);
063                    }
064    
065                    if (Validator.isNull(redirect)) {
066                            return null;
067                    }
068    
069                    pos = path.indexOf(Portal.FRIENDLY_URL_SEPARATOR);
070    
071                    String appPath = StringPool.BLANK;
072    
073                    if (pos != -1) {
074                            pos = path.indexOf(StringPool.SLASH, pos + 3);
075    
076                            if (pos != -1) {
077                                    appPath = path.substring(pos);
078                            }
079                    }
080    
081                    return new String[] {facebookCanvasPageURL, redirect, appPath};
082            }
083    
084            public static boolean isFacebook(String currentURL) {
085                    String path = currentURL;
086    
087                    if (currentURL.startsWith(Http.HTTP)) {
088                            int pos = currentURL.indexOf(
089                                    StringPool.SLASH, Http.HTTPS_WITH_SLASH.length());
090    
091                            path = currentURL.substring(pos);
092                    }
093    
094                    if (path.startsWith(FACEBOOK_SERVLET_PATH)) {
095                            return true;
096                    }
097                    else {
098                            return false;
099                    }
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(FacebookUtil.class);
103    
104    }