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