Return-Path: X-Original-To: apmail-incubator-jspwiki-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-jspwiki-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8B89BE2A1 for ; Wed, 26 Dec 2012 22:17:22 +0000 (UTC) Received: (qmail 25594 invoked by uid 500); 26 Dec 2012 22:17:22 -0000 Delivered-To: apmail-incubator-jspwiki-commits-archive@incubator.apache.org Received: (qmail 25574 invoked by uid 500); 26 Dec 2012 22:17:22 -0000 Mailing-List: contact jspwiki-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jspwiki-dev@incubator.apache.org Delivered-To: mailing list jspwiki-commits@incubator.apache.org Received: (qmail 25567 invoked by uid 99); 26 Dec 2012 22:17:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Dec 2012 22:17:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Dec 2012 22:17:16 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A1F7223889FA; Wed, 26 Dec 2012 22:16:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1426051 - /incubator/jspwiki/trunk/src/org/apache/wiki/filters/ Date: Wed, 26 Dec 2012 22:16:54 -0000 To: jspwiki-commits@incubator.apache.org From: juanpablo@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121226221654.A1F7223889FA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: juanpablo Date: Wed Dec 26 22:16:53 2012 New Revision: 1426051 URL: http://svn.apache.org/viewvc?rev=1426051&view=rev Log: adapted to use new Filter API. Added: incubator/jspwiki/trunk/src/org/apache/wiki/filters/DefaultFilterManager.java Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/BasicPageFilter.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/CreoleFilter.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterException.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterManager.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/PageFilter.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/PingWeblogsComFilter.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/ProfanityFilter.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/RedirectException.java incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/BasicPageFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/BasicPageFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/BasicPageFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/BasicPageFilter.java Wed Dec 26 22:16:53 2012 @@ -29,10 +29,8 @@ import org.apache.wiki.WikiEngine; * and implement only methods that you need. * */ -public class BasicPageFilter - implements PageFilter +public class BasicPageFilter extends org.apache.wiki.api.filters.BasicPageFilter { - protected WikiEngine m_engine; /** * If you override this, you should call super.initialize() first. @@ -80,10 +78,4 @@ public class BasicPageFilter { } - /** - * {@inheritDoc} - */ - public void destroy( WikiEngine engine ) - { - } } Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/CreoleFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/CreoleFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/CreoleFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/CreoleFilter.java Wed Dec 26 22:16:53 2012 @@ -20,10 +20,11 @@ package org.apache.wiki.filters; import java.util.Properties; +import org.apache.log4j.Logger; import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; -import org.apache.wiki.filters.BasicPageFilter; -import org.apache.wiki.filters.FilterException; +import org.apache.wiki.api.filters.BasicPageFilter; +import org.apache.wiki.api.exceptions.FilterException; import org.apache.wiki.parser.CreoleToJSPWikiTranslator; /** @@ -49,6 +50,9 @@ import org.apache.wiki.parser.CreoleToJS public class CreoleFilter extends BasicPageFilter { + + private static final Logger log = Logger.getLogger(CreoleFilter.class); + /** * {@inheritDoc} */ @@ -70,7 +74,7 @@ public class CreoleFilter extends BasicP } catch(Exception e ) { - e.printStackTrace(); + log.error( e.getMessage(), e ); return e.getMessage(); } } @@ -78,7 +82,6 @@ public class CreoleFilter extends BasicP /** * {@inheritDoc} */ - public String preTranslate(WikiContext wikiContext, String content) throws FilterException { @@ -86,11 +89,10 @@ public class CreoleFilter extends BasicP { Properties prop = wikiContext.getEngine().getWikiProperties(); return new CreoleToJSPWikiTranslator().translate(prop ,content); - } catch (Exception e) { - e.printStackTrace(); + log.error( e.getMessage(), e ); return content + "\n \n %%error \n" + "[CreoleFilterError]: This page was not translated by the CreoleFilter due to " Added: incubator/jspwiki/trunk/src/org/apache/wiki/filters/DefaultFilterManager.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/DefaultFilterManager.java?rev=1426051&view=auto ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/DefaultFilterManager.java (added) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/DefaultFilterManager.java Wed Dec 26 22:16:53 2012 @@ -0,0 +1,532 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.wiki.filters; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.*; + +import org.apache.log4j.Logger; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.jdom.xpath.XPath; + +import org.apache.wiki.WikiContext; +import org.apache.wiki.WikiEngine; +import org.apache.wiki.WikiException; +import org.apache.wiki.api.filters.PageFilter; +import org.apache.wiki.api.exceptions.FilterException; +import org.apache.wiki.event.WikiEventManager; +import org.apache.wiki.event.WikiPageEvent; +import org.apache.wiki.modules.ModuleManager; +import org.apache.wiki.modules.WikiModuleInfo; +import org.apache.wiki.util.ClassUtil; +import org.apache.wiki.util.PriorityList; + + +/** + * Manages the page filters. Page filters are components that can be executed + * at certain places: + *
    + *
  • Before the page is translated into HTML. + *
  • After the page has been translated into HTML. + *
  • Before the page is saved. + *
  • After the page has been saved. + *
