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 */
019package org.apache.wiki.tags;
020
021import java.io.IOException;
022
023import org.apache.wiki.WikiEngine;
024import org.apache.wiki.WikiPage;
025import org.apache.wiki.api.exceptions.ProviderException;
026import org.apache.wiki.pages.PageLock;
027import org.apache.wiki.pages.PageManager;
028
029import javax.servlet.http.HttpSession;
030
031/**
032 *  Checks whether the page is locked for editing.  If the mode matches,
033 *  the tag body is included.  The "mode" can be any of the following:
034 *  
035 *  <ul>
036 *  <li><b>locked</b> - The page is currently locked, but the lock is owned by someone else.</li>
037 *  <li><b>owned</b> - The page is currently locked and the current user is the owner of the lock.</li>
038 *  <li><b>unlocked</b> - Nobody has locked the page.</li>
039 *  </ul>
040 *  
041 *  @since 2.0
042 */
043public class CheckLockTag
044    extends WikiTagBase
045{
046    private static final long serialVersionUID = 1L;
047    
048    private static enum LockState
049    {
050        LOCKED, NOTLOCKED, OWNED
051    }
052
053    private LockState m_mode;
054
055    /**
056     *  {@inheritDoc}
057     */
058    @Override
059    public void initTag()
060    {
061        super.initTag();
062        m_mode = LockState.NOTLOCKED;
063    }
064
065    /**
066     *  Sets the mode to check for.
067     *  
068     *  @param arg A String for the mode.
069     */
070    public void setMode( String arg )
071    {
072        if( "locked".equals(arg) )
073        {
074            m_mode = LockState.LOCKED;
075        }
076        else if("owned".equals(arg) )
077        {
078            m_mode = LockState.OWNED;
079        }
080        else
081        {
082            m_mode = LockState.NOTLOCKED;
083        }
084    }
085
086    /**
087     *  {@inheritDoc}
088     */
089    @Override
090    public final int doWikiStartTag()
091        throws IOException,
092               ProviderException
093    {
094        WikiEngine engine = m_wikiContext.getEngine();
095        WikiPage   page   = m_wikiContext.getPage();
096
097        if( page != null )
098        {
099            PageManager mgr = engine.getPageManager();
100
101            PageLock lock = mgr.getCurrentLock( page );
102
103            HttpSession session = pageContext.getSession();
104
105            PageLock userLock = (PageLock) session.getAttribute("lock-"+page.getName());
106
107            if( (lock != null && m_mode == LockState.LOCKED && lock != userLock ) ||
108                (lock != null && m_mode == LockState.OWNED && lock == userLock ) ||
109                (lock == null && m_mode == LockState.NOTLOCKED) )
110            {
111                String tid = getId();
112
113                if( tid != null && lock != null )
114                {
115                    pageContext.setAttribute( tid, lock );
116                }
117
118                return EVAL_BODY_INCLUDE;
119            }
120        }
121
122        return SKIP_BODY;
123    }
124
125}