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.commons.lang3.StringUtils; 022import org.apache.logging.log4j.LogManager; 023import org.apache.logging.log4j.Logger; 024import org.apache.wiki.api.Release; 025import org.apache.wiki.api.core.Engine; 026import org.apache.wiki.api.core.Page; 027import org.apache.wiki.api.engine.Initializable; 028import org.apache.wiki.api.exceptions.ProviderException; 029import org.apache.wiki.api.exceptions.WikiException; 030import org.apache.wiki.attachment.AttachmentManager; 031import org.apache.wiki.auth.AuthenticationManager; 032import org.apache.wiki.auth.AuthorizationManager; 033import org.apache.wiki.auth.UserManager; 034import org.apache.wiki.auth.acl.AclManager; 035import org.apache.wiki.auth.authorize.GroupManager; 036import org.apache.wiki.cache.CachingManager; 037import org.apache.wiki.content.PageRenamer; 038import org.apache.wiki.diff.DifferenceManager; 039import org.apache.wiki.event.WikiEngineEvent; 040import org.apache.wiki.event.WikiEventListener; 041import org.apache.wiki.event.WikiEventManager; 042import org.apache.wiki.event.WikiPageEvent; 043import org.apache.wiki.filters.FilterManager; 044import org.apache.wiki.i18n.InternationalizationManager; 045import org.apache.wiki.notification.SubscriptionManager; 046import org.apache.wiki.pages.PageManager; 047import org.apache.wiki.plugin.PluginManager; 048import org.apache.wiki.references.ReferenceManager; 049import org.apache.wiki.render.RenderingManager; 050import org.apache.wiki.rss.RSSGenerator; 051import org.apache.wiki.search.SearchManager; 052import org.apache.wiki.tasks.TasksManager; 053import org.apache.wiki.ui.CommandResolver; 054import org.apache.wiki.ui.EditorManager; 055import org.apache.wiki.ui.TemplateManager; 056import org.apache.wiki.ui.admin.AdminBeanManager; 057import org.apache.wiki.ui.progress.ProgressManager; 058import org.apache.wiki.url.URLConstructor; 059import org.apache.wiki.util.ClassUtil; 060import org.apache.wiki.util.PropertyReader; 061import org.apache.wiki.util.Synchronizer; 062import org.apache.wiki.util.TextUtil; 063import org.apache.wiki.variables.VariableManager; 064import org.apache.wiki.workflow.WorkflowManager; 065 066import jakarta.servlet.ServletConfig; 067import jakarta.servlet.ServletContext; 068import java.io.File; 069import java.io.UnsupportedEncodingException; 070import java.net.MalformedURLException; 071import java.net.URL; 072import java.net.URLDecoder; 073import java.net.URLEncoder; 074import java.nio.charset.Charset; 075import java.nio.charset.StandardCharsets; 076import java.util.ArrayList; 077import java.util.Collection; 078import java.util.Date; 079import java.util.Enumeration; 080import java.util.List; 081import java.util.Locale; 082import java.util.Map; 083import java.util.Properties; 084import java.util.TimeZone; 085import java.util.concurrent.ConcurrentHashMap; 086import java.util.concurrent.atomic.AtomicReference; 087import java.util.concurrent.locks.ReentrantLock; 088import java.util.stream.Collectors; 089import org.apache.wiki.auth.SecurityVerificationUtility; 090import org.apache.wiki.api.core.Context; 091import org.apache.wiki.security.AuditLogger; 092 093 094/** 095 * Main implementation for {@link Engine}. 096 * 097 * <P> 098 * Using this class: Always get yourself an instance from JSP page by using the {@code WikiEngine.getInstance(..)} method. Never create 099 * a new WikiEngine() from scratch, unless you're writing tests. 100 * 101 * <p> 102 * {@inheritDoc} 103 */ 104public class WikiEngine implements Engine { 105 106 private static final String ATTR_WIKIENGINE = "org.apache.wiki.WikiEngine"; 107 private static final Logger LOG = LogManager.getLogger( WikiEngine.class ); 108 109 /** Stores properties. */ 110 private Properties m_properties; 111 112 /** Should the user info be saved with the page data as well? */ 113 private boolean m_saveUserInfo = true; 114 115 /** If true, uses UTF8 encoding for all data */ 116 private boolean m_useUTF8 = true; 117 118 /** Store the file path to the basic URL. When we're not running as a servlet, it defaults to the user's current directory. */ 119 private String m_rootPath = System.getProperty( "user.dir" ); 120 121 /** Store the ServletContext that we're in. This may be null if WikiEngine is not running inside a servlet container (i.e. when testing). */ 122 private ServletContext m_servletContext; 123 124 /** Stores the template path. This is relative to "templates". */ 125 private String m_templateDir; 126 127 /** The default front page name. Defaults to "Main". */ 128 private String m_frontPage; 129 130 /** The time when this engine was started. */ 131 private Date m_startTime; 132 133 /** The location where the work directory is. */ 134 private String m_workDir; 135 136 /** Each engine has their own application id. */ 137 private String m_appid = ""; 138 139 /** engine is up and running or not */ 140 private boolean m_isConfigured; 141 142 /** Stores wikiengine attributes. */ 143 private final Map< String, Object > m_attributes = new ConcurrentHashMap<>(); 144 145 /** Stores WikiEngine's associated managers. */ 146 protected final Map< Class< ? >, Object > managers = new ConcurrentHashMap<>(); 147 148 /** 149 * A lock used to ensure thread safety when accessing shared resources. 150 * This lock provides more flexibility and capabilities than the intrinsic locking mechanism, 151 * such as the ability to attempt to acquire a lock with a timeout, or to interrupt a thread 152 * waiting to acquire a lock. 153 * 154 * @see java.util.concurrent.locks.ReentrantLock 155 */ 156 private static final ReentrantLock lock = new ReentrantLock(); 157 158 /** 159 * Gets a WikiEngine related to this servlet. Since this method is only called from JSP pages (and JspInit()) to be specific, 160 * we throw a RuntimeException if things don't work. 161 * 162 * @param config The ServletConfig object for this servlet. 163 * @return A WikiEngine instance. 164 * @throws InternalWikiException in case something fails. This is a RuntimeException, so be prepared for it. 165 */ 166 public static WikiEngine getInstance( final ServletConfig config ) throws InternalWikiException { 167 return getInstance( config.getServletContext(), null ); 168 } 169 170 /** 171 * Gets a WikiEngine related to the servlet. Works like getInstance(ServletConfig), but does not force the Properties object. 172 * This method is just an optional way of initializing a WikiEngine for embedded JSPWiki applications; normally, you 173 * should use getInstance(ServletConfig). 174 * 175 * @param config The ServletConfig of the webapp servlet/JSP calling this method. 176 * @param props A set of properties, or null, if we are to load JSPWiki's default jspwiki.properties (this is the usual case). 177 * 178 * @return One well-behaving WikiEngine instance. 179 */ 180 public static WikiEngine getInstance( final ServletConfig config, final Properties props ) { 181 return getInstance( config.getServletContext(), props ); 182 } 183 184 /** 185 * Gets a WikiEngine related to the servlet. Works just like getInstance( ServletConfig ) 186 * 187 * @param context The ServletContext of the webapp servlet/JSP calling this method. 188 * @param props A set of properties, or null, if we are to load JSPWiki's default jspwiki.properties (this is the usual case). 189 * @return One fully functional, properly behaving WikiEngine. 190 * @throws InternalWikiException If the WikiEngine instantiation fails. 191 */ 192 public static WikiEngine getInstance(final ServletContext context, Properties props) throws InternalWikiException { 193 final AtomicReference<Properties> propsRef = new AtomicReference<>(props); 194 return Synchronizer.synchronize( lock, () -> { 195 WikiEngine engine = (WikiEngine) context.getAttribute(ATTR_WIKIENGINE); 196 if (engine == null) { 197 final String appid = Integer.toString(context.hashCode()); 198 context.log(" Assigning new engine to " + appid); 199 try { 200 if (propsRef.get() == null) { 201 propsRef.set(PropertyReader.loadWebAppProps(context)); 202 } 203 204 engine = new WikiEngine(context, appid); 205 try { 206 // Note: May be null, if JSPWiki has been deployed in a WAR file. 207 engine.start(propsRef.get()); 208 LOG.info("Root path for this Wiki is: '{}'", engine.getRootPath()); 209 } catch (final Exception e) { 210 final String msg = Release.APPNAME + ": Unable to load and setup properties from jspwiki.properties. " + e.getMessage(); 211 context.log(msg); 212 LOG.error(msg, e); 213 throw new WikiException(msg, e); 214 } 215 context.setAttribute(ATTR_WIKIENGINE, engine); 216 } catch (final Exception e) { 217 context.log("ERROR: Failed to create a Wiki engine: " + e.getMessage()); 218 LOG.error("ERROR: Failed to create a Wiki engine, stacktrace follows ", e); 219 throw new InternalWikiException("No wiki engine, check logs.", e); 220 } 221 } 222 return engine; 223 } ); 224 } 225 226 /** 227 * Instantiate the WikiEngine using a given set of properties. Use this constructor for testing purposes only. 228 * 229 * @param properties A set of properties to use to initialize this WikiEngine. 230 * @throws WikiException If the initialization fails. 231 */ 232 public WikiEngine( final Properties properties ) throws WikiException { 233 start( properties ); 234 } 235 236 /** 237 * Instantiate using this method when you're running as a servlet and WikiEngine will figure out where to look for the property file. 238 * Do not use this method - use WikiEngine.getInstance() instead. 239 * 240 * @param context A ServletContext. 241 * @param appid An Application ID. This application is a unique random string which is used to recognize this WikiEngine. 242 * @throws WikiException If the WikiEngine construction fails. 243 */ 244 protected WikiEngine( final ServletContext context, final String appid ) throws WikiException { 245 m_servletContext = context; 246 m_appid = appid; 247 248 // Stash the WikiEngine in the servlet context 249 if ( context != null ) { 250 context.setAttribute( ATTR_WIKIENGINE, this ); 251 m_rootPath = context.getRealPath( "/" ); 252 } 253 } 254 255 /** 256 * Does all the real initialization. 257 */ 258 @Override 259 public void initialize( final Properties props ) throws WikiException { 260 m_startTime = new Date(); 261 262 m_properties = props; 263 264 LOG.info( "*******************************************" ); 265 LOG.info( "{} {} starting. Whee!", Release.APPNAME, Release.getVersionString() ); 266 LOG.debug( "Java version: {}", System.getProperty( "java.runtime.version" ) ); 267 LOG.debug( "Java vendor: {}", System.getProperty( "java.vm.vendor" ) ); 268 LOG.debug( "OS: {} {} {}", System.getProperty( "os.name" ), System.getProperty( "os.version" ), System.getProperty( "os.arch" ) ); 269 LOG.debug( "Default server locale: {}", Locale.getDefault() ); 270 LOG.debug( "Default server timezone: {}", TimeZone.getDefault().getDisplayName( true, TimeZone.LONG ) ); 271 272 if( m_servletContext != null ) { 273 LOG.info( "Servlet container: {}", m_servletContext.getServerInfo() ); 274 if( m_servletContext.getMajorVersion() < 3 || ( m_servletContext.getMajorVersion() == 3 && m_servletContext.getMinorVersion() < 1 ) ) { 275 throw new InternalWikiException( "JSPWiki requires a container which supports at least version 3.1 of Servlet specification" ); 276 } 277 } 278 AuditLogger.initialize(this); 279 fireEvent( WikiEngineEvent.INITIALIZING ); // begin initialization 280 281 LOG.debug( "Configuring WikiEngine..." ); 282 283 createAndFindWorkingDirectory( props ); 284 285 m_useUTF8 = StandardCharsets.UTF_8.name().equals( TextUtil.getStringProperty( props, PROP_ENCODING, StandardCharsets.ISO_8859_1.name() ) ); 286 m_saveUserInfo = TextUtil.getBooleanProperty( props, PROP_STOREUSERNAME, m_saveUserInfo ); 287 m_frontPage = TextUtil.getStringProperty( props, PROP_FRONTPAGE, "Main" ); 288 m_templateDir = TextUtil.getStringProperty( props, PROP_TEMPLATEDIR, "default" ); 289 enforceValidTemplateDirectory(); 290 291 // 292 // Initialize the important modules. Any exception thrown by the managers means that we will not start up. 293 // 294 try { 295 final String aclClassName = m_properties.getProperty( PROP_ACL_MANAGER_IMPL, ClassUtil.getMappedClass( AclManager.class.getName() ).getName() ); 296 final String urlConstructorClassName = TextUtil.getStringProperty( props, PROP_URLCONSTRUCTOR, "DefaultURLConstructor" ); 297 final Class< URLConstructor > urlclass = ClassUtil.findClass( "org.apache.wiki.url", urlConstructorClassName ); 298 299 initComponent( CommandResolver.class, this, props ); 300 initComponent( urlclass.getName(), URLConstructor.class ); 301 initComponent( CachingManager.class, this, props ); 302 initComponent( PageManager.class, this, props ); 303 initComponent( PluginManager.class, this, props ); 304 initComponent( DifferenceManager.class, this, props ); 305 initComponent( AttachmentManager.class, this, props ); 306 initComponent( VariableManager.class, props ); 307 initComponent( SearchManager.class, this, props ); 308 initComponent( AuthenticationManager.class ); 309 initComponent( AuthorizationManager.class ); 310 initComponent( UserManager.class ); 311 initComponent( GroupManager.class ); 312 initComponent( EditorManager.class, this ); 313 initComponent( ProgressManager.class, this ); 314 initComponent( aclClassName, AclManager.class ); 315 initComponent( WorkflowManager.class ); 316 initComponent( TasksManager.class ); 317 initComponent( InternationalizationManager.class, this ); 318 initComponent( TemplateManager.class, this, props ); 319 initComponent( FilterManager.class, this, props ); 320 initComponent( AdminBeanManager.class, this ); 321 initComponent( PageRenamer.class, this, props ); 322 initComponent( SubscriptionManager.class ); 323 324 // RenderingManager depends on FilterManager events. 325 initComponent( RenderingManager.class ); 326 327 // ReferenceManager has the side effect of loading all pages. Therefore, after this point, all page attributes are available. 328 // initReferenceManager is indirectly using m_filterManager, so it has to be called after it was initialized. 329 initReferenceManager(); 330 331 // Hook the different manager routines into the system. 332 getManager( FilterManager.class ).addPageFilter( getManager( ReferenceManager.class ), -1001 ); 333 getManager( FilterManager.class ).addPageFilter( getManager( SearchManager.class ), -1002 ); 334 } catch( final RuntimeException e ) { 335 // RuntimeExceptions may occur here, even if they shouldn't. 336 LOG.fatal( "Failed to start managers.", e ); 337 throw new WikiException( "Failed to start managers: " + e.getMessage(), e ); 338 } catch( final ClassNotFoundException e ) { 339 LOG.fatal( "JSPWiki could not start, URLConstructor was not found: {}", e.getMessage(), e ); 340 throw new WikiException( e.getMessage(), e ); 341 } catch( final InstantiationException e ) { 342 LOG.fatal( "JSPWiki could not start, URLConstructor could not be instantiated: {}", e.getMessage(), e ); 343 throw new WikiException( e.getMessage(), e ); 344 } catch( final IllegalAccessException e ) { 345 LOG.fatal( "JSPWiki could not start, URLConstructor cannot be accessed: {}", e.getMessage(), e ); 346 throw new WikiException( e.getMessage(), e ); 347 } catch( final Exception e ) { 348 // Final catch-all for everything 349 LOG.fatal( "JSPWiki could not start, due to an unknown exception when starting.", e ); 350 throw new WikiException( "Failed to start. Caused by: " + e.getMessage() + "; please check log files for better information.", e ); 351 } 352 353 // Initialize the good-to-have-but-not-fatal modules. 354 try { 355 if( TextUtil.getBooleanProperty( props, RSSGenerator.PROP_GENERATE_RSS,false ) ) { 356 initComponent( RSSGenerator.class, this, props ); 357 } 358 } catch( final Exception e ) { 359 LOG.error( "Unable to start RSS generator - JSPWiki will still work, but there will be no RSS feed.", e ); 360 } 361 362 final Map< String, String > extraComponents = ClassUtil.getExtraClassMappings(); 363 initExtraComponents( extraComponents ); 364 365 366 ProductUpdateChecker.initialize(props); 367 368 fireEvent( WikiEngineEvent.INITIALIZED ); // initialization complete 369 370 LOG.info( "WikiEngine configured." ); 371 372 new SecurityVerificationUtility().verify(this); 373 m_isConfigured = true; 374 } 375 376 void createAndFindWorkingDirectory( final Properties props ) throws WikiException { 377 m_workDir = TextUtil.getStringProperty( props, PROP_WORKDIR, null ); 378 379 final File f = new File( m_workDir ); 380 try { 381 f.mkdirs(); 382 } catch( final SecurityException e ) { 383 LOG.fatal( "Unable to find or create the working directory: {}", m_workDir, e ); 384 throw new WikiException( "Unable to find or create the working dir: " + m_workDir, e ); 385 } 386 387 // A bunch of sanity checks 388 checkWorkingDirectory( !f.exists(), "Work directory does not exist: " + m_workDir ); 389 checkWorkingDirectory( !f.canRead(), "No permission to read work directory: " + m_workDir ); 390 checkWorkingDirectory( !f.canWrite(), "No permission to write to work directory: " + m_workDir ); 391 checkWorkingDirectory( !f.isDirectory(), "jspwiki.workDir does not point to a directory: " + m_workDir ); 392 393 LOG.info( "JSPWiki working directory is '{}'", m_workDir ); 394 } 395 396 void checkWorkingDirectory( final boolean condition, final String errMsg ) throws WikiException { 397 if( condition ) { 398 throw new WikiException( errMsg ); 399 } 400 } 401 402 void initExtraComponents( final Map< String, String > extraComponents ) { 403 for( final Map.Entry< String, String > extraComponent : extraComponents.entrySet() ) { 404 try { 405 LOG.info( "Registering on WikiEngine {} as {}", extraComponent.getKey(), extraComponent.getValue() ); 406 initComponent( extraComponent.getKey(), Class.forName( extraComponent.getValue() ) ); 407 } catch( final Exception e ) { 408 LOG.error( "Unable to start {}", extraComponent.getKey(), e ); 409 } 410 } 411 } 412 413 < T > void initComponent( final Class< T > componentClass, final Object... initArgs ) throws Exception { 414 initComponent( componentClass.getName(), componentClass, initArgs ); 415 } 416 417 < T > void initComponent( final String componentInitClass, final Class< T > componentClass, final Object... initArgs ) throws Exception { 418 final T component; 419 if( initArgs == null || initArgs.length == 0 ) { 420 component = ClassUtil.getMappedObject( componentInitClass ); 421 } else { 422 component = ClassUtil.getMappedObject( componentInitClass, initArgs ); 423 } 424 managers.put( componentClass, component ); 425 if( Initializable.class.isAssignableFrom( component.getClass() ) ) { 426 ( ( Initializable )component ).initialize( this, m_properties ); 427 } 428 } 429 430 /** {@inheritDoc} */ 431 @Override 432 @SuppressWarnings( "unchecked" ) 433 public < T > T getManager( final Class< T > manager ) { 434 return ( T )managers.entrySet().stream() 435 .filter( e -> manager.isAssignableFrom( e.getKey() ) ) 436 .map( Map.Entry::getValue ) 437 .findFirst().orElse( null ); 438 } 439 440 /** {@inheritDoc} */ 441 @Override 442 @SuppressWarnings( "unchecked" ) 443 public < T > List< T > getManagers( final Class< T > manager ) { 444 return ( List< T > )managers.entrySet().stream() 445 .filter( e -> manager.isAssignableFrom( e.getKey() ) ) 446 .map( Map.Entry::getValue ) 447 .collect( Collectors.toList() ); 448 } 449 450 /** {@inheritDoc} */ 451 @Override 452 public boolean isConfigured() { 453 return m_isConfigured; 454 } 455 456 /** 457 * Checks if the template directory specified in the wiki's properties actually exists. If it doesn't, then {@code m_templateDir} is 458 * set to {@link #DEFAULT_TEMPLATE_NAME}. 459 * <p> 460 * This checks the existence of the <tt>ViewTemplate.jsp</tt> file, which exists in every template using {@code m_servletContext.getRealPath("/")}. 461 * <p> 462 * {@code m_servletContext.getRealPath("/")} can return {@code null} on certain servers/conditions (f.ex, packed wars), an extra check 463 * against {@code m_servletContext.getResource} is made. 464 */ 465 void enforceValidTemplateDirectory() { 466 if( m_servletContext != null ) { 467 final String viewTemplate = "templates" + File.separator + getTemplateDir() + File.separator + "ViewTemplate.jsp"; 468 boolean exists = new File( m_servletContext.getRealPath( "/" ) + viewTemplate ).exists(); 469 if( !exists ) { 470 try { 471 final URL url = m_servletContext.getResource( viewTemplate ); 472 exists = url != null && StringUtils.isNotEmpty( url.getFile() ); 473 } catch( final MalformedURLException e ) { 474 LOG.warn( "template not found with viewTemplate {}", viewTemplate ); 475 } 476 } 477 if( !exists ) { 478 LOG.warn( "{} template not found, updating WikiEngine's default template to {}", getTemplateDir(), DEFAULT_TEMPLATE_NAME ); 479 m_templateDir = DEFAULT_TEMPLATE_NAME; 480 } 481 } 482 } 483 484 /** 485 * Initializes the reference manager. Scans all existing WikiPages for 486 * internal links and adds them to the ReferenceManager object. 487 * 488 * @throws WikiException If the reference manager initialization fails. 489 */ 490 public void initReferenceManager() throws WikiException { 491 try { 492 // Build a new manager with default key lists. 493 if( getManager( ReferenceManager.class ) == null ) { 494 final ArrayList< Page > pages = new ArrayList<>(); 495 pages.addAll( getManager( PageManager.class ).getAllPages() ); 496 pages.addAll( getManager( AttachmentManager.class ).getAllAttachments() ); 497 final String refMgrClassName = m_properties.getProperty( PROP_REF_MANAGER_IMPL, ClassUtil.getMappedClass( ReferenceManager.class.getName() ).getName() ); 498 499 initComponent( refMgrClassName, ReferenceManager.class, this ); 500 501 getManager( ReferenceManager.class ).initialize( pages ); 502 } 503 504 } catch( final ProviderException e ) { 505 LOG.fatal( "PageProvider is unable to list pages: ", e ); 506 } catch( final Exception e ) { 507 throw new WikiException( "Could not instantiate ReferenceManager: " + e.getMessage(), e ); 508 } 509 } 510 511 /** {@inheritDoc} */ 512 @Override 513 public Properties getWikiProperties() { 514 return m_properties; 515 } 516 517 /** {@inheritDoc} */ 518 @Override 519 public String getWorkDir() { 520 return m_workDir; 521 } 522 523 /** {@inheritDoc} */ 524 @Override 525 public String getTemplateDir() { 526 return m_templateDir; 527 } 528 529 /** {@inheritDoc} */ 530 @Override 531 public Date getStartTime() { 532 return ( Date )m_startTime.clone(); 533 } 534 535 /** {@inheritDoc} */ 536 @Override 537 public String getBaseURL() { 538 return m_servletContext.getContextPath(); 539 } 540 541 /** {@inheritDoc} */ 542 @Override 543 public String getGlobalRSSURL() { 544 final RSSGenerator rssGenerator = getManager( RSSGenerator.class ); 545 if( rssGenerator != null && rssGenerator.isEnabled() ) { 546 return getBaseURL() + "/" + rssGenerator.getRssFile(); 547 } 548 549 return null; 550 } 551 552 /** {@inheritDoc} */ 553 @Override 554 public String getInterWikiURL( final String wikiName ) { 555 return TextUtil.getStringProperty( m_properties,PROP_INTERWIKIREF + wikiName,null ); 556 } 557 558 /** {@inheritDoc} */ 559 @Override 560 public String getURL( final String context, String pageName, final String params ) { 561 if( pageName == null ) { 562 pageName = getFrontPage(); 563 } 564 final URLConstructor urlConstructor = getManager( URLConstructor.class ); 565 return urlConstructor.makeURL( context, pageName, params ); 566 } 567 568 /** {@inheritDoc} */ 569 @Override 570 public String getFrontPage() { 571 return m_frontPage; 572 } 573 574 /** {@inheritDoc} */ 575 @Override 576 public ServletContext getServletContext() { 577 return m_servletContext; 578 } 579 580 /** {@inheritDoc} */ 581 @Override 582 public Collection< String > getAllInterWikiLinks() { 583 final ArrayList< String > list = new ArrayList<>(); 584 for( final Enumeration< ? > i = m_properties.propertyNames(); i.hasMoreElements(); ) { 585 final String prop = ( String )i.nextElement(); 586 if( prop.startsWith( PROP_INTERWIKIREF ) ) { 587 list.add( prop.substring( prop.lastIndexOf( "." ) + 1 ) ); 588 } 589 } 590 591 return list; 592 } 593 594 /** {@inheritDoc} */ 595 @Override 596 public Collection< String > getAllInlinedImagePatterns() { 597 final ArrayList< String > ptrnlist = new ArrayList<>(); 598 for( final Enumeration< ? > e = m_properties.propertyNames(); e.hasMoreElements(); ) { 599 final String name = ( String )e.nextElement(); 600 if( name.startsWith( PROP_INLINEIMAGEPTRN ) ) { 601 ptrnlist.add( TextUtil.getStringProperty( m_properties, name, null ) ); 602 } 603 } 604 605 if( ptrnlist.isEmpty() ) { 606 ptrnlist.add( DEFAULT_INLINEPATTERN ); 607 } 608 609 return ptrnlist; 610 } 611 612 /** {@inheritDoc} */ 613 @Override 614 public String getSpecialPageReference( final String original ) { 615 return getManager( CommandResolver.class ).getSpecialPageReference( original ); 616 } 617 618 /** {@inheritDoc} */ 619 @Override 620 public String getApplicationName() { 621 final String appName = TextUtil.getStringProperty( m_properties, PROP_APPNAME, Release.APPNAME ); 622 return TextUtil.cleanString( appName, TextUtil.PUNCTUATION_CHARS_ALLOWED ); 623 } 624 625 /** {@inheritDoc} */ 626 @Override 627 public String getFinalPageName( final String page ) throws ProviderException { 628 return getManager( CommandResolver.class ).getFinalPageName( page ); 629 } 630 631 /** {@inheritDoc} */ 632 @Override 633 public String encodeName( final String pagename ) { 634 try { 635 return URLEncoder.encode( pagename, m_useUTF8 ? StandardCharsets.UTF_8.name() : StandardCharsets.ISO_8859_1.name() ); 636 } catch( final UnsupportedEncodingException e ) { 637 throw new InternalWikiException( "ISO-8859-1 not a supported encoding!?! Your platform is borked." , e); 638 } 639 } 640 641 /** {@inheritDoc} */ 642 @Override 643 public String decodeName( final String pagerequest ) { 644 try { 645 return URLDecoder.decode( pagerequest, m_useUTF8 ? StandardCharsets.UTF_8.name() : StandardCharsets.ISO_8859_1.name() ); 646 } catch( final UnsupportedEncodingException e ) { 647 throw new InternalWikiException("ISO-8859-1 not a supported encoding!?! Your platform is borked.", e); 648 } 649 } 650 651 /** {@inheritDoc} */ 652 @Override 653 public Charset getContentEncoding() { 654 if( m_useUTF8 ) { 655 return StandardCharsets.UTF_8; 656 } 657 return StandardCharsets.ISO_8859_1; 658 } 659 660 /** 661 * {@inheritDoc} 662 * <p>It is called by {@link WikiServlet#destroy()}. When this method is called, it fires a "shutdown" WikiEngineEvent to 663 * all registered listeners. 664 */ 665 @Override 666 public void shutdown() { 667 fireEvent( WikiEngineEvent.SHUTDOWN ); 668 getManager( CachingManager.class ).shutdown(); 669 getManager( FilterManager.class ).destroy(); 670 if (ProductUpdateChecker.getInstance() != null) { 671 ProductUpdateChecker.getInstance().shutdown(); 672 } 673 WikiEventManager.shutdown(); 674 } 675 676 /** 677 * Returns the current TemplateManager. 678 * 679 * @return A TemplateManager instance. 680 * @deprecated use {@code getManager( TemplateManager.class )} instead. 681 */ 682 @Deprecated 683 public TemplateManager getTemplateManager() { 684 return getManager( TemplateManager.class ); 685 } 686 687 /** 688 * Returns the {@link org.apache.wiki.workflow.WorkflowManager} associated with this WikiEngine. If the WikiEngine has not been 689 * initialized, this method will return <code>null</code>. 690 * 691 * @return the task queue 692 * @deprecated use {@code getManager( WorkflowManager.class )} instead. 693 */ 694 @Deprecated 695 public WorkflowManager getWorkflowManager() { 696 return getManager( WorkflowManager.class ); 697 } 698 699 /** 700 * Returns this object's ReferenceManager. 701 * 702 * @return The current ReferenceManager instance. 703 * @since 1.6.1 704 * @deprecated use {@code getManager( ReferenceManager.class )} instead. 705 */ 706 @Deprecated 707 public ReferenceManager getReferenceManager() { 708 return getManager( ReferenceManager.class ); 709 } 710 711 /** 712 * Returns the current rendering manager for this wiki application. 713 * 714 * @since 2.3.27 715 * @return A RenderingManager object. 716 * @deprecated use {@code getManager( RenderingManager.class )} instead. 717 */ 718 @Deprecated 719 public RenderingManager getRenderingManager() { 720 return getManager( RenderingManager.class ); 721 } 722 723 /** 724 * Returns the current plugin manager. 725 * 726 * @since 1.6.1 727 * @return The current PluginManager instance 728 * @deprecated use {@code getManager( PluginManager.class )} instead. 729 */ 730 @Deprecated 731 public PluginManager getPluginManager() { 732 return getManager( PluginManager.class ); 733 } 734 735 /** 736 * Returns the current variable manager. 737 * 738 * @return The current VariableManager. 739 * @deprecated use {@code getManager( VariableManager.class )} instead. 740 */ 741 @Deprecated 742 public VariableManager getVariableManager() { 743 return getManager( VariableManager.class ); 744 } 745 746 /** 747 * Returns the current PageManager which is responsible for storing and managing WikiPages. 748 * 749 * @return The current PageManager instance. 750 * @deprecated use {@code getManager( PageManager.class )} instead. 751 */ 752 @Deprecated 753 public PageManager getPageManager() { 754 return getManager( PageManager.class ); 755 } 756 757 /** 758 * Returns the CommandResolver for this wiki engine. 759 * 760 * @return the resolver 761 * @deprecated use {@code getManager( CommandResolver.class )} instead. 762 */ 763 @Deprecated 764 public CommandResolver getCommandResolver() { 765 return getManager( CommandResolver.class ); 766 } 767 768 /** 769 * Returns the current AttachmentManager, which is responsible for storing and managing attachments. 770 * 771 * @since 1.9.31. 772 * @return The current AttachmentManager instance 773 * @deprecated use {@code getManager( AttachmentManager.class )} instead. 774 */ 775 @Deprecated 776 public AttachmentManager getAttachmentManager() { 777 return getManager( AttachmentManager.class ); 778 } 779 780 /** 781 * Returns the currently used authorization manager. 782 * 783 * @return The current AuthorizationManager instance. 784 * @deprecated use {@code getManager( AuthorizationManager.class )} instead. 785 */ 786 @Deprecated 787 public AuthorizationManager getAuthorizationManager() { 788 return getManager( AuthorizationManager.class ); 789 } 790 791 /** 792 * Returns the currently used authentication manager. 793 * 794 * @return The current AuthenticationManager instance. 795 * @deprecated use {@code getManager( AuthenticationManager.class )} instead. 796 */ 797 @Deprecated 798 public AuthenticationManager getAuthenticationManager() { 799 return getManager( AuthenticationManager.class ); 800 } 801 802 /** 803 * Returns the manager responsible for the filters. 804 * 805 * @since 2.1.88 806 * @return The current FilterManager instance. 807 * @deprecated use {@code getManager( FilterManager.class )} instead. 808 */ 809 @Deprecated 810 public FilterManager getFilterManager() { 811 return getManager( FilterManager.class ); 812 } 813 814 /** 815 * Returns the manager responsible for searching the Wiki. 816 * 817 * @since 2.2.21 818 * @return The current SearchManager instance. 819 * @deprecated use {@code getManager( SearchManager.class )} instead. 820 */ 821 @Deprecated 822 public SearchManager getSearchManager() { 823 return getManager( SearchManager.class ); 824 } 825 826 /** 827 * Returns the progress manager we're using 828 * 829 * @return A ProgressManager. 830 * @since 2.6 831 * @deprecated use {@code getManager( ProgressManager.class )} instead. 832 */ 833 @Deprecated 834 public ProgressManager getProgressManager() { 835 return getManager( ProgressManager.class ); 836 } 837 838 /** {@inheritDoc} */ 839 @Override 840 public String getRootPath() { 841 return m_rootPath; 842 } 843 844 /** 845 * @since 2.2.6 846 * @return the URL constructor. 847 * @deprecated use {@code getManager( URLConstructor.class )} instead. 848 */ 849 @Deprecated 850 public URLConstructor getURLConstructor() { 851 return getManager( URLConstructor.class ); 852 } 853 854 /** 855 * Returns the RSSGenerator. If the property <code>jspwiki.rss.generate</code> has not been set to <code>true</code>, this method 856 * will return <code>null</code>, <em>and callers should check for this value.</em> 857 * 858 * @since 2.1.165 859 * @return the RSS generator 860 * @deprecated use {@code getManager( RSSGenerator.class )} instead. 861 */ 862 @Deprecated 863 public RSSGenerator getRSSGenerator() { 864 return getManager( RSSGenerator.class ); 865 } 866 867 /** 868 * Returns the PageRenamer employed by this WikiEngine. 869 * 870 * @since 2.5.141 871 * @return The current PageRenamer instance. 872 * @deprecated use {@code getManager( PageRenamer.class )} instead. 873 */ 874 @Deprecated 875 public PageRenamer getPageRenamer() { 876 return getManager( PageRenamer.class ); 877 } 878 879 /** 880 * Returns the UserManager employed by this WikiEngine. 881 * 882 * @since 2.3 883 * @return The current UserManager instance. 884 * @deprecated use {@code getManager( UserManager.class )} instead. 885 */ 886 @Deprecated 887 public UserManager getUserManager() { 888 return getManager( UserManager.class ); 889 } 890 891 /** 892 * Returns the TasksManager employed by this WikiEngine. 893 * 894 * @return The current TasksManager instance. 895 * @deprecated use {@code getManager( TaskManager.class )} instead. 896 */ 897 @Deprecated 898 public TasksManager getTasksManager() { 899 return getManager( TasksManager.class ); 900 } 901 902 /** 903 * Returns the GroupManager employed by this WikiEngine. 904 * 905 * @since 2.3 906 * @return The current GroupManager instance. 907 * @deprecated use {@code getManager( GroupManager.class )} instead. 908 */ 909 @Deprecated 910 public GroupManager getGroupManager() { 911 return getManager( GroupManager.class ); 912 } 913 914 /** 915 * Returns the current {@link AdminBeanManager}. 916 * 917 * @return The current {@link AdminBeanManager}. 918 * @since 2.6 919 * @deprecated use {@code getManager( AdminBeanManager.class )} instead. 920 */ 921 @Deprecated 922 public AdminBeanManager getAdminBeanManager() { 923 return getManager( AdminBeanManager.class ); 924 } 925 926 /** 927 * Returns the AclManager employed by this WikiEngine. The AclManager is lazily initialized. 928 * <p> 929 * The AclManager implementing class may be set by the System property {@link #PROP_ACL_MANAGER_IMPL}. 930 * </p> 931 * 932 * @since 2.3 933 * @return The current AclManager. 934 * @deprecated use {@code getManager( AclManager.class )} instead. 935 */ 936 @Deprecated 937 public AclManager getAclManager() { 938 return getManager( AclManager.class ); 939 } 940 941 /** 942 * Returns the DifferenceManager so that texts can be compared. 943 * 944 * @return the difference manager. 945 * @deprecated use {@code getManager( DifferenceManager.class )} instead. 946 */ 947 @Deprecated 948 public DifferenceManager getDifferenceManager() { 949 return getManager( DifferenceManager.class ); 950 } 951 952 /** 953 * Returns the current EditorManager instance. 954 * 955 * @return The current EditorManager. 956 * @deprecated use {@code getManager( EditorManager.class )} instead. 957 */ 958 @Deprecated 959 public EditorManager getEditorManager() { 960 return getManager( EditorManager.class ); 961 } 962 963 /** 964 * Returns the current i18n manager. 965 * 966 * @return The current Intertan... Interante... Internatatializ... Whatever. 967 * @deprecated use {@code getManager( InternationalizationManager.class )} instead. 968 */ 969 @Deprecated 970 public InternationalizationManager getInternationalizationManager() { 971 return getManager( InternationalizationManager.class ); 972 } 973 974 /** {@inheritDoc} */ 975 @Override 976 public final void addWikiEventListener( final WikiEventListener listener ) { 977 Synchronizer.synchronize(lock, () -> { 978 WikiEventManager.addWikiEventListener( this, listener ); 979 }); 980 } 981 982 /** 983 * {@inheritDoc} 984 */ 985 @Override 986 public final void removeWikiEventListener(final WikiEventListener listener) { 987 Synchronizer.synchronize(lock, () -> { 988 WikiEventManager.removeWikiEventListener(this, listener); 989 }); 990 } 991 992 /** 993 * Fires a WikiEngineEvent to all registered listeners. 994 * 995 * @param type the event type 996 */ 997 protected final void fireEvent( final int type ) { 998 if( WikiEventManager.isListening(this ) ) { 999 WikiEventManager.fireEvent( this, new WikiEngineEvent(this, type ) ); 1000 } 1001 } 1002 1003 /** 1004 * Fires a WikiPageEvent to all registered listeners. 1005 * 1006 * @param type the event type 1007 */ 1008 protected final void firePageEvent( final int type, final String pageName ) { 1009 if( WikiEventManager.isListening(this ) ) { 1010 WikiEventManager.fireEvent(this,new WikiPageEvent(this, type, pageName )); 1011 } 1012 } 1013 1014 /** {@inheritDoc} */ 1015 @Override 1016 public void setAttribute( final String key, final Object value ) { 1017 m_attributes.put( key, value ); 1018 } 1019 1020 /** {@inheritDoc} */ 1021 @Override 1022 @SuppressWarnings( "unchecked" ) 1023 public < T > T getAttribute( final String key ) { 1024 return ( T )m_attributes.get( key ); 1025 } 1026 1027 /** {@inheritDoc} */ 1028 @Override 1029 @SuppressWarnings( "unchecked" ) 1030 public < T > T removeAttribute( final String key ) { 1031 return ( T )m_attributes.remove( key ); 1032 } 1033 1034}