+ * + * Using page filters allows you to modify the page data on-the-fly, and do things like + * adding your own custom WikiMarkup. + * + *

+ * The initial page filter configuration is kept in a file called "filters.xml". The + * format is really very simple: + *

+ *  
+ * 
+ *  
+ *
+ *    
+ *      org.apache.wiki.filters.ProfanityFilter
+ *    
+ *  
+ *    
+ *      org.apache.wiki.filters.TestFilter
+ *
+ *      
+ *        foobar
+ *        Zippadippadai
+ *      
+ *
+ *      
+ *        blatblaa
+ *        5
+ *      
+ *
+ *    
+ *  
+ *  
+ * + * The <filter> -sections define the filters. For more information, please see + * the PageFilterConfiguration page in the JSPWiki distribution. + * + * FIXME: this class should be final in 2.10. It isn't right now because of compatibility reasons + */ +public class DefaultFilterManager extends ModuleManager implements org.apache.wiki.api.filters.FilterManager +{ + private PriorityList m_pageFilters = new PriorityList(); + + private Map m_filterClassMap = new HashMap(); + + private static final Logger log = Logger.getLogger(DefaultFilterManager.class); + + /** + * Constructs a new FilterManager object. + * + * @param engine The WikiEngine which owns the FilterManager + * @param props Properties to initialize the FilterManager with + * @throws WikiException If something goes wrong. + */ + public DefaultFilterManager( WikiEngine engine, Properties props ) + throws WikiException + { + super( engine ); + initialize( props ); + } + + /** + * Adds a page filter to the queue. The priority defines in which + * order the page filters are run, the highest priority filters go + * in the queue first. + *

