Index: src/com/ecyrd/jspwiki/plugin/denounce.properties =================================================================== --- src/com/ecyrd/jspwiki/plugin/denounce.properties (revision 675691) +++ src/com/ecyrd/jspwiki/plugin/denounce.properties (working copy) @@ -8,14 +8,14 @@ # # Which user agents should be denounced. +# Uses java.util.regex.Pattern for matching +denounce.agentpattern.googlebot = .*Googlebot.* +denounce.agentpattern.inktomi = .*Slurp/.* +denounce.agentpattern.fastsearch = .*FAST-WebCrawler/.* +denounce.agentpattern.voila = .*VoilaBot.* +denounce.agentpattern.ms = .*MicrosoftPrototypeCrawler.* +denounce.agentpattern.grub = .*grub-client.* # -denounce.agentpattern.googlebot = *Googlebot* -denounce.agentpattern.inktomi = *Slurp/* -denounce.agentpattern.fastsearch = *FAST-WebCrawler/* -denounce.agentpattern.voila = *VoilaBot* -denounce.agentpattern.ms = *MicrosoftPrototypeCrawler* -denounce.agentpattern.grub = *grub-client* -# # Which referers should be denounced. # #denounce.refererpattern = http://localhost/* @@ -25,6 +25,6 @@ # of ServletRequest.getRemoteHost(), which may return an IP address as # well, if the name cannot be determined. # -denounce.hostpattern.googlebot = *.googlebot.com -denounce.hostpattern.inktomi = *.inktomisearch.com -denounce.hostpattern.alexa = crawl*.alexa.com +denounce.hostpattern.googlebot = .*googlebot\\.com +denounce.hostpattern.inktomi = .*inktomisearch\\.com +denounce.hostpattern.alexa = crawl\\.*\\.alexa\\.com Index: src/com/ecyrd/jspwiki/plugin/Denounce.java =================================================================== --- src/com/ecyrd/jspwiki/plugin/Denounce.java (revision 675691) +++ src/com/ecyrd/jspwiki/plugin/Denounce.java (working copy) @@ -24,9 +24,7 @@ import com.ecyrd.jspwiki.*; import org.apache.log4j.Logger; -import org.apache.oro.text.*; -import org.apache.oro.text.regex.*; - +import java.util.regex.*; import java.util.*; import java.io.InputStream; import java.io.IOException; @@ -46,6 +44,7 @@ public static final String PARAM_LINK = "link"; public static final String PARAM_TEXT = "text"; + private static final String PROPERTYFILE = "com/ecyrd/jspwiki/plugin/denounce.properties"; private static final String PROP_AGENTPATTERN = "denounce.agentpattern."; private static final String PROP_HOSTPATTERN = "denounce.hostpattern."; @@ -67,7 +66,6 @@ { try { - PatternCompiler compiler = new GlobCompiler(); ClassLoader loader = Denounce.class.getClassLoader(); InputStream in = loader.getResourceAsStream( PROPERTYFILE ); @@ -90,18 +88,18 @@ { if( name.startsWith( PROP_REFERERPATTERN ) ) { - c_refererPatterns.add( compiler.compile( props.getProperty(name) ) ); + c_refererPatterns.add( Pattern.compile( props.getProperty(name) ) ); } else if( name.startsWith( PROP_AGENTPATTERN ) ) { - c_agentPatterns.add( compiler.compile( props.getProperty(name) ) ); + c_agentPatterns.add( Pattern.compile( props.getProperty(name) ) ); } else if( name.startsWith( PROP_HOSTPATTERN ) ) { - c_hostPatterns.add( compiler.compile( props.getProperty(name) ) ); + c_hostPatterns.add( Pattern.compile( props.getProperty(name) ) ); } } - catch( MalformedPatternException ex ) + catch(PatternSyntaxException ex ) { log.error( "Malformed URL pattern in "+PROPERTYFILE+": "+props.getProperty(name), ex ); } @@ -157,11 +155,10 @@ */ private boolean matchPattern( List list, String path ) { - PatternMatcher matcher = new Perl5Matcher(); - for( Iterator i = list.iterator(); i.hasNext(); ) { - if( matcher.matches( path, (Pattern)i.next() ) ) + Pattern pattern = (Pattern)i.next(); + if (pattern.matcher(path).find()) { return true; } Index: tests/com/ecyrd/jspwiki/plugin/DenouncePluginTest.java =================================================================== --- tests/com/ecyrd/jspwiki/plugin/DenouncePluginTest.java (revision 0) +++ tests/com/ecyrd/jspwiki/plugin/DenouncePluginTest.java (revision 0) @@ -0,0 +1,119 @@ +package com.ecyrd.jspwiki.plugin; + +import java.util.Properties; +import java.io.InputStream; +import java.io.IOException; + +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.framework.Test; + +import com.ecyrd.jspwiki.TestEngine; +import com.ecyrd.jspwiki.WikiContext; +import com.ecyrd.jspwiki.WikiPage; +import net.sourceforge.stripes.mock.MockHttpServletRequest; + +public class DenouncePluginTest extends TestCase { + Properties props = new Properties(); + TestEngine engine; + WikiContext context; + PluginManager manager; + Properties denounceProps; + private final String pluginCmdLine = "[{Denounce link='http://www.mobileasses.com' text='peoples asses'}]"; + + public DenouncePluginTest(String s) { + super(s); + } + + public void setUp() + throws Exception { + props.load(TestEngine.findTestProperties()); + + engine = new TestEngine(props); + try { + + + ClassLoader loader = Denounce.class.getClassLoader(); + InputStream in = loader.getResourceAsStream("com/ecyrd/jspwiki/plugin/denounce.properties"); + + if (in == null) { + throw new IOException("No property file found! (Check the installation, it should be there.)"); + } + denounceProps = new Properties(); + denounceProps.load(in); + } catch (IOException e) { + fail("failed to load denounce.properties"); + } + + + } + + private void setupHTTPRequest(String host, String header) { + MockHttpServletRequest request = engine.newHttpRequest(); + if (header != null) + request.addHeader("User-Agent", header); + //if(host != null) + + request.getParameterMap().put("page", new String[]{"TestPage"}); + context = new WikiContext(engine, request, new WikiPage(engine, "TestPage")); + manager = new PluginManager(engine, props); + } + + public void tearDown() { + TestEngine.deleteTestPage("TestPage"); + TestEngine.deleteTestPage("Foobar"); + TestEngine.emptyWorkDir(); + } + + public void testSLURPBot() throws Exception { + setupHTTPRequest(null, "Slurp/2.1"); + String res = manager.execute(context, pluginCmdLine); + assertEquals(getDenounceText(), res); + // + setupHTTPRequest(null, "ETSlurp/"); + res = manager.execute(context, pluginCmdLine); + assertEquals(getDenounceText(), res); + + setupHTTPRequest(null, "Slurp"); + res = manager.execute(context, pluginCmdLine); + assertFalse(getDenounceText().equalsIgnoreCase(res)); + + } + public void testGoogleBotWithWrongCase() throws Exception { + setupHTTPRequest(null, "gOOglebot/2.1"); + String res = manager.execute(context, pluginCmdLine); + assertFalse(getDenounceText().equalsIgnoreCase(res)); + } + public void testGoogleBot() throws Exception { + setupHTTPRequest(null, "Googlebot/2.1"); + String res = manager.execute(context, pluginCmdLine); + assertEquals(getDenounceText(), res); + // + setupHTTPRequest(null, "ETSGooglebot/2.1"); + res = manager.execute(context, pluginCmdLine); + assertEquals(getDenounceText(), res); + + setupHTTPRequest(null, "ETSGooglebot"); + res = manager.execute(context, pluginCmdLine); + assertEquals(getDenounceText(), res); + + } + + public void testPlugin() throws Exception { + setupHTTPRequest(null, null); + + String res = manager.execute(context, pluginCmdLine); + + assertEquals("peoples asses", res); + + } + + private String getDenounceText() { + return denounceProps.getProperty("denounce.denouncetext"); + } + + + public static Test suite() { + return new TestSuite(DenouncePluginTest.class); + } +} \ No newline at end of file Index: tests/com/ecyrd/jspwiki/plugin/AllTests.java =================================================================== --- tests/com/ecyrd/jspwiki/plugin/AllTests.java (revision 675691) +++ tests/com/ecyrd/jspwiki/plugin/AllTests.java (working copy) @@ -25,6 +25,7 @@ suite.addTest( ReferringPagesPluginTest.suite() ); suite.addTest( TableOfContentsTest.suite() ); suite.addTest( UndefinedPagesPluginTest.suite() ); + suite.addTest(DenouncePluginTest.suite()); return suite; }