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