001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 020package org.apache.wiki.filters; 021 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.api.core.Engine; 024import org.apache.wiki.api.exceptions.FilterException; 025import org.apache.wiki.api.filters.BasePageFilter; 026import org.apache.wiki.event.WikiEventListener; 027import org.apache.wiki.event.WikiEventManager; 028import org.apache.wiki.event.WikiPageEvent; 029 030import java.util.Properties; 031import org.apache.wiki.security.EventUtil; 032 033/** 034 * Fires WikiPageEvents for page events. 035 * <p> 036 * Adding a PageEventFilter to the FilterManager will automatically attach an event delegate with the WikiEventManager to provide for 037 * firing and listener management. All that remains is then adding the listener to the filter via the WikiEventManager. This is quite 038 * simple: 039 * </p> 040 * <pre> 041 * PageEventFilter filter = new PageEventFilter(); 042 * engine.getFilterManager().addPageFilter(filter,5000); 043 * // attach listener to filter 044 * WikiEventManager.addWikiEventListener(filter,listener); 045 * </pre> 046 * <p> 047 * This class provides convenience methods for adding and removing WikiEventListeners. 048 * </p> 049 * 050 * @see org.apache.wiki.event.WikiEventManager 051 */ 052public class PageEventFilter extends BasePageFilter { 053 054 /** 055 * Called whenever a new PageFilter is instantiated and reset. 056 */ 057 @Override 058 public void initialize( final Engine engine, final Properties properties ) throws FilterException { 059 super.initialize( engine, properties ); 060 } 061 062 /** 063 * This method is called whenever a page has been loaded from the provider, but not yet been sent through the TranslatorReader. 064 * Note that you cannot do HTML translation here, because TranslatorReader is likely to escape it. 065 * 066 * @param wikiContext The current wikicontext. 067 * @param content WikiMarkup. 068 */ 069 @Override 070 public String preTranslate( final Context wikiContext, final String content ) { 071 fireEvent( WikiPageEvent.PRE_TRANSLATE, wikiContext ); 072 return content; 073 } 074 075 076 /** 077 * This method is called after a page has been fed through the TranslatorReader, so anything you are seeing here is translated content. 078 * If you want to do any of your own WikiMarkup2HTML translation, do it here. 079 */ 080 @Override 081 public String postTranslate( final Context wikiContext, final String htmlContent ) { 082 fireEvent( WikiPageEvent.POST_TRANSLATE, wikiContext ); 083 return htmlContent; 084 } 085 086 087 /** 088 * This method is called before the page has been saved to the PageProvider. 089 */ 090 @Override 091 public String preSave( final Context wikiContext, final String content ) { 092 fireEvent( WikiPageEvent.PRE_SAVE, wikiContext ); 093 return content; 094 } 095 096 097 /** 098 * This method is called after the page has been successfully saved. If the saving fails for any reason, then this method will not 099 * be called. 100 * <p> 101 * Since the result is discarded from this method, this is only useful for things like counters, etc. 102 */ 103 @Override 104 public void postSave( final Context wikiContext, final String content ) { 105 fireEvent( WikiPageEvent.POST_SAVE, wikiContext ); 106 } 107 108 109 // events processing ....................................................... 110 111 /** 112 * Registers a WikiEventListener with this instance. This is a convenience method. 113 * 114 * @param listener the event listener 115 */ 116 public final synchronized void addWikiEventListener( final WikiEventListener listener ) { 117 WikiEventManager.addWikiEventListener( this, listener ); 118 } 119 120 /** 121 * Un-registers a WikiEventListener with this instance. This is a convenience method. 122 * 123 * @param listener the event listener 124 */ 125 public final synchronized void removeWikiEventListener( final WikiEventListener listener ) { 126 WikiEventManager.removeWikiEventListener( this, listener ); 127 } 128 129 /** 130 * Fires a WikiPageEvent of the provided type and page name to all registered listeners. Only <tt>PAGE_LOCK</tt> and 131 * <tt>PAGE_UNLOCK</tt> event types will fire an event; other event types are ignored. 132 * 133 * @see org.apache.wiki.event.WikiPageEvent 134 * @param type the WikiPageEvent type to be fired. 135 * @param context the WikiContext of the event. 136 */ 137 protected final void fireEvent( final int type, final Context context ) { 138 if( WikiEventManager.isListening(this ) && WikiPageEvent.isValidType( type ) ) { 139 final WikiPageEvent event = new WikiPageEvent( context.getEngine(), type, context.getPage().getName() ); 140 EventUtil.applyFrom(event, context); 141 WikiEventManager.fireEvent(this, event ); 142 } 143 } 144 145}