Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.struts2.components;

import org.apache.struts2.StrutsConstants;
import org.apache.struts2.config.ScopableConstants;

import java.util.Set;

/**
* Lets a package override the default UI theme and template directory with {@code <scoped-constant>}.
*
* @since 7.5.0
*/
public class StrutsUiScopableConstants implements ScopableConstants {

@Override
public Set<String> getNames() {
return Set.of(StrutsConstants.STRUTS_UI_THEME, StrutsConstants.STRUTS_UI_TEMPLATEDIR);
}
}
26 changes: 22 additions & 4 deletions core/src/main/java/org/apache/struts2/components/UIBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.struts2.components;

import org.apache.struts2.config.ConfigurationException;
import org.apache.struts2.config.ScopedConstantProvider;
import org.apache.struts2.inject.Inject;
import org.apache.struts2.util.TextParseUtil;
import org.apache.struts2.util.ValueStack;
Expand Down Expand Up @@ -528,28 +529,45 @@
// dynamic attributes
protected Map<String, Object> dynamicAttributes = new HashMap<>();

/** @deprecated no longer read; use {@link #scopedConstantProvider} */
@Deprecated(since = "7.5.0", forRemoval = true)
protected String defaultTemplateDir;

Check warning on line 534 in core/src/main/java/org/apache/struts2/components/UIBean.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=apache_struts&issues=AaExhiUoWwNrJ9-TfOBy&open=AaExhiUoWwNrJ9-TfOBy&pullRequest=2008
/** @deprecated no longer read; use {@link #scopedConstantProvider} */
@Deprecated(since = "7.5.0", forRemoval = true)
protected String defaultUITheme;

Check warning on line 537 in core/src/main/java/org/apache/struts2/components/UIBean.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=apache_struts&issues=AaExhiUoWwNrJ9-TfOB0&open=AaExhiUoWwNrJ9-TfOB0&pullRequest=2008
protected String uiThemeExpansionToken;
protected String uiStaticContentPath;

protected ScopedConstantProvider scopedConstantProvider;

protected TemplateEngineManager templateEngineManager;

protected CspNonceReader cspNonceReader;

protected HtmlConstraintProvider htmlConstraintProvider;
protected boolean html5ConstraintsEnabled;

@Inject(StrutsConstants.STRUTS_UI_TEMPLATEDIR)
/**
* @deprecated no longer read; set {@code struts.ui.templateDir} globally or per package with {@code <scoped-constant>}
*/
@Deprecated(since = "7.5.0", forRemoval = true)
public void setDefaultTemplateDir(String dir) {

Check warning on line 554 in core/src/main/java/org/apache/struts2/components/UIBean.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=apache_struts&issues=AaExhiUoWwNrJ9-TfOB2&open=AaExhiUoWwNrJ9-TfOB2&pullRequest=2008
this.defaultTemplateDir = dir;
}

@Inject(StrutsConstants.STRUTS_UI_THEME)
/**
* @deprecated no longer read; set {@code struts.ui.theme} globally or per package with {@code <scoped-constant>}
*/
@Deprecated(since = "7.5.0", forRemoval = true)
public void setDefaultUITheme(String theme) {

Check warning on line 562 in core/src/main/java/org/apache/struts2/components/UIBean.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Do not forget to remove this deprecated code someday.

See more on https://sonarcloud.io/project/issues?id=apache_struts&issues=AaExhiUoWwNrJ9-TfOB3&open=AaExhiUoWwNrJ9-TfOB3&pullRequest=2008
this.defaultUITheme = theme;
}

@Inject
public void setScopedConstantProvider(ScopedConstantProvider scopedConstantProvider) {
this.scopedConstantProvider = scopedConstantProvider;
}

