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.test.randomizerbumpers;
016    
017    import com.liferay.portal.LayoutFriendlyURLException;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.model.impl.LayoutImpl;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class FriendlyURLRandomizerBumper implements RandomizerBumper<String> {
026    
027            public static final FriendlyURLRandomizerBumper INSTANCE =
028                    new FriendlyURLRandomizerBumper();
029    
030            @Override
031            public boolean accept(String randomValue) {
032                    if ((randomValue == null) || randomValue.isEmpty()) {
033                            return false;
034                    }
035    
036                    if (randomValue.charAt(0) != CharPool.SLASH) {
037                            randomValue = StringPool.SLASH.concat(randomValue);
038                    }
039    
040                    if (LayoutImpl.validateFriendlyURL(randomValue) != -1) {
041                            return false;
042                    }
043    
044                    try {
045                            LayoutImpl.validateFriendlyURLKeyword(randomValue);
046    
047                            return true;
048                    }
049                    catch (LayoutFriendlyURLException lfurle) {
050                            return false;
051                    }
052            }
053    
054    }