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; 020 021import org.apache.logging.log4j.LogManager; 022import org.apache.logging.log4j.Logger; 023import org.apache.wiki.api.core.Command; 024import org.apache.wiki.api.core.Context; 025import org.apache.wiki.api.core.ContextEnum; 026import org.apache.wiki.api.core.Engine; 027import org.apache.wiki.api.core.Page; 028import org.apache.wiki.api.core.Session; 029import org.apache.wiki.api.spi.Wiki; 030import org.apache.wiki.auth.AuthorizationManager; 031import org.apache.wiki.auth.NoSuchPrincipalException; 032import org.apache.wiki.auth.UserManager; 033import org.apache.wiki.auth.WikiPrincipal; 034import org.apache.wiki.auth.permissions.AllPermission; 035import org.apache.wiki.auth.user.UserDatabase; 036import org.apache.wiki.pages.PageManager; 037import org.apache.wiki.render.RenderingManager; 038import org.apache.wiki.ui.CommandResolver; 039import org.apache.wiki.ui.Installer; 040import org.apache.wiki.ui.PageCommand; 041import org.apache.wiki.ui.WikiCommand; 042import org.apache.wiki.util.TextUtil; 043 044import jakarta.servlet.http.HttpServletRequest; 045import jakarta.servlet.http.HttpSession; 046import jakarta.servlet.jsp.PageContext; 047import java.security.Permission; 048import java.security.Principal; 049import java.util.HashMap; 050import java.util.PropertyPermission; 051 052/** 053 * <p>Provides state information throughout the processing of a page. A WikiContext is born when the JSP pages that are the main entry 054 * points, are invoked. The JSPWiki engine creates the new WikiContext, which basically holds information about the page, the 055 * handling engine, and in which context (view, edit, etc) the call was done.</p> 056 * <p>A WikiContext also provides request-specific variables, which can be used to communicate between plugins on the same page, or 057 * between different instances of the same plugin. A WikiContext variable is valid until the processing of the page has ended. For 058 * an example, please see the Counter plugin.</p> 059 * <p>When a WikiContext is created, it automatically associates a {@link WikiSession} object with the user's HttpSession. The 060 * WikiSession contains information about the user's authentication status, and is consulted by {@link #getCurrentUser()} object.</p> 061 * <p>Do not cache the page object that you get from the WikiContext; always use getPage()!</p> 062 * 063 * @see org.apache.wiki.plugin.Counter 064 */ 065public class WikiContext implements Context, Command { 066 067 private Command m_command; 068 private WikiPage m_page; 069 private WikiPage m_realPage; 070 private Engine m_engine; 071 private String m_template = "default"; 072 073 private HashMap< String, Object > m_variableMap = new HashMap<>(); 074 075 /** Stores the HttpServletRequest. May be null, if the request did not come from a servlet. */ 076 protected HttpServletRequest m_request; 077 078 private Session m_session; 079 080 /** User is doing administrative things. */ 081 public static final String ADMIN = ContextEnum.WIKI_ADMIN.getRequestContext(); 082 083 /** User is downloading an attachment. */ 084 public static final String ATTACH = ContextEnum.PAGE_ATTACH.getRequestContext(); 085 086 /** User is commenting something. */ 087 public static final String COMMENT = ContextEnum.PAGE_COMMENT.getRequestContext(); 088 089 /** User has an internal conflict, and does quite not know what to do. Please provide some counseling. */ 090 public static final String CONFLICT = ContextEnum.PAGE_CONFLICT.getRequestContext(); 091 092 /** User wishes to create a new group */ 093 public static final String CREATE_GROUP = ContextEnum.WIKI_CREATE_GROUP.getRequestContext(); 094 095 /** User is deleting a page or an attachment. */ 096 public static final String DELETE = ContextEnum.PAGE_DELETE.getRequestContext(); 097 098 /** User is deleting an existing group. */ 099 public static final String DELETE_GROUP = ContextEnum.GROUP_DELETE.getRequestContext(); 100 101 /** User is viewing a DIFF between the two versions of the page. */ 102 public static final String DIFF = ContextEnum.PAGE_DIFF.getRequestContext(); 103 104 /** The EDIT context - the user is editing the page. */ 105 public static final String EDIT = ContextEnum.PAGE_EDIT.getRequestContext(); 106 107 /** User is editing an existing group. */ 108 public static final String EDIT_GROUP = ContextEnum.GROUP_EDIT.getRequestContext(); 109 110 /** An error has been encountered and the user needs to be informed. */ 111 public static final String ERROR = ContextEnum.WIKI_ERROR.getRequestContext(); 112 113 /** User is searching for content. */ 114 public static final String FIND = ContextEnum.WIKI_FIND.getRequestContext(); 115 116 /** User is viewing page history. */ 117 public static final String INFO = ContextEnum.PAGE_INFO.getRequestContext(); 118 119 /** User is administering JSPWiki (Install, SecurityConfig). */ 120 public static final String INSTALL = ContextEnum.WIKI_INSTALL.getRequestContext(); 121 122 /** User is preparing for a login/authentication. */ 123 public static final String LOGIN = ContextEnum.WIKI_LOGIN.getRequestContext(); 124 125 /** User is preparing to log out. */ 126 public static final String LOGOUT = ContextEnum.WIKI_LOGOUT.getRequestContext(); 127 128 /** JSPWiki wants to display a message. */ 129 public static final String MESSAGE = ContextEnum.WIKI_MESSAGE.getRequestContext(); 130 131 /** This is not a JSPWiki context, use it to access static files. */ 132 public static final String NONE = ContextEnum.PAGE_NONE.getRequestContext(); 133 134 /** Same as NONE; this is just a clarification. */ 135 public static final String OTHER = ContextEnum.PAGE_NONE.getRequestContext(); 136 137 /** User is editing preferences */ 138 public static final String PREFS = ContextEnum.WIKI_PREFS.getRequestContext(); 139 140 /** User is previewing the changes he just made. */ 141 public static final String PREVIEW = ContextEnum.PAGE_PREVIEW.getRequestContext(); 142 143 /** User is renaming a page. */ 144 public static final String RENAME = ContextEnum.PAGE_RENAME.getRequestContext(); 145 146 /** RSS feed is being generated. */ 147 public static final String RSS = ContextEnum.PAGE_RSS.getRequestContext(); 148 149 /** User is uploading something. */ 150 public static final String UPLOAD = ContextEnum.PAGE_UPLOAD.getRequestContext(); 151 152 /** The VIEW context - the user just wants to view the page contents. */ 153 public static final String VIEW = ContextEnum.PAGE_VIEW.getRequestContext(); 154 155 /** User is viewing an existing group */ 156 public static final String VIEW_GROUP = ContextEnum.GROUP_VIEW.getRequestContext(); 157 158 /** User wants to view or administer workflows. */ 159 public static final String WORKFLOW = ContextEnum.WIKI_WORKFLOW.getRequestContext(); 160 161 private static final Logger LOG = LogManager.getLogger( WikiContext.class ); 162 163 /** 164 * The permission that {@link #requiredPermission()} returns when a context needs none. The wiki policy does not grant it, so 165 * {@link org.apache.wiki.auth.AuthorizationManager#checkPermission(org.apache.wiki.api.core.Session, java.security.Permission)} allows it to 166 * every session. 167 * 168 * @since 3.0.1 169 */ 170 public static final Permission DUMMY_PERMISSION = new PropertyPermission( "os.name", "read" ); 171 172 /** 173 * Note: it is preferred to use the DSL API for creating a wiki context. 174 * Example: Wiki.contex().create( .. ) 175 * 176 * Create a new WikiContext for the given WikiPage. Delegates to {@link #WikiContext(Engine, HttpServletRequest, Page)}. 177 * 178 * @param engine The Engine that is handling the request. 179 * @param page The WikiPage. If you want to create a WikiContext for an older version of a page, you must use this constructor. 180 */ 181 public WikiContext( final Engine engine, final Page page ) { 182 this( engine, null, findCommand( engine, null, page ) ); 183 } 184 185 /** 186 * <p> 187 * Creates a new WikiContext for the given Engine, Command and HttpServletRequest. 188 * </p> 189 * Note: it is preferred to use the DSL API for creating a wiki context. 190 * Example: Wiki.contex().create( .. ) 191 * <p> 192 * This constructor will also look up the HttpSession associated with the request, and determine if a Session object is present. 193 * If not, a new one is created. 194 * </p> 195 * @param engine The Engine that is handling the request 196 * @param request The HttpServletRequest that should be associated with this context. This parameter may be <code>null</code>. 197 * @param command the command 198 * @throws IllegalArgumentException if <code>engine</code> or <code>command</code> are <code>null</code> 199 */ 200 public WikiContext( final Engine engine, final HttpServletRequest request, final Command command ) throws IllegalArgumentException { 201 if ( engine == null || command == null ) { 202 throw new IllegalArgumentException( "Parameter engine and command must not be null." ); 203 } 204 205 m_engine = engine; 206 m_request = request; 207 m_session = Wiki.session().find( engine, request ); 208 m_command = command; 209 210 // If PageCommand, get the WikiPage 211 if( command instanceof PageCommand ) { 212 m_page = ( WikiPage )command.getTarget(); 213 } 214 215 // If page not supplied, default to front page to avoid NPEs 216 if( m_page == null ) { 217 m_page = ( WikiPage )m_engine.getManager( PageManager.class ).getPage( m_engine.getFrontPage() ); 218 219 // Front page does not exist? 220 if( m_page == null ) { 221 m_page = ( WikiPage )Wiki.contents().page( m_engine, m_engine.getFrontPage() ); 222 } 223 } 224 225 m_realPage = m_page; 226 227 // Special case: retarget any empty 'view' PageCommands to the front page 228 if ( PageCommand.VIEW.equals( command ) && command.getTarget() == null ) { 229 m_command = command.targetedCommand( m_page ); 230 } 231 232 // Debugging... 233 final HttpSession session = ( request == null ) ? null : request.getSession( false ); 234 final String sid = session == null ? "(null)" : session.getId(); 235 LOG.debug( "Creating WikiContext for session ID={}; target={}", sid, getName() ); 236 237 // Figure out what template to use 238 setDefaultTemplate( request ); 239 } 240 241 /** 242 * Creates a new WikiContext for the given Engine, WikiPage and HttpServletRequest. This method simply looks up the appropriate 243 * Command using {@link #findCommand(Engine, HttpServletRequest, Page)} and delegates to 244 * {@link #WikiContext(Engine, HttpServletRequest, Command)}. 245 * 246 * Note: it is preferred to use the DSL API for creating a wiki context. 247 * Example: Wiki.contex().create( .. ) 248 * 249 * @param engine The Engine that is handling the request 250 * @param request The HttpServletRequest that should be associated with this context. This parameter may be <code>null</code>. 251 * @param page The WikiPage. If you want to create a WikiContext for an older version of a page, you must supply this parameter 252 */ 253 public WikiContext( final Engine engine, final HttpServletRequest request, final Page page ) { 254 this( engine, request, findCommand( engine, request, page ) ); 255 } 256 257 /** 258 * Creates a new WikiContext from a supplied HTTP request, using a default wiki context. 259 * 260 * Note: it is preferred to use the DSL API for creating a wiki context. 261 * Example: Wiki.contex().create( .. ) 262 * 263 * @param engine The Engine that is handling the request 264 * @param request the HTTP request 265 * @param requestContext the default context to use 266 * @see org.apache.wiki.ui.CommandResolver 267 * @see org.apache.wiki.api.core.Command 268 * @since 2.1.15. 269 */ 270 public WikiContext( final Engine engine, final HttpServletRequest request, final String requestContext ) { 271 this( engine, request, engine.getManager( CommandResolver.class ).findCommand( request, requestContext ) ); 272 if( !engine.isConfigured() ) { 273 throw new InternalWikiException( "Engine has not been properly started. It is likely that the configuration is faulty. Please check all logs for the possible reason." ); 274 } 275 } 276 277 /** 278 * {@inheritDoc} 279 * @see org.apache.wiki.api.core.Command#getContentTemplate() 280 */ 281 @Override 282 public String getContentTemplate() 283 { 284 return m_command.getContentTemplate(); 285 } 286 287 /** 288 * {@inheritDoc} 289 * @see org.apache.wiki.api.core.Command#getJSP() 290 */ 291 @Override 292 public String getJSP() 293 { 294 return m_command.getContentTemplate(); 295 } 296 297 /** 298 * Sets a reference to the real page whose content is currently being rendered. 299 * <p> 300 * Sometimes you may want to render the page using some other page's context. In those cases, it is highly recommended that you set 301 * the setRealPage() to point at the real page you are rendering. Please see InsertPageTag for an example. 302 * <p> 303 * 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 304 * with InsertPageTag, you should consider what you want to do - do you wish to really reference the "master" page or the included 305 * page. 306 * 307 * @param page The real page which is being rendered. 308 * @return The previous real page 309 * @since 2.3.14 310 * @see org.apache.wiki.tags.InsertPageTag 311 */ 312 @Override 313 public WikiPage setRealPage( final Page page ) { 314 final WikiPage old = m_realPage; 315 m_realPage = ( WikiPage )page; 316 updateCommand( m_command.getRequestContext() ); 317 return old; 318 } 319 320 /** 321 * Gets a reference to the real page whose content is currently being rendered. If your plugin e.g. does some variable setting, be 322 * aware that if it is embedded in the LeftMenu or some other page added with InsertPageTag, you should consider what you want to 323 * do - do you wish to really reference the "master" page or the included page. 324 * <p> 325 * For example, in the default template, there is a page called "LeftMenu". Whenever you access a page, e.g. "Main", the master 326 * page will be Main, and that's what the getPage() will return - regardless of whether your plugin resides on the LeftMenu or on 327 * the Main page. However, getRealPage() will return "LeftMenu". 328 * 329 * @return A reference to the real page. 330 * @see org.apache.wiki.tags.InsertPageTag 331 * @see org.apache.wiki.parser.JSPWikiMarkupParser 332 */ 333 @Override 334 public WikiPage getRealPage() 335 { 336 return m_realPage; 337 } 338 339 /** 340 * Figure out to which page we are really going to. Considers special page names from the jspwiki.properties, and possible aliases. 341 * This method forwards requests to {@link org.apache.wiki.ui.CommandResolver#getSpecialPageReference(String)}. 342 * @return A complete URL to the new page to redirect to 343 * @since 2.2 344 */ 345 @Override 346 public String getRedirectURL() { 347 final String pagename = m_page.getName(); 348 String redirURL = m_engine.getManager( CommandResolver.class ).getSpecialPageReference( pagename ); 349 if( redirURL == null ) { 350 parseForMetadata(); 351 final String alias = m_page.getAttribute( WikiPage.ALIAS ); 352 if( alias != null ) { 353 redirURL = getViewURL( alias ); 354 } else { 355 redirURL = m_page.getAttribute( WikiPage.REDIRECT ); 356 } 357 } 358 359 return redirURL; 360 } 361 362 /** Aliases and redirects are set while parsing, and a page that comes straight from the provider, as without the page cache, was not parsed yet. */ 363 private void parseForMetadata() { 364 if( m_page.hasMetadata() ) { 365 return; 366 } 367 try { 368 final String text = m_engine.getManager( PageManager.class ).getPureText( m_page.getName(), m_page.getVersion() ); 369 m_engine.getManager( RenderingManager.class ).getParser( this, text ).parse(); 370 } catch( final Exception e ) { 371 LOG.debug( "Unable to parse {} for its metadata", m_page, e ); 372 } 373 } 374 375 /** 376 * Returns the handling engine. 377 * 378 * @return The engine owning this context. 379 */ 380 @Override 381 public WikiEngine getEngine() { 382 return ( WikiEngine )m_engine; 383 } 384 385 /** 386 * Returns the page that is being handled. 387 * 388 * @return the page which was fetched. 389 */ 390 @Override 391 public WikiPage getPage() 392 { 393 return m_page; 394 } 395 396 /** 397 * Sets the page that is being handled. 398 * 399 * @param page The wikipage 400 * @since 2.1.37. 401 */ 402 @Override 403 public void setPage( final Page page ) { 404 m_page = (WikiPage)page; 405 updateCommand( m_command.getRequestContext() ); 406 } 407 408 /** 409 * Returns the request context. 410 * 411 * @return The name of the request context (e.g. VIEW). 412 */ 413 @Override 414 public String getRequestContext() 415 { 416 return m_command.getRequestContext(); 417 } 418 419 /** 420 * Sets the request context. See above for the different request contexts (VIEW, EDIT, etc.) 421 * 422 * @param arg The request context (one of the predefined contexts.) 423 */ 424 @Override 425 public void setRequestContext( final String arg ) 426 { 427 updateCommand( arg ); 428 } 429 430 /** 431 * {@inheritDoc} 432 * @see org.apache.wiki.api.core.Command#getTarget() 433 */ 434 @Override 435 public Object getTarget() 436 { 437 return m_command.getTarget(); 438 } 439 440 /** 441 * {@inheritDoc} 442 * @see org.apache.wiki.api.core.Command#getURLPattern() 443 */ 444 @Override 445 public String getURLPattern() 446 { 447 return m_command.getURLPattern(); 448 } 449 450 /** 451 * Gets a previously set variable. 452 * 453 * @param key The variable name. 454 * @return The variable contents. 455 */ 456 @Override 457 @SuppressWarnings( "unchecked" ) 458 public < T > T getVariable( final String key ) { 459 return ( T )m_variableMap.get( key ); 460 } 461 462 /** 463 * Sets a variable. The variable is valid while the WikiContext is valid, i.e. while page processing continues. The variable data 464 * is discarded once the page processing is finished. 465 * 466 * @param key The variable name. 467 * @param data The variable value. 468 */ 469 @Override 470 public void setVariable( final String key, final Object data ) { 471 m_variableMap.put( key, data ); 472 updateCommand( m_command.getRequestContext() ); 473 } 474 475 /** 476 * This is just a simple helper method which will first check the context if there is already an override in place, and if there is not, 477 * it will then check the given properties. 478 * 479 * @param key What key are we searching for? 480 * @param defValue Default value for the boolean 481 * @return {@code true} or {@code false}. 482 */ 483 @Override 484 public boolean getBooleanWikiProperty( final String key, final boolean defValue ) { 485 final String bool = getVariable( key ); 486 if( bool != null ) { 487 return TextUtil.isPositive( bool ); 488 } 489 490 return TextUtil.getBooleanProperty( getEngine().getWikiProperties(), key, defValue ); 491 } 492 493 /** 494 * This method will safely return any HTTP parameters that might have been defined. You should use this method instead 495 * of peeking directly into the result of getHttpRequest(), since this method is smart enough to do all the right things, 496 * figure out UTF-8 encoded parameters, etc. 497 * 498 * @since 2.0.13. 499 * @param paramName Parameter name to look for. 500 * @return HTTP parameter, or null, if no such parameter existed. 501 */ 502 @Override 503 public String getHttpParameter( final String paramName ) { 504 String result = null; 505 if( m_request != null ) { 506 result = m_request.getParameter( paramName ); 507 } 508 509 return result; 510 } 511 512 /** 513 * If the request did originate from an HTTP request, then the HTTP request can be fetched here. However, if the request 514 * did NOT originate from an HTTP request, then this method will return null, and YOU SHOULD CHECK FOR IT! 515 * 516 * @return Null, if no HTTP request was done. 517 * @since 2.0.13. 518 */ 519 @Override 520 public HttpServletRequest getHttpRequest() 521 { 522 return m_request; 523 } 524 525 /** 526 * Sets the template to be used for this request. 527 * 528 * @param dir The template name 529 * @since 2.1.15. 530 */ 531 @Override 532 public void setTemplate( final String dir ) 533 { 534 m_template = dir; 535 } 536 537 /** 538 * Returns the target of this wiki context: a page, group name or JSP. If the associated Command is a PageCommand, this method 539 * returns the page's name. Otherwise, this method delegates to the associated Command's {@link org.apache.wiki.api.core.Command#getName()} 540 * method. Calling classes can rely on the results of this method for looking up canonically-correct page or group names. Because it 541 * does not automatically assume that the wiki context is a PageCommand, calling this method is inherently safer than calling 542 * {@code getPage().getName()}. 543 * 544 * @return the name of the target of this wiki context 545 * @see org.apache.wiki.ui.PageCommand#getName() 546 * @see org.apache.wiki.ui.GroupCommand#getName() 547 */ 548 @Override 549 public String getName() { 550 if ( m_command instanceof PageCommand ) { 551 return m_page != null ? m_page.getName() : "<no page>"; 552 } 553 return m_command.getName(); 554 } 555 556 /** 557 * Gets the template that is to be used throughout this request. 558 * 559 * @since 2.1.15. 560 * @return template name 561 */ 562 @Override 563 public String getTemplate() 564 { 565 return m_template; 566 } 567 568 /** 569 * Convenience method that gets the current user. Delegates the lookup to the WikiSession associated with this WikiContect. 570 * May return null, in case the current user has not yet been determined; or this is an internal system. If the WikiSession has not 571 * been set, <em>always</em> returns null. 572 * 573 * @return The current user; or maybe null in case of internal calls. 574 */ 575 @Override 576 public Principal getCurrentUser() { 577 if (m_session == null) { 578 // This shouldn't happen, really... 579 return WikiPrincipal.GUEST; 580 } 581 return m_session.getLoginPrincipal(); 582 } 583 584 /** 585 * A shortcut to generate a VIEW url. 586 * 587 * @param page The page to which to link. 588 * @return A URL to the page. This honours the current absolute/relative setting. 589 */ 590 @Override 591 public String getViewURL( final String page ) { 592 return getURL( ContextEnum.PAGE_VIEW.getRequestContext(), page, null ); 593 } 594 595 /** 596 * Creates a URL for the given request context. 597 * 598 * @param context e.g. WikiContext.EDIT 599 * @param page The page to which to link 600 * @return A URL to the page, honours the absolute/relative setting in jspwiki.properties 601 */ 602 @Override 603 public String getURL( final String context, final String page ) { 604 return getURL( context, page, null ); 605 } 606 607 /** 608 * Returns a URL from a page. It this WikiContext instance was constructed with an actual HttpServletRequest, we will attempt to 609 * construct the URL using HttpUtil, which preserves the HTTPS portion if it was used. 610 * 611 * @param context The request context (e.g. WikiContext.UPLOAD) 612 * @param page The page to which to link 613 * @param params A list of parameters, separated with "&" 614 * 615 * @return A URL to the given context and page. 616 */ 617 @Override 618 public String getURL( final String context, final String page, final String params ) { 619 // FIXME: is rather slow 620 return m_engine.getURL( context, page, params ); 621 } 622 623 /** 624 * Returns the Command associated with this WikiContext. 625 * 626 * @return the command 627 */ 628 @Override 629 public Command getCommand() { 630 return m_command; 631 } 632 633 /** 634 * Returns a shallow clone of the WikiContext. 635 * 636 * @since 2.1.37. 637 * @return A shallow clone of the WikiContext 638 */ 639 @Override 640 public WikiContext clone() { 641 try { 642 // super.clone() must always be called to make sure that inherited objects 643 // get the right type 644 final WikiContext copy = (WikiContext)super.clone(); 645 646 copy.m_engine = m_engine; 647 copy.m_command = m_command; 648 649 copy.m_template = m_template; 650 copy.m_variableMap = m_variableMap; 651 copy.m_request = m_request; 652 copy.m_session = m_session; 653 copy.m_page = m_page; 654 copy.m_realPage = m_realPage; 655 return copy; 656 } catch( final CloneNotSupportedException e ){ 657 // Never happens 658 LOG.debug(e.getMessage(), e); 659 } 660 661 return null; 662 } 663 664 /** 665 * Creates a deep clone of the WikiContext. This is useful when you want to be sure that you don't accidentally mess with page 666 * attributes, etc. 667 * 668 * @since 2.8.0 669 * @return A deep clone of the WikiContext. 670 */ 671 @Override 672 @SuppressWarnings("unchecked") 673 public WikiContext deepClone() { 674 try { 675 // super.clone() must always be called to make sure that inherited objects 676 // get the right type 677 final WikiContext copy = (WikiContext)super.clone(); 678 679 // No need to deep clone these 680 copy.m_engine = m_engine; 681 copy.m_command = m_command; // Static structure 682 683 copy.m_template = m_template; 684 copy.m_variableMap = (HashMap<String,Object>)m_variableMap.clone(); 685 copy.m_request = m_request; 686 copy.m_session = m_session; 687 copy.m_page = m_page.clone(); 688 copy.m_realPage = m_realPage.clone(); 689 return copy; 690 } 691 catch( final CloneNotSupportedException e ){ 692 LOG.debug(e.getMessage(), e); 693 // Never happens 694 } 695 696 return null; 697 } 698 699 /** 700 * Returns the Session associated with the context. This method is guaranteed to always return a valid Session. 701 * If this context was constructed without an associated HttpServletRequest, it will return 702 * {@link org.apache.wiki.WikiSession#guestSession(Engine)}. 703 * 704 * @return The Session associated with this context. 705 */ 706 @Override 707 public WikiSession getWikiSession() { 708 return ( WikiSession )m_session; 709 } 710 711 /** 712 * This method can be used to find the WikiContext programmatically from a JSP PageContext. We check the request context. 713 * The wiki context, if it exists, is looked up using the key {@link #ATTR_CONTEXT}. 714 * 715 * @since 2.4 716 * @param pageContext the JSP page context 717 * @return Current WikiContext, or null, of no context exists. 718 * @deprecated use {@link Context#findContext( PageContext )} instead. 719 * @see Context#findContext( PageContext ) 720 */ 721 @Deprecated 722 public static WikiContext findContext( final PageContext pageContext ) { 723 final HttpServletRequest request = ( HttpServletRequest )pageContext.getRequest(); 724 return ( WikiContext )request.getAttribute( ATTR_CONTEXT ); 725 } 726 727 /** 728 * Returns the permission required to successfully execute this context. For example, a wiki context of VIEW for a certain page 729 * means that the PagePermission "view" is required for the page. In some cases, no particular permission is required, in which case 730 * a dummy permission will be returned ({@link java.util.PropertyPermission}<code> "os.name", "read"</code>). This method is guaranteed 731 * to always return a valid, non-null permission. 732 * 733 * @return the permission 734 * @since 2.4 735 */ 736 @Override 737 public Permission requiredPermission() { 738 // This is a filthy rotten hack -- absolutely putrid 739 if ( WikiCommand.INSTALL.equals( m_command ) ) { 740 // See if admin users exists 741 try { 742 final UserManager userMgr = m_engine.getManager( UserManager.class ); 743 final UserDatabase userDb = userMgr.getUserDatabase(); 744 userDb.findByLoginName( Installer.ADMIN_ID ); 745 } catch ( final NoSuchPrincipalException e ) { 746 return DUMMY_PERMISSION; 747 } 748 return new AllPermission( m_engine.getApplicationName() ); 749 } 750 751 // TODO: we should really break the contract so that this 752 // method returns null, but until then we will use this hack 753 if( m_command.requiredPermission() == null ) { 754 return DUMMY_PERMISSION; 755 } 756 757 return m_command.requiredPermission(); 758 } 759 760 /** 761 * Associates a target with the current Command and returns the new targeted Command. If the Command associated with this 762 * WikiContext is already "targeted", it is returned instead. 763 * 764 * @see org.apache.wiki.api.core.Command#targetedCommand(java.lang.Object) 765 * 766 * {@inheritDoc} 767 */ 768 @Override 769 public Command targetedCommand( final Object target ) { 770 if ( m_command.getTarget() == null ) { 771 return m_command.targetedCommand( target ); 772 } 773 return m_command; 774 } 775 776 /** 777 * Returns true, if the current user has administrative permissions (i.e. the omnipotent AllPermission). 778 * 779 * @since 2.4.46 780 * @return true, if the user has all permissions. 781 */ 782 @Override 783 public boolean hasAdminPermissions() { 784 return m_engine.getManager( AuthorizationManager.class ).checkPermission( getWikiSession(), new AllPermission( m_engine.getApplicationName() ) ); 785 } 786 787 /** 788 * Figures out which template a new WikiContext should be using. 789 * @param request the HTTP request 790 */ 791 protected void setDefaultTemplate( final HttpServletRequest request ) { 792 final String defaultTemplate = m_engine.getTemplateDir(); 793 794 // Figure out which template we should be using for this page. 795 String template = null; 796 if ( request != null ) { 797 final String skin = request.getParameter( "skin" ); 798 if( skin != null ) 799 { 800 template = skin.replaceAll("\\p{Punct}", ""); 801 } 802 803 } 804 805 // If request doesn't supply the value, extract from wiki page 806 if( template == null ) { 807 final WikiPage page = getPage(); 808 if ( page != null ) { 809 template = page.getAttribute( Engine.PROP_TEMPLATEDIR ); 810 } 811 812 } 813 814 // If something over-wrote the default, set the new value. 815 if ( template != null ) { 816 setTemplate( template ); 817 } else { 818 setTemplate( defaultTemplate ); 819 } 820 } 821 822 /** 823 * Looks up and returns a PageCommand based on a supplied WikiPage and HTTP request. First, the appropriate Command is obtained by 824 * examining the HTTP request; the default is {@link ContextEnum#PAGE_VIEW}. If the Command is a PageCommand (and it should be, in most 825 * cases), a targeted Command is created using the (non-<code>null</code>) WikiPage as target. 826 * 827 * @param engine the wiki engine 828 * @param request the HTTP request 829 * @param page the wiki page 830 * @return the correct command 831 */ 832 protected static Command findCommand( final Engine engine, final HttpServletRequest request, final Page page ) { 833 final String defaultContext = ContextEnum.PAGE_VIEW.getRequestContext(); 834 Command command = engine.getManager( CommandResolver.class ).findCommand( request, defaultContext ); 835 if ( command instanceof PageCommand && page != null ) { 836 command = command.targetedCommand( page ); 837 } 838 return command; 839 } 840 841 /** 842 * Protected method that updates the internally cached Command. Will always be called when the page name, request context, or variable 843 * changes. 844 * 845 * @param requestContext the desired request context 846 * @since 2.4 847 */ 848 protected void updateCommand( final String requestContext ) { 849 if ( requestContext == null ) { 850 m_command = PageCommand.NONE; 851 } else { 852 final CommandResolver resolver = m_engine.getManager( CommandResolver.class ); 853 m_command = resolver.findCommand( m_request, requestContext ); 854 } 855 856 if ( m_command instanceof PageCommand && m_page != null ) { 857 m_command = m_command.targetedCommand( m_page ); 858 } 859 } 860 861}