📄 wikicontext.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. 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 com.ecyrd.jspwiki;import java.io.IOException;import java.security.Permission;import java.security.Principal;import java.text.MessageFormat;import java.util.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.servlet.jsp.PageContext;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.auth.*;import com.ecyrd.jspwiki.auth.permissions.AllPermission;import com.ecyrd.jspwiki.auth.user.UserDatabase;import com.ecyrd.jspwiki.i18n.InternationalizationManager;import com.ecyrd.jspwiki.tags.WikiTagBase;import com.ecyrd.jspwiki.ui.*;import com.ecyrd.jspwiki.preferences.Preferences;/** * <p>Provides state information throughout the processing of a page. A * WikiContext is born when the JSP pages that are the main entry * points, are invoked. The JSPWiki engine creates the new * WikiContext, which basically holds information about the page, the * handling engine, and in which context (view, edit, etc) the * call was done.</p> * <p>A WikiContext also provides request-specific variables, which can * be used to communicate between plugins on the same page, or * between different instances of the same plugin. A WikiContext * variable is valid until the processing of the page has ended. For * an example, please see the Counter plugin.</p> * <p>When a WikiContext is created, it automatically associates a * {@link WikiSession} object with the user's HttpSession. The * WikiSession contains information about the user's authentication * status, and is consulted by {@link #getCurrentUser()}. * object</p> * <p>Do not cache the page object that you get from the WikiContext; always * use getPage()!</p> * * @see com.ecyrd.jspwiki.plugin.Counter * * @author Andrew R. Jaquith */public class WikiContext implements Cloneable, Command{ private Command m_command = null; private WikiPage m_page; private WikiPage m_realPage; private WikiEngine m_engine; private String m_template = "default"; private HashMap<String,Object> m_variableMap = new HashMap<String,Object>(); /** * Stores the HttpServletRequest. May be null, if the request did not * come from a servlet. */ protected HttpServletRequest m_request = null; private WikiSession m_session = null; /** User is administering JSPWiki (Install, SecurityConfig). */ public static final String INSTALL = WikiCommand.INSTALL.getRequestContext(); /** The VIEW context - the user just wants to view the page contents. */ public static final String VIEW = PageCommand.VIEW.getRequestContext(); /** User wants to view or administer workflows. */ public static final String WORKFLOW = WikiCommand.WORKFLOW.getRequestContext(); /** The EDIT context - the user is editing the page. */ public static final String EDIT = PageCommand.EDIT.getRequestContext(); /** User is preparing for a login/authentication. */ public static final String LOGIN = WikiCommand.LOGIN.getRequestContext(); /** User is preparing to log out. */ public static final String LOGOUT = WikiCommand.LOGOUT.getRequestContext(); /** JSPWiki wants to display a message. */ public static final String MESSAGE = WikiCommand.MESSAGE.getRequestContext(); /** User is viewing a DIFF between the two versions of the page. */ public static final String DIFF = PageCommand.DIFF.getRequestContext(); /** User is viewing page history. */ public static final String INFO = PageCommand.INFO.getRequestContext(); /** User is previewing the changes he just made. */ public static final String PREVIEW = PageCommand.PREVIEW.getRequestContext(); /** User has an internal conflict, and does quite not know what to do. Please provide some counseling. */ public static final String CONFLICT = PageCommand.CONFLICT.getRequestContext(); /** An error has been encountered and the user needs to be informed. */ public static final String ERROR = WikiCommand.ERROR.getRequestContext(); /** User is uploading something. */ public static final String UPLOAD = PageCommand.UPLOAD.getRequestContext(); /** User is commenting something. */ public static final String COMMENT = PageCommand.COMMENT.getRequestContext(); /** User is searching for content. */ public static final String FIND = WikiCommand.FIND.getRequestContext(); /** User wishes to create a new group */ public static final String CREATE_GROUP = WikiCommand.CREATE_GROUP.getRequestContext(); /** User is deleting an existing group. */ public static final String DELETE_GROUP = GroupCommand.DELETE_GROUP.getRequestContext(); /** User is editing an existing group. */ public static final String EDIT_GROUP = GroupCommand.EDIT_GROUP.getRequestContext(); /** User is viewing an existing group */ public static final String VIEW_GROUP = GroupCommand.VIEW_GROUP.getRequestContext(); /** User is editing preferences */ public static final String PREFS = WikiCommand.PREFS.getRequestContext(); /** User is renaming a page. */ public static final String RENAME = PageCommand.RENAME.getRequestContext(); /** User is deleting a page or an attachment. */ public static final String DELETE = PageCommand.DELETE.getRequestContext(); /** User is downloading an attachment. */ public static final String ATTACH = PageCommand.ATTACH.getRequestContext(); /** RSS feed is being generated. */ public static final String RSS = PageCommand.RSS.getRequestContext(); /** This is not a JSPWiki context, use it to access static files. */ public static final String NONE = PageCommand.NONE.getRequestContext(); /** Same as NONE; this is just a clarification. */ public static final String OTHER = PageCommand.OTHER.getRequestContext(); /** User is doing administrative things. */ public static final String ADMIN = WikiCommand.ADMIN.getRequestContext(); private static final Logger log = Logger.getLogger( WikiContext.class ); private static final Permission DUMMY_PERMISSION = new java.util.PropertyPermission( "os.name", "read" ); /** * Create a new WikiContext for the given WikiPage. Delegates to * {@link #WikiContext(WikiEngine, HttpServletRequest, WikiPage)}. * @param engine The WikiEngine that is handling the request. * @param page The WikiPage. If you want to create a * WikiContext for an older version of a page, you must use this * constructor. */ public WikiContext( WikiEngine engine, WikiPage page ) { this( engine, null, findCommand( engine, null, page ) ); } /** * <p> * Creates a new WikiContext for the given WikiEngine, Command and * HttpServletRequest. * </p> * <p> * This constructor will also look up the HttpSession associated with the * request, and determine if a WikiSession object is present. If not, a new * one is created. * </p> * @param engine The WikiEngine that is handling the request * @param request The HttpServletRequest that should be associated with this * context. This parameter may be <code>null</code>. * @param command the command * @throws IllegalArgumentException if <code>engine</code> or * <code>command</code> are <code>null</code> */ public WikiContext( WikiEngine engine, HttpServletRequest request, Command command ) throws IllegalArgumentException { super(); if ( engine == null || command == null ) { throw new IllegalArgumentException( "Parameter engine and command must not be null." ); } m_engine = engine; m_request = request; m_session = WikiSession.getWikiSession( engine, request ); m_command = command; // If PageCommand, get the WikiPage if( command instanceof PageCommand ) { m_page = (WikiPage)((PageCommand)command).getTarget(); } // If page not supplied, default to front page to avoid NPEs if( m_page == null ) { m_page = m_engine.getPage( m_engine.getFrontPage() ); // Front page does not exist? if( m_page == null ) { m_page = new WikiPage( m_engine, m_engine.getFrontPage() ); } } m_realPage = m_page; // Special case: retarget any empty 'view' PageCommands to the front page if ( PageCommand.VIEW.equals( command ) && command.getTarget() == null ) { m_command = command.targetedCommand( m_page ); } // Debugging... if( log.isDebugEnabled() ) { HttpSession session = ( request == null ) ? null : request.getSession( false ); String sid = ( session == null ) ? "(null)" : session.getId(); log.debug( "Creating WikiContext for session ID=" + sid + "; target=" + getName() ); } // Figure out what template to use setDefaultTemplate( request ); } /** * Creates a new WikiContext for the given WikiEngine, WikiPage and * HttpServletRequest. This method simply looks up the appropriate Command * using {@link #findCommand(WikiEngine, HttpServletRequest, WikiPage)} and * delegates to * {@link #WikiContext(WikiEngine, HttpServletRequest, Command)}. * @param engine The WikiEngine that is handling the request * @param request The HttpServletRequest that should be associated with this * context. This parameter may be <code>null</code>. * @param page The WikiPage. If you want to create a WikiContext for an * older version of a page, you must supply this parameter */ public WikiContext(WikiEngine engine, HttpServletRequest request, WikiPage page) { this( engine, request, findCommand( engine, request, page ) ); } /** * {@inheritDoc} * @see com.ecyrd.jspwiki.ui.Command#getContentTemplate() */ public String getContentTemplate() { return m_command.getContentTemplate(); } /** * {@inheritDoc} * @see com.ecyrd.jspwiki.ui.Command#getJSP() */ public String getJSP() { return m_command.getContentTemplate(); } /** * Sets a reference to the real page whose content is currently being * rendered. * <p> * Sometimes you may want to render the page using some other page's context. * In those cases, it is highly recommended that you set the setRealPage() * to point at the real page you are rendering. Please see InsertPageTag * for an example. * <p> * Also, if your plugin e.g. does some variable setting, be aware that if it * is embedded in the LeftMenu or some other page added with InsertPageTag, * you should consider what you want to do - do you wish to really reference * the "master" page or the included page. * * @param page The real page which is being rendered. * @return The previous real page * @since 2.3.14 * @see com.ecyrd.jspwiki.tags.InsertPageTag */ public WikiPage setRealPage( WikiPage page ) { WikiPage old = m_realPage; m_realPage = page; updateCommand( m_command.getRequestContext() ); return old; } /** * Gets a reference to the real page whose content is currently being rendered. * If your plugin e.g. does some variable setting, be aware that if it * is embedded in the LeftMenu or some other page added with InsertPageTag, * you should consider what you want to do - do you wish to really reference * the "master" page or the included page. * <p> * For example, in the default template, there is a page called "LeftMenu". * Whenever you access a page, e.g. "Main", the master page will be Main, and * that's what the getPage() will return - regardless of whether your plugin * resides on the LeftMenu or on the Main page. However, getRealPage() * will return "LeftMenu". * * @return A reference to the real page.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -