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.portal.kernel.servlet.filters.compoundsessionid;
016    
017    import com.liferay.portal.kernel.util.PropsKeys;
018    import com.liferay.portal.kernel.util.PropsUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    /**
024     * <p>
025     * See http://issues.liferay.com/browse/LPS-18587.
026     * </p>
027     *
028     * @author Michael C. Han
029     * @author Shuyang Zhou
030     */
031    public class CompoundSessionIdSplitterUtil {
032    
033            public static String getSessionIdDelimiter() {
034                    return _sessionIdDelimiter;
035            }
036    
037            public static boolean hasSessionDelimiter() {
038                    return _hasSessionDelimiter;
039            }
040    
041            public static String parseSessionId(String sessionId) {
042                    if (!_hasSessionDelimiter) {
043                            return sessionId;
044                    }
045    
046                    int pos = sessionId.indexOf(_sessionIdDelimiter);
047    
048                    if (pos == -1) {
049                            return sessionId;
050                    }
051    
052                    return sessionId.substring(0, pos);
053            }
054    
055            private static final boolean _hasSessionDelimiter;
056            private static final String _sessionIdDelimiter;
057    
058            static {
059                    String sessionIdDelimiter = PropsUtil.get(
060                            PropsKeys.SESSION_ID_DELIMITER);
061    
062                    if (Validator.isNull(sessionIdDelimiter)) {
063                            sessionIdDelimiter = PropsUtil.get(
064                                    "session.id." + ServerDetector.getServerId() + ".delimiter");
065                    }
066    
067                    if (Validator.isNotNull(sessionIdDelimiter)) {
068                            _hasSessionDelimiter = true;
069                            _sessionIdDelimiter = sessionIdDelimiter;
070                    }
071                    else {
072                            _hasSessionDelimiter = false;
073                            _sessionIdDelimiter = StringPool.BLANK;
074                    }
075            }
076    
077    }