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.servlet.filters.compoundsessionid;
016    
017    import com.liferay.portal.kernel.servlet.filters.compoundsessionid.CompoundSessionIdSplitter;
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    import com.liferay.portal.util.PropsValues;
023    
024    /**
025     * @author Michael C. Han
026     */
027    public class CompoundSessionIdSplitterImpl
028            implements CompoundSessionIdSplitter {
029    
030            @Override
031            public String getSessionIdDelimiter() {
032                    return _sessionIdDelimiter;
033            }
034    
035            @Override
036            public boolean hasSessionDelimiter() {
037                    return Validator.isNotNull(_sessionIdDelimiter);
038            }
039    
040            @Override
041            public String parseSessionId(String sessionId) {
042                    if (Validator.isNull(_sessionIdDelimiter)) {
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 String _sessionIdDelimiter;
056    
057            static {
058                    String sessionIdDelimiter = PropsValues.SESSION_ID_DELIMITER;
059    
060                    if (Validator.isNull(sessionIdDelimiter)) {
061                            sessionIdDelimiter = PropsUtil.get(
062                                    "session.id." + ServerDetector.getServerId() + ".delimiter");
063                    }
064    
065                    if (sessionIdDelimiter == null) {
066                            sessionIdDelimiter = StringPool.BLANK;
067                    }
068    
069                    _sessionIdDelimiter = sessionIdDelimiter;
070            }
071    
072    }