This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 13f2d0df7c10a6ce0f07731735879b0ecbd03e11 Author: juanpablo <[email protected]> AuthorDate: Sat Jan 11 22:48:17 2020 +0100 move RenderingManager#VAR_EXECUTE_PLUGINS constant to WikiContext --- .../src/main/java/org/apache/wiki/WikiContext.java | 6 + .../apache/wiki/auth/acl/DefaultAclManager.java | 138 +++++++++------------ .../java/org/apache/wiki/parser/PluginContent.java | 14 +-- .../wiki/render/DefaultRenderingManager.java | 2 +- .../org/apache/wiki/render/RenderingManager.java | 6 - 5 files changed, 71 insertions(+), 95 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java index ace5764..4b9a1d6 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/WikiContext.java @@ -71,6 +71,12 @@ public class WikiContext implements Cloneable, Command { public static final String ATTR_CONTEXT = "jspwiki.context"; + /** + * Variable name which tells whether plugins should be executed or not. Value can be either {@code Boolean.TRUE} or + * {@code Boolean.FALSE}. While not set it's value is {@code null}. + */ + public static final String VAR_EXECUTE_PLUGINS = "_PluginContent.execute"; + /** Name of the variable which is set to Boolean.TRUE or Boolean.FALSE depending on whether WYSIWYG is currently in effect. */ public static final String VAR_WYSIWYG_EDITOR_MODE = "WYSIWYG_EDITOR_MODE"; diff --git a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/DefaultAclManager.java b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/DefaultAclManager.java index 860eafa..d5ff97f 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/DefaultAclManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/auth/acl/DefaultAclManager.java @@ -31,12 +31,10 @@ import org.apache.wiki.auth.permissions.PagePermission; import org.apache.wiki.auth.permissions.PermissionFactory; import org.apache.wiki.pages.PageLock; import org.apache.wiki.pages.PageManager; -import org.apache.wiki.render.RenderingManager; import java.security.Permission; import java.security.Principal; import java.util.ArrayList; -import java.util.Collections; import java.util.Enumeration; import java.util.List; import java.util.Map; @@ -73,72 +71,66 @@ public class DefaultAclManager implements AclManager { * the second is the list of Principals separated by commas. The overall match is * the ACL string from [{ to }]. */ - public static final Pattern ACL_PATTERN = Pattern.compile(ACL_REGEX); + public static final Pattern ACL_PATTERN = Pattern.compile( ACL_REGEX ); /** * Initializes the AclManager with a supplied wiki engine and properties. * * @param engine the wiki engine * @param props the initialization properties - * @see org.apache.wiki.auth.acl.AclManager#initialize(org.apache.wiki.WikiEngine, - * java.util.Properties) + * @see org.apache.wiki.auth.acl.AclManager#initialize(org.apache.wiki.WikiEngine, java.util.Properties) */ - public void initialize(WikiEngine engine, Properties props) { + public void initialize( final WikiEngine engine, final Properties props ) { m_auth = engine.getAuthorizationManager(); m_engine = engine; } /** - * A helper method for parsing textual AccessControlLists. The line is in - * form "ALLOW <permission> <principal>, <principal>, <principal>". This - * method was moved from Authorizer. + * A helper method for parsing textual AccessControlLists. The line is in form + * {@code ALLOW <permission> <principal>, <principal>, <principal>}. This method was moved from Authorizer. * - * @param page The current wiki page. If the page already has an ACL, it - * will be used as a basis for this ACL in order to avoid the - * creation of a new one. + * @param page The current wiki page. If the page already has an ACL, it will be used as a basis for this ACL in order to avoid the + * creation of a new one. * @param ruleLine The rule line, as described above. * @return A valid Access Control List. May be empty. * @throws WikiSecurityException if the ruleLine was faulty somehow. * @since 2.1.121 */ - public Acl parseAcl(WikiPage page, String ruleLine) throws WikiSecurityException { + public Acl parseAcl( final WikiPage page, final String ruleLine ) throws WikiSecurityException { Acl acl = page.getAcl(); if (acl == null) { acl = new AclImpl(); } try { - StringTokenizer fieldToks = new StringTokenizer(ruleLine); + final StringTokenizer fieldToks = new StringTokenizer(ruleLine); fieldToks.nextToken(); - String actions = fieldToks.nextToken(); - page.getName(); + final String actions = fieldToks.nextToken(); - while (fieldToks.hasMoreTokens()) { - String principalName = fieldToks.nextToken(",").trim(); - Principal principal = m_auth.resolvePrincipal(principalName); - AclEntry oldEntry = acl.getEntry(principal); + while( fieldToks.hasMoreTokens() ) { + final String principalName = fieldToks.nextToken(",").trim(); + final Principal principal = m_auth.resolvePrincipal(principalName); + final AclEntry oldEntry = acl.getEntry(principal); - if (oldEntry != null) { - log.debug("Adding to old acl list: " + principal + ", " + actions); - oldEntry.addPermission(PermissionFactory.getPagePermission(page, actions)); + if( oldEntry != null ) { + log.debug( "Adding to old acl list: " + principal + ", " + actions ); + oldEntry.addPermission( PermissionFactory.getPagePermission( page, actions ) ); } else { - log.debug("Adding new acl entry for " + actions); - AclEntry entry = new AclEntryImpl(); + log.debug( "Adding new acl entry for " + actions ); + final AclEntry entry = new AclEntryImpl(); + entry.setPrincipal( principal ); + entry.addPermission( PermissionFactory.getPagePermission( page, actions ) ); - entry.setPrincipal(principal); - entry.addPermission(PermissionFactory.getPagePermission(page, actions)); - - acl.addEntry(entry); + acl.addEntry( entry ); } } - page.setAcl(acl); - - log.debug(acl.toString()); - } catch (NoSuchElementException nsee) { - log.warn("Invalid access rule: " + ruleLine + " - defaults will be used."); - throw new WikiSecurityException("Invalid access rule: " + ruleLine, nsee); - } catch (IllegalArgumentException iae) { + page.setAcl( acl ); + log.debug( acl.toString() ); + } catch( final NoSuchElementException nsee ) { + log.warn( "Invalid access rule: " + ruleLine + " - defaults will be used." ); + throw new WikiSecurityException( "Invalid access rule: " + ruleLine, nsee ); + } catch( final IllegalArgumentException iae ) { throw new WikiSecurityException("Invalid permission type: " + ruleLine, iae); } @@ -159,28 +151,19 @@ public class DefaultAclManager implements AclManager { * @since 2.2.121 */ public Acl getPermissions( final WikiPage page ) { - // // Does the page already have cached ACLs? - // Acl acl = page.getAcl(); - log.debug("page=" + page.getName() + "\n" + acl); + log.debug( "page=" + page.getName() + "\n" + acl ); - if (acl == null) { - // + if( acl == null ) { // If null, try the parent. - // if( page instanceof Attachment ) { final WikiPage parent = m_engine.getPageManager().getPage( ( ( Attachment ) page ).getParentName() ); - acl = getPermissions(parent); } else { - // // Or, try parsing the page - // - final WikiContext ctx = new WikiContext(m_engine, page); - - ctx.setVariable(RenderingManager.VAR_EXECUTE_PLUGINS, Boolean.FALSE); - + final WikiContext ctx = new WikiContext( m_engine, page ); + ctx.setVariable( WikiContext.VAR_EXECUTE_PLUGINS, Boolean.FALSE ); m_engine.getHTML(ctx, page); if (page.getAcl() == null) { @@ -234,47 +217,44 @@ public class DefaultAclManager implements AclManager { * @param acl the ACL * @return the ACL string */ - protected static String printAcl(Acl acl) { + protected static String printAcl( final Acl acl ) { // Extract the ACL entries into a Map with keys == permissions, values == principals - Map<String, List<Principal>> permissionPrincipals = new TreeMap<String, List<Principal>>(); - Enumeration<AclEntry> entries = acl.entries(); - while (entries.hasMoreElements()) { - AclEntry entry = entries.nextElement(); - Principal principal = entry.getPrincipal(); - Enumeration<Permission> permissions = entry.permissions(); - while (permissions.hasMoreElements()) { - Permission permission = permissions.nextElement(); - List<Principal> principals = permissionPrincipals.get(permission.getActions()); + final Map< String, List< Principal > > permissionPrincipals = new TreeMap<>(); + final Enumeration< AclEntry > entries = acl.entries(); + while( entries.hasMoreElements() ) { + final AclEntry entry = entries.nextElement(); + final Principal principal = entry.getPrincipal(); + final Enumeration< Permission > permissions = entry.permissions(); + while( permissions.hasMoreElements() ) { + final Permission permission = permissions.nextElement(); + List< Principal > principals = permissionPrincipals.get( permission.getActions() ); if (principals == null) { - principals = new ArrayList<Principal>(); - String action = permission.getActions(); - if (action.indexOf(',') != -1) { + principals = new ArrayList<>(); + final String action = permission.getActions(); + if( action.indexOf(',') != -1 ) { throw new IllegalStateException("AclEntry permission cannot have multiple targets."); } - permissionPrincipals.put(action, principals); + permissionPrincipals.put( action, principals ); } - principals.add(principal); + principals.add( principal ); } } // Now, iterate through each permission in the map and generate an ACL string - - StringBuilder s = new StringBuilder(); - for (Map.Entry<String, List<Principal>> entry : permissionPrincipals.entrySet()) { - String action = entry.getKey(); - List<Principal> principals = entry.getValue(); - Collections.sort(principals, new PrincipalComparator()); - s.append("[{ALLOW "); - s.append(action); - s.append(" "); - for (int i = 0; i < principals.size(); i++) { - Principal principal = principals.get(i); - s.append(principal.getName()); - if (i < (principals.size() - 1)) { - s.append(","); + final StringBuilder s = new StringBuilder(); + for( final Map.Entry< String, List< Principal > > entry : permissionPrincipals.entrySet() ) { + final String action = entry.getKey(); + final List< Principal > principals = entry.getValue(); + principals.sort( new PrincipalComparator() ); + s.append( "[{ALLOW " ).append( action ).append( " " ); + for( int i = 0; i < principals.size(); i++ ) { + final Principal principal = principals.get( i ); + s.append( principal.getName() ); + if( i < ( principals.size() - 1 ) ) { + s.append( "," ); } } - s.append("}]\n"); + s.append( "}]\n" ); } return s.toString(); } diff --git a/jspwiki-main/src/main/java/org/apache/wiki/parser/PluginContent.java b/jspwiki-main/src/main/java/org/apache/wiki/parser/PluginContent.java index 56b9c30..1fa4947 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/parser/PluginContent.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/parser/PluginContent.java @@ -30,7 +30,6 @@ import org.apache.wiki.api.exceptions.PluginException; import org.apache.wiki.api.plugin.ParserStagePlugin; import org.apache.wiki.api.plugin.WikiPlugin; import org.apache.wiki.preferences.Preferences; -import org.apache.wiki.render.RenderingManager; import org.jdom2.Text; import java.io.IOException; @@ -44,13 +43,10 @@ import java.util.ResourceBundle; /** * Stores the contents of a plugin in a WikiDocument DOM tree. * <p/> - * If the RenderingManager.WYSIWYG_EDITOR_MODE is set to Boolean.TRUE in the context, - * then the plugin - * is rendered as WikiMarkup. This allows an HTML editor to work without - * rendering the plugin each time as well. + * If the WikiContext.VAR_WYSIWYG_EDITOR_MODE is set to Boolean.TRUE in the context, then the plugin is rendered as WikiMarkup. + * This allows an HTML editor to work without rendering the plugin each time as well. * <p/> - * If RenderingManager.VAR_EXECUTE_PLUGINS is set to Boolean.FALSE, then - * the plugin is not executed. + * If WikiContext.VAR_EXECUTE_PLUGINS is set to Boolean.FALSE, then the plugin is not executed. * * @since 2.4 */ @@ -176,7 +172,7 @@ public class PluginContent extends Text { final String cmdLine = m_params.get( CMDLINE ).replaceAll( LINEBREAK, ELEMENT_BR ); result = result + cmdLine + PLUGIN_END; } else { - final Boolean b = ( Boolean )context.getVariable(RenderingManager.VAR_EXECUTE_PLUGINS ); + final Boolean b = ( Boolean )context.getVariable( WikiContext.VAR_EXECUTE_PLUGINS ); if (b != null && !b ) { return BLANK; } @@ -222,7 +218,7 @@ public class PluginContent extends Text { final Map< String, String > params = getParameters(); final WikiPlugin plugin = pm.newWikiPlugin( getPluginName(), rb ); try { - if( plugin != null && plugin instanceof ParserStagePlugin ) { + if( plugin instanceof ParserStagePlugin ) { ( ( ParserStagePlugin )plugin ).executeParser(this, context, params ); } } catch( final ClassCastException e ) { diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/DefaultRenderingManager.java b/jspwiki-main/src/main/java/org/apache/wiki/render/DefaultRenderingManager.java index 989a7ba..5e63df5 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/DefaultRenderingManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/DefaultRenderingManager.java @@ -154,7 +154,7 @@ public class DefaultRenderingManager implements RenderingManager { public WikiDocument getRenderedDocument( final WikiContext context, final String pagedata ) { final String pageid = context.getRealPage().getName() + VERSION_DELIMITER + context.getRealPage().getVersion() + VERSION_DELIMITER + - context.getVariable( DefaultRenderingManager.VAR_EXECUTE_PLUGINS ); + context.getVariable( WikiContext.VAR_EXECUTE_PLUGINS ); if( useCache( context ) ) { final Element element = m_documentCache.get( pageid ); diff --git a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java index 12564dc..ea0d0e6 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/render/RenderingManager.java @@ -57,12 +57,6 @@ public interface RenderingManager extends WikiEventListener, InternalModule { String DOCUMENTCACHE_NAME = "jspwiki.renderingCache"; /** - * Variable name which tells whether plugins should be executed or not. Value can be either {@code Boolean.TRUE} or - * {@code Boolean.FALSE}. While not set it's value is {@code null}. - */ - String VAR_EXECUTE_PLUGINS = "_PluginContent.execute"; - - /** * Initializes the RenderingManager. * Checks for cache size settings, initializes the document cache. * Looks for alternative WikiRenderers, initializes one, or the default