@Inject(StrutsConstants.STRUTS_UI_THEME_EXPANSION_TOKEN)
public void setUIThemeExpansionToken(String uiThemeExpansionToken) {
this.uiThemeExpansionToken = uiThemeExpansionToken;
Expand Down Expand Up @@ -643,7 +661,7 @@

// Default template set
if (isBlank(result)) {
result = defaultTemplateDir;
result = scopedConstantProvider.getValue(StrutsConstants.STRUTS_UI_TEMPLATEDIR);
}

// Defaults to 'template'
Expand Down Expand Up @@ -675,7 +693,7 @@

// Default theme set
if (isBlank(result)) {
result = defaultUITheme;
result = scopedConstantProvider.getValue(StrutsConstants.STRUTS_UI_THEME);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import org.apache.struts2.DefaultUnknownHandlerManager;
import org.apache.struts2.FileManagerFactory;
import org.apache.struts2.UnknownHandlerManager;
import org.apache.struts2.components.StrutsUiScopableConstants;
import org.apache.struts2.config.Configuration;
import org.apache.struts2.config.ConfigurationException;
import org.apache.struts2.config.ConfigurationProvider;
import org.apache.struts2.config.ScopableConstants;
import org.apache.struts2.config.ScopedConstantProvider;
import org.apache.struts2.config.impl.DefaultConfiguration;
import org.apache.struts2.conversion.NullHandler;
Expand Down Expand Up @@ -132,6 +134,7 @@ public void register(ContainerBuilder builder, LocatableProperties props) throws
.factory(UnknownHandlerManager.class, DefaultUnknownHandlerManager.class, Scope.SINGLETON)

.factory(ScopedConstantProvider.class, StrutsScopedConstantProvider.class, Scope.SINGLETON)
.factory(ScopableConstants.class, StrutsUiScopableConstants.class, Scope.SINGLETON)

// silly workarounds for ognl since there is no way to flush its caches
.factory(PropertyAccessor.class, List.class.getName(), XWorkListPropertyAccessor.class, Scope.SINGLETON)
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/struts-beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@

<bean type="org.apache.struts2.config.ScopedConstantProvider" name="struts"
class="org.apache.struts2.config.providers.StrutsScopedConstantProvider" scope="singleton"/>
<bean type="org.apache.struts2.config.ScopableConstants" name="struts"
class="org.apache.struts2.components.StrutsUiScopableConstants" scope="singleton"/>

<bean type="org.apache.struts2.url.QueryStringBuilder" name="strutsQueryStringBuilder"
class="org.apache.struts2.url.StrutsQueryStringBuilder" scope="singleton"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.struts2.components;

import org.apache.struts2.ActionContext;
import org.apache.struts2.ActionSupport;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.config.entities.ActionConfig;
import org.apache.struts2.dispatcher.AttributeMap;
import org.apache.struts2.dispatcher.DispatcherConstants;
import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.mock.MockActionInvocation;
import org.apache.struts2.mock.MockActionProxy;
import org.apache.struts2.util.ValueStack;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.Map;

public class UIBeanScopedConstantsTest extends StrutsInternalTestCase {

private ValueStack stack;
private MockHttpServletRequest request;
private MockHttpServletResponse response;

@Override
protected void setUp() throws Exception {
super.setUp();
initDispatcherWithConfigs("struts-default.xml,org/apache/struts2/components/struts-scoped-ui.xml");
stack = ActionContext.getContext().getValueStack();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}

public void testPackageOverrideIsUsedWithoutTagAttribute() {
enterPackage("admin");

TextField txtFld = newTextField();

assertEquals("css_xhtml", txtFld.getTheme());
assertEquals("admin-template", txtFld.getTemplateDir());
}

public void testChildPackageInheritsOverride() {
enterPackage("adminChild");

TextField txtFld = newTextField();

assertEquals("css_xhtml", txtFld.getTheme());
assertEquals("admin-template", txtFld.getTemplateDir());
}

public void testGlobalValueWithoutOverride() {
enterPackage("plain");

TextField txtFld = newTextField();

assertEquals("xhtml", txtFld.getTheme());
assertEquals("template", txtFld.getTemplateDir());
}

public void testGlobalValueWithoutInvocation() {
TextField txtFld = newTextField();

assertEquals("xhtml", txtFld.getTheme());
assertEquals("template", txtFld.getTemplateDir());
}

public void testTagAttributeWinsOverPackageOverride() {
enterPackage("admin");

TextField txtFld = newTextField();
txtFld.setTheme("simple");
txtFld.setTemplateDir("tag-template");

assertEquals("simple", txtFld.getTheme());
assertEquals("tag-template", txtFld.getTemplateDir());
}

public void testFormThemeWinsOverPackageOverride() {
enterPackage("admin");

Form form = new Form(stack, request, response);
container.inject(form);
form.setTheme("simple");

assertEquals("simple", newTextField().getTheme());
}

public void testScopeAttributeWinsOverPackageOverride() {
enterPackage("admin");
request.setAttribute("theme", "simple");
request.setAttribute("templateDir", "attr-template");
Map<String, Object> context = stack.getContext();
context.put(DispatcherConstants.REQUEST, new RequestMap(request));
context.put("attr", new AttributeMap(context));

TextField txtFld = newTextField();

assertEquals("simple", txtFld.getTheme());
assertEquals("attr-template", txtFld.getTemplateDir());
}

private TextField newTextField() {
TextField txtFld = new TextField(stack, request, response);
container.inject(txtFld);
return txtFld;
}

private void enterPackage(String packageName) {
MockActionProxy proxy = new MockActionProxy();
proxy.setConfig(new ActionConfig.Builder(packageName, "test", ActionSupport.class.getName()).build());
MockActionInvocation invocation = new MockActionInvocation();
invocation.setProxy(proxy);
ActionContext.getContext().withActionInvocation(invocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void testBuildTemplate() {
MockHttpServletResponse res = new MockHttpServletResponse();

TextField txtFld = new TextField(stack, req, res);
container.inject(txtFld);

Template defaultTemplate = txtFld.buildTemplateName(null, defaultTemplateName);
Template customTemplate = txtFld.buildTemplateName(customTemplateName, defaultTemplateName);
Expand All @@ -203,7 +204,7 @@ public void testGetTemplateDirExplicit() {

TextField txtFld = new TextField(stack, req, res);
txtFld.setTemplateDir(explicitTemplateDir);
txtFld.setDefaultTemplateDir(defaultTemplateDir);
txtFld.setScopedConstantProvider(name -> defaultTemplateDir);

assertEquals(explicitTemplateDir, txtFld.getTemplateDir());
}
Expand All @@ -215,7 +216,7 @@ public void testGetTemplateDirDefault() {
MockHttpServletResponse res = new MockHttpServletResponse();

TextField txtFld = new TextField(stack, req, res);
txtFld.setDefaultTemplateDir(defaultTemplateDir);
txtFld.setScopedConstantProvider(name -> defaultTemplateDir);

assertEquals(defaultTemplateDir, txtFld.getTemplateDir());
}
Expand All @@ -226,6 +227,7 @@ public void testGetTemplateDirNoneSet() {
MockHttpServletResponse res = new MockHttpServletResponse();

TextField txtFld = new TextField(stack, req, res);
txtFld.setScopedConstantProvider(name -> null);

assertEquals("template", txtFld.getTemplateDir());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package org.apache.struts2.config;

import org.apache.struts2.ActionContext;
import org.apache.struts2.FileManagerFactory;
import org.apache.struts2.config.impl.DefaultConfiguration;
import org.apache.struts2.config.providers.StrutsDefaultConfigurationProvider;
import org.apache.struts2.inject.Container;
import org.apache.struts2.inject.ContainerBuilder;
import org.apache.struts2.inject.Scope;
import org.apache.struts2.util.fs.DefaultFileManagerFactory;
import org.apache.struts2.util.location.LocatableProperties;
import org.junit.After;
import org.junit.Test;

Expand Down Expand Up @@ -49,12 +55,20 @@ public void registeredNameLoads() {
.isEqualTo(Map.of("sample.scopable", "x"));
}

@Test
public void uiThemeAndTemplateDirAreScopable() {
Configuration configuration = load("struts-scoped-constants-ui.xml");

assertThat(configuration.getPackageConfig("ui").getScopedConstants())
.isEqualTo(Map.of("struts.ui.theme", "css_xhtml", "struts.ui.templateDir", "admin-template"));
}

@Test
public void unregisteredNameFails() {
assertThatThrownBy(() -> load("struts-scoped-constants-unregistered.xml"))
.isInstanceOf(ConfigurationException.class)
.hasStackTraceContaining("Package [unregistered] declares scoped constant [sample.unknown], which is not scopable. "
+ "Scopable constants: [sample.scopable, sample.unset].");
+ "Scopable constants: [sample.scopable, sample.unset, struts.ui.templateDir, struts.ui.theme].");
}

@Test
Expand All @@ -66,7 +80,7 @@ public void abstractPackageIsValidatedToo() {

@Test
public void anyScopedConstantFailsWhenNothingIsScopable() {
assertThatThrownBy(() -> load("struts-scoped-constants-no-registry.xml"))
assertThatThrownBy(() -> loadWithBootstrapBeansOnly("struts-scoped-constants-no-registry.xml"))
.isInstanceOf(ConfigurationException.class)
.hasStackTraceContaining("Package [unregistered] declares scoped constant [sample.scopable], which is not scopable. "
+ "No constants are scopable in this configuration.");
Expand All @@ -86,4 +100,18 @@ private Configuration load(String fixture) {
configurationManager.addContainerProvider(new StrutsXmlConfigurationProvider("org/apache/struts2/config/" + fixture));
return configurationManager.getConfiguration();
}

private Configuration loadWithBootstrapBeansOnly(String fixture) {
configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
configurationManager.addContainerProvider(new StrutsDefaultConfigurationProvider() {
@Override
public void register(ContainerBuilder builder, LocatableProperties props) {
DefaultConfiguration.bootstrapFactories(builder)
.factory(FileManagerFactory.class, DefaultFileManagerFactory.class, Scope.SINGLETON);
DefaultConfiguration.BOOTSTRAP_CONSTANTS.forEach((name, value) -> props.setProperty(name, String.valueOf(value)));
}
});
configurationManager.addContainerProvider(new StrutsXmlConfigurationProvider("org/apache/struts2/config/" + fixture));
return configurationManager.getConfiguration();
}
}
Loading
Loading