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 c1c6955cf0ad2c3b472425d82ba91f4008ce842c Author: Juan Pablo Santos RodrÃguez <[email protected]> AuthorDate: Tue Mar 22 11:47:36 2022 +0100 TemplateManager now understands resources beginning with 'engine://' as keys of Engine's wiki properties --- .../java/org/apache/wiki/ui/TemplateManager.java | 26 +++++--- .../org/apache/wiki/ui/TemplateManagerTest.java | 75 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/jspwiki-main/src/main/java/org/apache/wiki/ui/TemplateManager.java b/jspwiki-main/src/main/java/org/apache/wiki/ui/TemplateManager.java index 8c457a2..b1fa47c 100644 --- a/jspwiki-main/src/main/java/org/apache/wiki/ui/TemplateManager.java +++ b/jspwiki-main/src/main/java/org/apache/wiki/ui/TemplateManager.java @@ -18,6 +18,7 @@ */ package org.apache.wiki.ui; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.wiki.api.core.Context; import org.apache.wiki.i18n.InternationalizationManager; @@ -102,7 +103,7 @@ public interface TemplateManager extends ModuleManager { boolean templateExists( String templateName ); /** - * An utility method for finding a JSP page. It searches only under either current context or by the absolute name. + * A utility method for finding a JSP page. It searches only under either current context or by the absolute name. * * @param pageContext the JSP PageContext * @param name The name of the JSP page to look for (e.g "Wiki.jsp") @@ -249,7 +250,7 @@ public interface TemplateManager extends ModuleManager { } /** - * Returns the include resources marker for a given type. This is in a + * Returns the include resources marker for a given type. This is in an * HTML or Javascript comment format. * * @param context the wiki context @@ -298,7 +299,7 @@ public interface TemplateManager extends ModuleManager { * Adds a resource request to the current request context. The content will be added at the resource-type marker * (see IncludeResourcesTag) in WikiJSPFilter. * <p> - * The resources can be of different types. For RESOURCE_SCRIPT and RESOURCE_STYLESHEET this is an URI path to the resource + * The resources can be of different types. For RESOURCE_SCRIPT and RESOURCE_STYLESHEET this is a URI path to the resource * (a script file or an external stylesheet) that needs to be included. For RESOURCE_INLINECSS the resource should be something * that can be added between <style></style> in the header file (commonheader.jsp). For RESOURCE_JSFUNCTION it is the name * of the Javascript function that should be run at page load. @@ -311,7 +312,7 @@ public interface TemplateManager extends ModuleManager { * rendered. It's thus a good idea to make this request only once during the page life cycle. * * @param ctx The current wiki context - * @param type What kind of a request should be added? + * @param type What kind of resource should be added? * @param resource The resource to add. */ static void addResourceRequest( final Context ctx, final String type, final String resource ) { @@ -324,21 +325,28 @@ public interface TemplateManager extends ModuleManager { if( resources == null ) { resources = new Vector<>(); } + String resolvedResource = resource; + if( StringUtils.startsWith( resource, "engine://" ) ) { + final String val = ctx.getEngine().getWikiProperties().getProperty( resource.substring( 9 ) ); // "engine//:".length() == 9 + if( StringUtils.isNotBlank( val ) ) { + resolvedResource = val; + } + } String resourceString = null; switch( type ) { case RESOURCE_SCRIPT: - resourceString = "<script type='text/javascript' src='" + resource + "'></script>"; + resourceString = "<script type='text/javascript' src='" + resolvedResource + "'></script>"; break; case RESOURCE_STYLESHEET: - resourceString = "<link rel='stylesheet' type='text/css' href='" + resource + "' />"; + resourceString = "<link rel='stylesheet' type='text/css' href='" + resolvedResource + "' />"; break; case RESOURCE_INLINECSS: - resourceString = "<style type='text/css'>\n" + resource + "\n</style>\n"; + resourceString = "<style type='text/css'>\n" + resolvedResource + "\n</style>\n"; break; case RESOURCE_JSFUNCTION: case RESOURCE_HTTPHEADER: - resourceString = resource; + resourceString = resolvedResource; break; } @@ -346,7 +354,7 @@ public interface TemplateManager extends ModuleManager { resources.add( resourceString ); } - LogManager.getLogger( TemplateManager.class ).debug( "Request to add a resource: " + resourceString ); + LogManager.getLogger( TemplateManager.class ).debug( "Request to add a resource: {}", resourceString ); resourcemap.put( type, resources ); ctx.setVariable( RESOURCE_INCLUDES, resourcemap ); diff --git a/jspwiki-main/src/test/java/org/apache/wiki/ui/TemplateManagerTest.java b/jspwiki-main/src/test/java/org/apache/wiki/ui/TemplateManagerTest.java new file mode 100644 index 0000000..03f8506 --- /dev/null +++ b/jspwiki-main/src/test/java/org/apache/wiki/ui/TemplateManagerTest.java @@ -0,0 +1,75 @@ +/* + 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.ui; + +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.core.Engine; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.HashMap; +import java.util.Properties; +import java.util.Vector; +import java.util.stream.Stream; + + +@ExtendWith( MockitoExtension.class ) +class TemplateManagerTest { + + @Mock + Context ctx; + + @Mock + Engine engine; + + @ParameterizedTest + @MethodSource( "provideArgumentsForAddResourceRequest" ) + void shouldCheckAddResourceRequest( final String type, final String res, final String expected ) { + if( TemplateManager.RESOURCE_SCRIPT.equals( type ) || TemplateManager.RESOURCE_STYLESHEET.equals( type ) ) { + final Properties properties = new Properties(); + properties.setProperty( "jspwiki.syntax.plain", "plain/wiki-snips-jspwiki.js" ); + Mockito.doReturn( engine ).when( ctx ).getEngine(); + Mockito.doReturn( properties ).when( engine ).getWikiProperties(); + } + + Mockito.doAnswer( invocationOnMock -> { + final HashMap< String, Vector< String > > map = invocationOnMock.getArgument( 1, HashMap.class ); + Assertions.assertEquals( expected, map.get( type ).get( 0 ) ); + return null; + } ).when( ctx ).setVariable( Mockito.eq( TemplateManager.RESOURCE_INCLUDES ), Mockito.any( HashMap.class ) ); + + TemplateManager.addResourceRequest( ctx, type, res ); + } + + static Stream< Arguments > provideArgumentsForAddResourceRequest() { + return Stream.of( + Arguments.of( TemplateManager.RESOURCE_SCRIPT, "engine://jspwiki.syntax.plain", "<script type='text/javascript' src='plain/wiki-snips-jspwiki.js'></script>" ), + Arguments.of( TemplateManager.RESOURCE_STYLESHEET, "engine://jspwiki.whatever.not.exists", "<link rel='stylesheet' type='text/css' href='engine://jspwiki.whatever.not.exists' />" ), + Arguments.of( TemplateManager.RESOURCE_INLINECSS, "color: white", "<style type='text/css'>\ncolor: white\n</style>\n" ), + Arguments.of( TemplateManager.RESOURCE_JSFUNCTION, "function whatever() {}", "function whatever() {}" ) + ); + } + +}
