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