All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wiki.plugin.InsertPage Maven / Gradle / Ivy

/*
    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 org.apache.wiki.plugin;

import org.apache.wiki.api.core.Context;
import org.apache.wiki.api.core.ContextEnum;
import org.apache.wiki.api.core.Engine;
import org.apache.wiki.api.core.Page;
import org.apache.wiki.api.exceptions.PluginException;
import org.apache.wiki.api.exceptions.ProviderException;
import org.apache.wiki.api.plugin.Plugin;
import org.apache.wiki.auth.AuthorizationManager;
import org.apache.wiki.auth.permissions.PermissionFactory;
import org.apache.wiki.pages.PageManager;
import org.apache.wiki.preferences.Preferences;
import org.apache.wiki.render.RenderingManager;
import org.apache.wiki.util.HttpUtil;
import org.apache.wiki.util.TextUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;


/**
 *  Inserts page contents.  Muchos thanks to Scott Hurlbert for the initial code.
 *
 *  

Parameters :

*
    *
  • page - the name of the page to be inserted
  • *
  • style - the style to use
  • *
  • maxlength - the maximum length of the page to be inserted (page contents)
  • *
  • class - the class to use
  • *
  • section - the section of the page that has to be inserted (separated by "----"
  • *
  • default - the text to insert if the requested page does not exist
  • *
* * @since 2.1.37 */ public class InsertPage implements Plugin { /** Parameter name for setting the page. Value is {@value}. */ public static final String PARAM_PAGENAME = "page"; /** Parameter name for setting the style. Value is {@value}. */ public static final String PARAM_STYLE = "style"; /** Parameter name for setting the maxlength. Value is {@value}. */ public static final String PARAM_MAXLENGTH = "maxlength"; /** Parameter name for setting the class. Value is {@value}. */ public static final String PARAM_CLASS = "class"; /** Parameter name for setting the show option. Value is {@value}. */ public static final String PARAM_SHOW = "show"; /** Parameter name for setting the section. Value is {@value}. */ public static final String PARAM_SECTION = "section"; /** Parameter name for setting the default. Value is {@value}. */ public static final String PARAM_DEFAULT = "default"; private static final String DEFAULT_STYLE = ""; private static final String ONCE_COOKIE = "JSPWiki.Once."; /** This attribute is stashed in the WikiContext to make sure that we don't have circular references. */ public static final String ATTR_RECURSE = "org.apache.wiki.plugin.InsertPage.recurseCheck"; /** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public String execute( final Context context, final Map params ) throws PluginException { final Engine engine = context.getEngine(); final StringBuilder res = new StringBuilder(); final String clazz = TextUtil.replaceEntities(params.get( PARAM_CLASS )); final String includedPage = TextUtil.replaceEntities(params.get( PARAM_PAGENAME )); String style = TextUtil.replaceEntities(params.get( PARAM_STYLE )); final boolean showOnce = "once".equals( params.get( PARAM_SHOW ) ); final String defaultstr = params.get( PARAM_DEFAULT ); final int section = TextUtil.parseIntParameter(params.get( PARAM_SECTION ), -1 ); int maxlen = TextUtil.parseIntParameter(params.get( PARAM_MAXLENGTH ), -1 ); final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); if( style == null ) { style = DEFAULT_STYLE; } if( maxlen == -1 ) { maxlen = Integer.MAX_VALUE; } if( includedPage != null ) { final Page page; try { final String pageName = engine.getFinalPageName( includedPage ); page = engine.getManager(PageManager.class).getPage(Objects.requireNonNullElse(pageName, includedPage)); } catch( final ProviderException e ) { res.append( "Page could not be found by the page provider." ); return res.toString(); } if( page != null ) { // Check for recursivity List previousIncludes = context.getVariable( ATTR_RECURSE ); if( previousIncludes != null ) { if( previousIncludes.contains( page.getName() ) ) { return "Error: Circular reference - you can't include a page in itself!"; } } else { previousIncludes = new ArrayList<>(); } // Check for permissions final AuthorizationManager mgr = engine.getManager( AuthorizationManager.class ); if( !mgr.checkPermission( context.getWikiSession(), PermissionFactory.getPagePermission( page, "view") ) ) { res.append("You do not have permission to view this included page."); return res.toString(); } // Show Once // Check for page-cookie, only include page if cookie is not yet set String cookieName = ""; if( showOnce ) { cookieName = ONCE_COOKIE + TextUtil.urlEncodeUTF8( page.getName() ).replaceAll( "\\+", "%20" ); if( HttpUtil.retrieveCookieValue( context.getHttpRequest(), cookieName ) != null ) { return ""; //silent exit } } // move here, after premature exit points (permissions, page-cookie) previousIncludes.add( page.getName() ); context.setVariable( ATTR_RECURSE, previousIncludes ); /** * We want inclusion to occur within the context of * its own page, because we need the links to be correct. */ final Context includedContext = context.clone(); includedContext.setPage( page ); String pageData = engine.getManager( PageManager.class ).getPureText( page ); String moreLink = ""; if( section != -1 ) { try { pageData = TextUtil.getSection( pageData, section ); } catch( final IllegalArgumentException e ) { throw new PluginException( e.getMessage() ); } } if( pageData.length() > maxlen ) { pageData = pageData.substring( 0, maxlen )+" ..."; moreLink = "

"+rb.getString("insertpage.more")+"

"; } res.append("
"); res.append( engine.getManager( RenderingManager.class ).textToHTML( includedContext, pageData ) ); res.append( moreLink ); res.append("
"); // // Remove the name from the stack; we're now done with this. // previousIncludes.remove( page.getName() ); context.setVariable( ATTR_RECURSE, previousIncludes ); } else { if( defaultstr != null ) { res.append( defaultstr ); } else { res.append( "There is no page called '" ).append( includedPage ).append( "'. Would you like to " ); res.append( "create it?" ); } } } else { res.append( "" ); res.append( "You have to define a page!" ); res.append( "" ); } return res.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy