001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet.filters.compoundsessionid;
016    
017    import com.liferay.portal.kernel.util.Props;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    /**
025     * <p>
026     * See http://issues.liferay.com/browse/LPS-18587.
027     * </p>
028     *
029     * @author Michael C. Han
030     * @author Shuyang Zhou
031     */
032    public class CompoundSessionIdSplitterUtil {
033    
034            public static String getSessionIdDelimiter() {
035                    return _sessionIdDelimiter;
036            }
037    
038            public static boolean hasSessionDelimiter() {
039                    if (_sessionIdDelimiter == null) {
040                            Props props = PropsUtil.getProps();
041    
042                            if (props == null) {
043                                    return false;
044                            }
045    
046                            String sessionIdDelimiter = props.get(
047                                    PropsKeys.SESSION_ID_DELIMITER);
048    
049                            if (Validator.isNull(sessionIdDelimiter)) {
050                                    sessionIdDelimiter = props.get(
051                                            "session.id." + ServerDetector.getServerId() +
052                                                    ".delimiter");
053                            }
054    
055                            if (Validator.isNotNull(sessionIdDelimiter)) {
056                                    _sessionIdDelimiter = sessionIdDelimiter;
057    
058                                    return true;
059                            }
060                            else {
061                                    _sessionIdDelimiter = StringPool.BLANK;
062    
063                                    return false;
064                            }
065                    }
066    
067                    return !_sessionIdDelimiter.isEmpty();
068            }
069    
070            public static String parseSessionId(String sessionId) {
071                    String sessionIdDelimiter = _sessionIdDelimiter;
072    
073                    if (sessionIdDelimiter == null) {
074                            return sessionId;
075                    }
076    
077                    int pos = sessionId.indexOf(sessionIdDelimiter);
078    
079                    if (pos == -1) {
080                            return sessionId;
081                    }
082    
083                    return sessionId.substring(0, pos);
084            }
085    
086            private static volatile String _sessionIdDelimiter;
087    
088    }