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;
029import org.apache.wiki.util.Synchronizer;
030
031import java.util.Properties;
032import org.apache.wiki.security.EventUtil;
033import java.util.concurrent.locks.ReentrantLock;
034
035/**
036  * Fires WikiPageEvents for page events.
037  * <p>
038  * Adding a PageEventFilter to the FilterManager will automatically attach an event delegate with the WikiEventManager to provide for
039  * firing and listener management. All that remains is then adding the listener to the filter via the WikiEventManager. This is quite
040  * simple:
041  * </p>
042  * <pre>
043  *    PageEventFilter filter = new PageEventFilter();
044  *    engine.getFilterManager().addPageFilter(filter,5000);
045  *    // attach listener to filter
046  *    WikiEventManager.addWikiEventListener(filter,listener);
047  * </pre>
048  * <p>
049  * This class provides convenience methods for adding and removing WikiEventListeners.
050  * </p>
051  *
052  * @see org.apache.wiki.event.WikiEventManager
053  */
054public class PageEventFilter extends BasePageFilter {
055
056    /**
057     * A lock used to ensure thread safety when accessing shared resources.
058     * This lock provides more flexibility and capabilities than the intrinsic locking mechanism,
059     * such as the ability to attempt to acquire a lock with a timeout, or to interrupt a thread
060     * waiting to acquire a lock.
061     *
062     * @see java.util.concurrent.locks.ReentrantLock
063     */
064    private final ReentrantLock lock = new ReentrantLock();
065
066    /**
067     * Called whenever a new PageFilter is instantiated and reset.
068     */
069    @Override
070    public void initialize( final Engine engine, final Properties properties ) throws FilterException {
071        super.initialize( engine, properties );
072    }
073
074    /**
075     * This method is called whenever a page has been loaded from the provider, but not yet been sent through the TranslatorReader.
076     * Note that you cannot do HTML translation here, because TranslatorReader is likely to escape it.
077     *
078     * @param wikiContext The current wikicontext.
079     * @param content WikiMarkup.
080     */
081    @Override
082    public String preTranslate( final Context wikiContext, final String content ) {
083        fireEvent( WikiPageEvent.PRE_TRANSLATE, wikiContext );
084        return content;
085    }
086
087
088    /**
089     * This method is called after a page has been fed through the TranslatorReader, so anything you are seeing here is translated content.
090     * If you want to do any of your own WikiMarkup2HTML translation, do it here.
091     */
092    @Override
093    public String postTranslate( final Context wikiContext, final String htmlContent ) {
094        fireEvent( WikiPageEvent.POST_TRANSLATE, wikiContext );
095        return htmlContent;
096    }
097
098
099    /**
100      * This method is called before the page has been saved to the PageProvider.
101      */
102    @Override
103    public String preSave( final Context wikiContext, final String content ) {
104        fireEvent( WikiPageEvent.PRE_SAVE, wikiContext );
105        return content;
106    }
107
108
109    /**
110      * This method is called after the page has been successfully saved. If the saving fails for any reason, then this method will not
111      * be called.
112      * <p>
113      * Since the result is discarded from this method, this is only useful for things like counters, etc.
114      */
115    @Override
116    public void postSave( final Context wikiContext, final String content ) {
117        fireEvent( WikiPageEvent.POST_SAVE, wikiContext );
118    }
119
120
121    // events processing .......................................................
122
123    /**
124     *  Registers a WikiEventListener with this instance. This is a convenience method.
125     *
126     * @param listener the event listener
127     */
128    public final void addWikiEventListener( final WikiEventListener listener ) {
129        Synchronizer.synchronize(lock, () -> {
130            WikiEventManager.addWikiEventListener( this, listener );
131        });
132    }
133
134    /**
135     *  Un-registers a WikiEventListener with this instance. This is a convenience method.
136     *
137     * @param listener the event listener
138     */
139    public final void removeWikiEventListener( final WikiEventListener listener ) {
140        Synchronizer.synchronize(lock, () -> {
141            WikiEventManager.removeWikiEventListener( this, listener );
142        });
143    }
144
145    /**
146     *  Fires a WikiPageEvent of the provided type and page name to all registered listeners. Only <tt>PAGE_LOCK</tt> and
147     *  <tt>PAGE_UNLOCK</tt> event types will fire an event; other event types are ignored.
148     *
149     * @see org.apache.wiki.event.WikiPageEvent
150     * @param type      the WikiPageEvent type to be fired.
151     * @param context   the WikiContext of the event.
152     */
153    protected final void fireEvent( final int type, final Context context ) {
154        if( WikiEventManager.isListening(this ) && WikiPageEvent.isValidType( type ) ) {
155            final WikiPageEvent event = new WikiPageEvent( context.getEngine(), type, context.getPage().getName() );
156            EventUtil.applyFrom(event, context);
157            WikiEventManager.fireEvent(this, event );
158        }
159    }
160
161}