+ * In case two filters have the same priority, their execution order + * is the insertion order. + * + * @since 2.1.44. + * @param f PageFilter to add + * @param priority The priority in which position to add it in. + * @throws IllegalArgumentException If the PageFilter is null or invalid. + */ + public void addPageFilter( PageFilter f, int priority ) throws IllegalArgumentException + { + if( f == null ) + { + throw new IllegalArgumentException("Attempt to provide a null filter - this should never happen. Please check your configuration (or if you're a developer, check your own code.)"); + } + + m_pageFilters.add( f, priority ); + } + + private void initPageFilter( String className, Properties props ) + { + try + { + PageFilterInfo info = m_filterClassMap.get( className ); + + if( info != null && !checkCompatibility(info) ) + { + String msg = "Filter '"+info.getName()+"' not compatible with this version of JSPWiki"; + log.warn(msg); + return; + } + + int priority = 0; // FIXME: Currently fixed. + + Class cl = ClassUtil.findClass( "org.apache.wiki.filters", + className ); + + PageFilter filter = (PageFilter)cl.newInstance(); + + filter.initialize( m_engine, props ); + + addPageFilter( filter, priority ); + log.info("Added page filter "+cl.getName()+" with priority "+priority); + } + catch( ClassNotFoundException e ) + { + log.error("Unable to find the filter class: "+className); + } + catch( InstantiationException e ) + { + log.error("Cannot create filter class: "+className); + } + catch( IllegalAccessException e ) + { + log.error("You are not allowed to access class: "+className); + } + catch( ClassCastException e ) + { + log.error("Suggested class is not a PageFilter: "+className); + } + catch( FilterException e ) + { + log.error("Filter "+className+" failed to initialize itself.", e); + } + } + + + /** + * Initializes the filters from an XML file. + * + * @param props The list of properties. Typically jspwiki.properties + * @throws WikiException If something goes wrong. + */ + protected void initialize( Properties props ) + throws WikiException + { + InputStream xmlStream = null; + String xmlFile = props.getProperty( PROP_FILTERXML ); + + try + { + registerFilters(); + + if( m_engine.getServletContext() != null ) + { + log.debug( "Attempting to locate " + DEFAULT_XMLFILE + " from servlet context." ); + if( xmlFile == null ) + { + xmlStream = m_engine.getServletContext().getResourceAsStream( DEFAULT_XMLFILE ); + } + else + { + xmlStream = m_engine.getServletContext().getResourceAsStream( xmlFile ); + } + } + + if( xmlStream == null ) + { + // just a fallback element to the old behaviour prior to 2.5.8 + log.debug( "Attempting to locate filters.xml from class path." ); + + if( xmlFile == null ) + { + xmlStream = getClass().getResourceAsStream( "/filters.xml" ); + } + else + { + xmlStream = getClass().getResourceAsStream( xmlFile ); + } + } + + if( (xmlStream == null) && (xmlFile != null) ) + { + log.debug("Attempting to load property file "+xmlFile); + xmlStream = new FileInputStream( new File(xmlFile) ); + } + + if( xmlStream == null ) + { + log.info("Cannot find property file for filters (this is okay, expected to find it as: '"+ (xmlFile == null ? DEFAULT_XMLFILE : xmlFile ) +"')"); + return; + } + + parseConfigFile( xmlStream ); + } + catch( IOException e ) + { + log.error("Unable to read property file", e); + } + catch( JDOMException e ) + { + log.error("Problem in the XML file",e); + } + } + + /** + * Parses the XML filters configuration file. + * + * @param xmlStream + * @throws JDOMException + * @throws IOException + */ + private void parseConfigFile( InputStream xmlStream ) + throws JDOMException, + IOException + { + Document doc = new SAXBuilder().build( xmlStream ); + + XPath xpath = XPath.newInstance("/pagefilters/filter"); + + List nodes = xpath.selectNodes( doc ); + + for( Iterator i = nodes.iterator(); i.hasNext(); ) + { + Element f = (Element) i.next(); + + String filterClass = f.getChildText("class"); + + Properties props = new Properties(); + + List params = f.getChildren("param"); + + for( Iterator par = params.iterator(); par.hasNext(); ) + { + Element p = (Element) par.next(); + + props.setProperty( p.getChildText("name"), p.getChildText("value") ); + } + + initPageFilter( filterClass, props ); + } + + } + + + /** + * Does the filtering before a translation. + * + * @param context The WikiContext + * @param pageData WikiMarkup data to be passed through the preTranslate chain. + * @throws FilterException If any of the filters throws a FilterException + * @return The modified WikiMarkup + * + * @see PageFilter#preTranslate(WikiContext, String) + */ + public String doPreTranslateFiltering( WikiContext context, String pageData ) + throws FilterException + { + fireEvent( WikiPageEvent.PRE_TRANSLATE_BEGIN, context ); + + for( PageFilter f : m_pageFilters ) + { + pageData = f.preTranslate( context, pageData ); + } + + fireEvent( WikiPageEvent.PRE_TRANSLATE_END, context ); + + return pageData; + } + + /** + * Does the filtering after HTML translation. + * + * @param context The WikiContext + * @param htmlData HTML data to be passed through the postTranslate + * @throws FilterException If any of the filters throws a FilterException + * @return The modified HTML + * @see PageFilter#postTranslate(WikiContext, String) + */ + public String doPostTranslateFiltering( WikiContext context, String htmlData ) + throws FilterException + { + fireEvent( WikiPageEvent.POST_TRANSLATE_BEGIN, context ); + + for( PageFilter f : m_pageFilters ) + { + htmlData = f.postTranslate( context, htmlData ); + } + + fireEvent( WikiPageEvent.POST_TRANSLATE_END, context ); + + return htmlData; + } + + /** + * Does the filtering before a save to the page repository. + * + * @param context The WikiContext + * @param pageData WikiMarkup data to be passed through the preSave chain. + * @throws FilterException If any of the filters throws a FilterException + * @return The modified WikiMarkup + * @see PageFilter#preSave(WikiContext, String) + */ + public String doPreSaveFiltering( WikiContext context, String pageData ) + throws FilterException + { + fireEvent( WikiPageEvent.PRE_SAVE_BEGIN, context ); + + for( PageFilter f : m_pageFilters ) + { + pageData = f.preSave( context, pageData ); + } + + fireEvent( WikiPageEvent.PRE_SAVE_END, context ); + + return pageData; + } + + /** + * Does the page filtering after the page has been saved. + * + * @param context The WikiContext + * @param pageData WikiMarkup data to be passed through the postSave chain. + * @throws FilterException If any of the filters throws a FilterException + * + * @see PageFilter#postSave(WikiContext, String) + */ + public void doPostSaveFiltering( WikiContext context, String pageData ) + throws FilterException + { + fireEvent( WikiPageEvent.POST_SAVE_BEGIN, context ); + + for( PageFilter f : m_pageFilters ) + { + // log.info("POSTSAVE: "+f.toString() ); + f.postSave( context, pageData ); + } + + fireEvent( WikiPageEvent.POST_SAVE_END, context ); + } + + /** + * Returns the list of filters currently installed. Note that this is not + * a copy, but the actual list. So be careful with it. + * + * @return A List of PageFilter objects + */ + public List< PageFilter > getFilterList() + { + return m_pageFilters; + } + + /** + * + * Notifies PageFilters to clean up their ressources. + * + */ + public void destroy() + { + for( PageFilter f : m_pageFilters ) + { + f.destroy( m_engine ); + } + } + + // events processing ....................................................... + + /** + * Fires a WikiPageEvent of the provided type and WikiContext. + * Invalid WikiPageEvent types are ignored. + * + * @see org.apache.wiki.event.WikiPageEvent + * @param type the WikiPageEvent type to be fired. + * @param context the WikiContext of the event. + */ + public final void fireEvent( int type, WikiContext context ) + { + if ( WikiEventManager.isListening(this) && WikiPageEvent.isValidType(type) ) + { + WikiEventManager.fireEvent(this, + new WikiPageEvent(m_engine,type,context.getPage().getName()) ); + } + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public Collection modules() + { + ArrayList modules = new ArrayList(); + + modules.addAll( m_pageFilters ); + + return modules; + } + + private void registerFilters() + { + log.info( "Registering filters" ); + + SAXBuilder builder = new SAXBuilder(); + + try + { + // + // Register all filters which have created a resource containing its properties. + // + // Get all resources of all plugins. + // + + Enumeration resources = getClass().getClassLoader().getResources( PLUGIN_RESOURCE_LOCATION ); + + while( resources.hasMoreElements() ) + { + URL resource = (URL) resources.nextElement(); + + try + { + log.debug( "Processing XML: " + resource ); + + Document doc = builder.build( resource ); + + List plugins = XPath.selectNodes( doc, "/modules/filter"); + + for( Iterator i = plugins.iterator(); i.hasNext(); ) + { + Element pluginEl = (Element) i.next(); + + String className = pluginEl.getAttributeValue("class"); + + PageFilterInfo pluginInfo = PageFilterInfo.newInstance( className, pluginEl ); + + if( pluginInfo != null ) + { + registerPlugin( pluginInfo ); + } + } + } + catch( java.io.IOException e ) + { + log.error( "Couldn't load " + PLUGIN_RESOURCE_LOCATION + " resources: " + resource, e ); + } + catch( JDOMException e ) + { + log.error( "Error parsing XML for filter: "+PLUGIN_RESOURCE_LOCATION ); + } + } + } + catch( java.io.IOException e ) + { + log.error( "Couldn't load all " + PLUGIN_RESOURCE_LOCATION + " resources", e ); + } + } + + private void registerPlugin(PageFilterInfo pluginInfo) + { + m_filterClassMap.put( pluginInfo.getName(), pluginInfo ); + } + + /** + * Stores information about the filters. + * + * @since 2.6.1 + */ + private static final class PageFilterInfo extends WikiModuleInfo + { + private PageFilterInfo( String name ) + { + super(name); + } + + protected static PageFilterInfo newInstance(String className, Element pluginEl) + { + if( className == null || className.length() == 0 ) return null; + PageFilterInfo info = new PageFilterInfo( className ); + + info.initializeFromXML( pluginEl ); + return info; + } + } +} Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterException.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterException.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterException.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterException.java Wed Dec 26 22:16:53 2012 @@ -18,15 +18,17 @@ */ package org.apache.wiki.filters; -import org.apache.wiki.WikiException; /** * A generic PageFilter exception. * * @since 2.1.112 + * @deprecated will be removed in 2.10 scope. Consider using + * {@link org.apache.wiki.api.exceptions.FilterException} instead */ +@Deprecated public class FilterException - extends WikiException + extends org.apache.wiki.api.exceptions.FilterException { private static final long serialVersionUID = 0L; Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterManager.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterManager.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterManager.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/FilterManager.java Wed Dec 26 22:16:53 2012 @@ -18,29 +18,11 @@ */ package org.apache.wiki.filters; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.*; +import java.util.List; +import java.util.Properties; -import org.apache.log4j.Logger; -import org.jdom.Document; -import org.jdom.Element; -import org.jdom.JDOMException; -import org.jdom.input.SAXBuilder; -import org.jdom.xpath.XPath; - -import org.apache.wiki.WikiContext; import org.apache.wiki.WikiEngine; import org.apache.wiki.WikiException; -import org.apache.wiki.event.WikiEventManager; -import org.apache.wiki.event.WikiPageEvent; -import org.apache.wiki.modules.ModuleManager; -import org.apache.wiki.modules.WikiModuleInfo; -import org.apache.wiki.util.ClassUtil; -import org.apache.wiki.util.PriorityList; /** @@ -88,27 +70,11 @@ import org.apache.wiki.util.PriorityList * The <filter> -sections define the filters. For more information, please see * the PageFilterConfiguration page in the JSPWiki distribution. * + * @deprecated will be removed in 2.10 scope. Consider using {@link DefaultFilterManager} instead */ -public final class FilterManager extends ModuleManager +@Deprecated +public final class FilterManager extends DefaultFilterManager { - private PriorityList m_pageFilters = new PriorityList(); - - private HashMap m_filterClassMap = new HashMap(); - - private static final Logger log = Logger.getLogger(WikiEngine.class); - - /** Property name for setting the filter XML property file. Value is {@value}. */ - public static final String PROP_FILTERXML = "jspwiki.filterConfig"; - - /** Default location for the filter XML property file. Value is {@value}. */ - public static final String DEFAULT_XMLFILE = "/WEB-INF/filters.xml"; - - /** JSPWiki system filters are all below this value. */ - public static final int SYSTEM_FILTER_PRIORITY = -1000; - - /** The standard user level filtering. */ - public static final int USER_FILTER_PRIORITY = 0; - /** * Constructs a new FilterManager object. * @@ -119,433 +85,12 @@ public final class FilterManager extends public FilterManager( WikiEngine engine, Properties props ) throws WikiException { - super( engine ); - initialize( props ); - } - - /** - * Adds a page filter to the queue. The priority defines in which - * order the page filters are run, the highest priority filters go - * in the queue first. - *

- * In case two filters have the same priority, their execution order - * is the insertion order. - * - * @since 2.1.44. - * @param f PageFilter to add - * @param priority The priority in which position to add it in. - * @throws IllegalArgumentException If the PageFilter is null or invalid. - */ - public void addPageFilter( PageFilter f, int priority ) throws IllegalArgumentException - { - if( f == null ) - { - throw new IllegalArgumentException("Attempt to provide a null filter - this should never happen. Please check your configuration (or if you're a developer, check your own code.)"); - } - - m_pageFilters.add( f, priority ); - } - - private void initPageFilter( String className, Properties props ) - { - try - { - PageFilterInfo info = m_filterClassMap.get( className ); - - if( info != null && !checkCompatibility(info) ) - { - String msg = "Filter '"+info.getName()+"' not compatible with this version of JSPWiki"; - log.warn(msg); - return; - } - - int priority = 0; // FIXME: Currently fixed. - - Class cl = ClassUtil.findClass( "org.apache.wiki.filters", - className ); - - PageFilter filter = (PageFilter)cl.newInstance(); - - filter.initialize( m_engine, props ); - - addPageFilter( filter, priority ); - log.info("Added page filter "+cl.getName()+" with priority "+priority); - } - catch( ClassNotFoundException e ) - { - log.error("Unable to find the filter class: "+className); - } - catch( InstantiationException e ) - { - log.error("Cannot create filter class: "+className); - } - catch( IllegalAccessException e ) - { - log.error("You are not allowed to access class: "+className); - } - catch( ClassCastException e ) - { - log.error("Suggested class is not a PageFilter: "+className); - } - catch( FilterException e ) - { - log.error("Filter "+className+" failed to initialize itself.", e); - } + super( engine, props ); } - - - /** - * Initializes the filters from an XML file. - * - * @param props The list of properties. Typically jspwiki.properties - * @throws WikiException If something goes wrong. - */ - protected void initialize( Properties props ) - throws WikiException - { - InputStream xmlStream = null; - String xmlFile = props.getProperty( PROP_FILTERXML ); - - try - { - registerFilters(); - - if( m_engine.getServletContext() != null ) - { - log.debug( "Attempting to locate " + DEFAULT_XMLFILE + " from servlet context." ); - if( xmlFile == null ) - { - xmlStream = m_engine.getServletContext().getResourceAsStream( DEFAULT_XMLFILE ); - } - else - { - xmlStream = m_engine.getServletContext().getResourceAsStream( xmlFile ); - } - } - - if( xmlStream == null ) - { - // just a fallback element to the old behaviour prior to 2.5.8 - log.debug( "Attempting to locate filters.xml from class path." ); - - if( xmlFile == null ) - { - xmlStream = getClass().getResourceAsStream( "/filters.xml" ); - } - else - { - xmlStream = getClass().getResourceAsStream( xmlFile ); - } - } - - if( (xmlStream == null) && (xmlFile != null) ) - { - log.debug("Attempting to load property file "+xmlFile); - xmlStream = new FileInputStream( new File(xmlFile) ); - } - - if( xmlStream == null ) - { - log.info("Cannot find property file for filters (this is okay, expected to find it as: '"+ (xmlFile == null ? DEFAULT_XMLFILE : xmlFile ) +"')"); - return; - } - - parseConfigFile( xmlStream ); - } - catch( IOException e ) - { - log.error("Unable to read property file", e); - } - catch( JDOMException e ) - { - log.error("Problem in the XML file",e); - } - } - - /** - * Parses the XML filters configuration file. - * - * @param xmlStream - * @throws JDOMException - * @throws IOException - */ - private void parseConfigFile( InputStream xmlStream ) - throws JDOMException, - IOException - { - Document doc = new SAXBuilder().build( xmlStream ); - - XPath xpath = XPath.newInstance("/pagefilters/filter"); - List nodes = xpath.selectNodes( doc ); - - for( Iterator i = nodes.iterator(); i.hasNext(); ) - { - Element f = (Element) i.next(); - - String filterClass = f.getChildText("class"); - - Properties props = new Properties(); - - List params = f.getChildren("param"); - - for( Iterator par = params.iterator(); par.hasNext(); ) - { - Element p = (Element) par.next(); - - props.setProperty( p.getChildText("name"), p.getChildText("value") ); - } - - initPageFilter( filterClass, props ); - } - - } - - - /** - * Does the filtering before a translation. - * - * @param context The WikiContext - * @param pageData WikiMarkup data to be passed through the preTranslate chain. - * @throws FilterException If any of the filters throws a FilterException - * @return The modified WikiMarkup - * - * @see PageFilter#preTranslate(WikiContext, String) - */ - public String doPreTranslateFiltering( WikiContext context, String pageData ) - throws FilterException - { - fireEvent( WikiPageEvent.PRE_TRANSLATE_BEGIN, context ); - - for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) - { - PageFilter f = (PageFilter) i.next(); - - pageData = f.preTranslate( context, pageData ); - } - - fireEvent( WikiPageEvent.PRE_TRANSLATE_END, context ); - - return pageData; - } - - /** - * Does the filtering after HTML translation. - * - * @param context The WikiContext - * @param htmlData HTML data to be passed through the postTranslate - * @throws FilterException If any of the filters throws a FilterException - * @return The modified HTML - * @see PageFilter#postTranslate(WikiContext, String) - */ - public String doPostTranslateFiltering( WikiContext context, String htmlData ) - throws FilterException - { - fireEvent( WikiPageEvent.POST_TRANSLATE_BEGIN, context ); - - for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) - { - PageFilter f = (PageFilter) i.next(); - - htmlData = f.postTranslate( context, htmlData ); - } - - fireEvent( WikiPageEvent.POST_TRANSLATE_END, context ); - - return htmlData; - } - - /** - * Does the filtering before a save to the page repository. - * - * @param context The WikiContext - * @param pageData WikiMarkup data to be passed through the preSave chain. - * @throws FilterException If any of the filters throws a FilterException - * @return The modified WikiMarkup - * @see PageFilter#preSave(WikiContext, String) - */ - public String doPreSaveFiltering( WikiContext context, String pageData ) - throws FilterException - { - fireEvent( WikiPageEvent.PRE_SAVE_BEGIN, context ); - - for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) - { - PageFilter f = (PageFilter) i.next(); - - pageData = f.preSave( context, pageData ); - } - - fireEvent( WikiPageEvent.PRE_SAVE_END, context ); - - return pageData; - } - - /** - * Does the page filtering after the page has been saved. - * - * @param context The WikiContext - * @param pageData WikiMarkup data to be passed through the postSave chain. - * @throws FilterException If any of the filters throws a FilterException - * - * @see PageFilter#postSave(WikiContext, String) - */ - public void doPostSaveFiltering( WikiContext context, String pageData ) - throws FilterException - { - fireEvent( WikiPageEvent.POST_SAVE_BEGIN, context ); - - for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) - { - PageFilter f = (PageFilter) i.next(); - - // log.info("POSTSAVE: "+f.toString() ); - f.postSave( context, pageData ); - } - - fireEvent( WikiPageEvent.POST_SAVE_END, context ); - } - - /** - * Returns the list of filters currently installed. Note that this is not - * a copy, but the actual list. So be careful with it. - * - * @return A List of PageFilter objects - */ public List getFilterList() { - return m_pageFilters; - } - - /** - * - * Notifies PageFilters to clean up their ressources. - * - */ - public void destroy() - { - for( Iterator i = m_pageFilters.iterator(); i.hasNext(); ) - { - PageFilter f = (PageFilter) i.next(); - - f.destroy( m_engine ); - } - } - - // events processing ....................................................... - - /** - * Fires a WikiPageEvent of the provided type and WikiContext. - * Invalid WikiPageEvent types are ignored. - * - * @see org.apache.wiki.event.WikiPageEvent - * @param type the WikiPageEvent type to be fired. - * @param context the WikiContext of the event. - */ - public final void fireEvent( int type, WikiContext context ) - { - if ( WikiEventManager.isListening(this) && WikiPageEvent.isValidType(type) ) - { - WikiEventManager.fireEvent(this, - new WikiPageEvent(m_engine,type,context.getPage().getName()) ); - } - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - public Collection modules() - { - ArrayList modules = new ArrayList(); - - modules.addAll( m_pageFilters ); - - return modules; - } - - private void registerFilters() - { - log.info( "Registering filters" ); - - SAXBuilder builder = new SAXBuilder(); - - try - { - // - // Register all filters which have created a resource containing its properties. - // - // Get all resources of all plugins. - // - - Enumeration resources = getClass().getClassLoader().getResources( PLUGIN_RESOURCE_LOCATION ); - - while( resources.hasMoreElements() ) - { - URL resource = (URL) resources.nextElement(); - - try - { - log.debug( "Processing XML: " + resource ); - - Document doc = builder.build( resource ); - - List plugins = XPath.selectNodes( doc, "/modules/filter"); - - for( Iterator i = plugins.iterator(); i.hasNext(); ) - { - Element pluginEl = (Element) i.next(); - - String className = pluginEl.getAttributeValue("class"); - - PageFilterInfo pluginInfo = PageFilterInfo.newInstance( className, pluginEl ); - - if( pluginInfo != null ) - { - registerPlugin( pluginInfo ); - } - } - } - catch( java.io.IOException e ) - { - log.error( "Couldn't load " + PLUGIN_RESOURCE_LOCATION + " resources: " + resource, e ); - } - catch( JDOMException e ) - { - log.error( "Error parsing XML for filter: "+PLUGIN_RESOURCE_LOCATION ); - } - } - } - catch( java.io.IOException e ) - { - log.error( "Couldn't load all " + PLUGIN_RESOURCE_LOCATION + " resources", e ); - } - } - - private void registerPlugin(PageFilterInfo pluginInfo) - { - m_filterClassMap.put( pluginInfo.getName(), pluginInfo ); - } - - /** - * Stores information about the filters. - * - * @since 2.6.1 - */ - private static final class PageFilterInfo extends WikiModuleInfo - { - private PageFilterInfo( String name ) - { - super(name); - } - - protected static PageFilterInfo newInstance(String className, Element pluginEl) - { - if( className == null || className.length() == 0 ) return null; - PageFilterInfo info = new PageFilterInfo( className ); - - info.initializeFromXML( pluginEl ); - return info; - } + return super.getFilterList(); } + } Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/PageFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/PageFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/PageFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/PageFilter.java Wed Dec 26 22:16:53 2012 @@ -40,8 +40,11 @@ import org.apache.wiki.WikiEngine; *

* As of 2.5.30, initialize() gains access to the WikiEngine. * + * @deprecated will be removed in 2.10 scope. Consider using + * {@link org.apache.wiki.api.filters.PageFilter} instead */ -public interface PageFilter +@Deprecated +public interface PageFilter extends org.apache.wiki.api.filters.PageFilter { /** * Is called whenever the a new PageFilter is instantiated and Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/PingWeblogsComFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/PingWeblogsComFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/PingWeblogsComFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/PingWeblogsComFilter.java Wed Dec 26 22:16:53 2012 @@ -18,13 +18,18 @@ */ package org.apache.wiki.filters; -import org.apache.wiki.WikiContext; -import org.apache.wiki.WikiEngine; -import org.apache.xmlrpc.*; -import java.net.URL; import java.net.MalformedURLException; -import java.util.*; +import java.net.URL; +import java.util.Hashtable; +import java.util.Properties; +import java.util.Vector; + import org.apache.log4j.Logger; +import org.apache.wiki.WikiContext; +import org.apache.wiki.WikiEngine; +import org.apache.wiki.api.filters.BasicPageFilter; +import org.apache.xmlrpc.AsyncCallback; +import org.apache.xmlrpc.XmlRpcClient; /** * A very dumb class that pings weblogs.com on each save. INTERNAL USE ONLY SO FAR! Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/ProfanityFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/ProfanityFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/ProfanityFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/ProfanityFilter.java Wed Dec 26 22:16:53 2012 @@ -29,6 +29,7 @@ import org.apache.log4j.Logger; import org.apache.wiki.TextUtil; import org.apache.wiki.WikiContext; +import org.apache.wiki.api.filters.BasicPageFilter; /** * This class is an example of how to have a simple filter. It removes Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/RedirectException.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/RedirectException.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/RedirectException.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/RedirectException.java Wed Dec 26 22:16:53 2012 @@ -23,14 +23,15 @@ package org.apache.wiki.filters; * redirect the user elsewhere. * * @since 2.1.112 + * @deprecated will be removed in 2.10 scope. Consider using + * {@link org.apache.wiki.api.exceptions.RedirectException} instead */ +@Deprecated public class RedirectException - extends FilterException + extends org.apache.wiki.api.exceptions.RedirectException { private static final long serialVersionUID = 0L; - private final String m_where; - /** * Constructs a new RedirectException. * @@ -39,18 +40,7 @@ public class RedirectException */ public RedirectException( String msg, String redirect ) { - super( msg ); - - m_where = redirect; + super( msg, redirect ); } - /** - * Get the URI for redirection. - * - * @return The URI given in the constructor. - */ - public String getRedirect() - { - return m_where; - } } Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java?rev=1426051&r1=1426050&r2=1426051&view=diff ============================================================================== --- incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java (original) +++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java Wed Dec 26 22:16:53 2012 @@ -31,6 +31,8 @@ import org.apache.commons.lang.time.Stop import org.apache.log4j.Logger; import org.apache.oro.text.regex.*; import org.apache.wiki.*; +import org.apache.wiki.api.exceptions.RedirectException; +import org.apache.wiki.api.filters.BasicPageFilter; import org.apache.wiki.attachment.Attachment; import org.apache.wiki.auth.user.UserProfile; import org.apache.wiki.providers.ProviderException;