001    /**
002     * Copyright (c) 2000-present 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 https://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 _SESSION_ID_DELIMITER;
035            }
036    
037            public static boolean hasSessionDelimiter() {
038                    return _HAS_SESSION_DELIMITER;
039            }
040    
041            public static String parseSessionId(String sessionId) {
042                    if (!_HAS_SESSION_DELIMITER) {
043                            return sessionId;
044                    }
045    
046                    int pos = sessionId.indexOf(_SESSION_ID_DELIMITER);
047    
048                    if (pos == -1) {
049                            return sessionId;
050                    }
051    
052                    return sessionId.substring(0, pos);
053            }
054    
055            private static final boolean _HAS_SESSION_DELIMITER;
056    
057            private static final String _SESSION_ID_DELIMITER;
058    
059            static {
060                    String sessionIdDelimiter = PropsUtil.get(
061                            PropsKeys.SESSION_ID_DELIMITER);
062    
063                    if (Validator.isNull(sessionIdDelimiter)) {
064                            sessionIdDelimiter = PropsUtil.get(
065                                    "session.id." + ServerDetector.getServerId() + ".delimiter");
066                    }
067    
068                    if (Validator.isNotNull(sessionIdDelimiter)) {
069                            _HAS_SESSION_DELIMITER = true;
070                            _SESSION_ID_DELIMITER = sessionIdDelimiter;
071                    }
072                    else {
073                            _HAS_SESSION_DELIMITER = false;
074                            _SESSION_ID_DELIMITER = StringPool.BLANK;
075                    }
076            }
077    
078    }