Skip to content

Commit

Permalink
do more test and clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserzamani committed May 20, 2021
1 parent a37e35b commit 3a62e32
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 151 deletions.
Expand Up @@ -160,7 +160,7 @@ public void setAliasesKey(String aliasesKey) {
ValueStack stack = ac.getValueStack();
Object obj = stack.findValue(aliasExpression);

if (obj != null && obj instanceof Map) {
if (obj instanceof Map) {
//get secure stack
ValueStack newStack = valueStackFactory.createValueStack(stack);
boolean clearableStack = newStack instanceof ClearableValueStack;
Expand Down
Expand Up @@ -217,7 +217,6 @@ protected Component findAncestor(Class<?> clazz) {
if (currPosition >= 0) {
int start = componentStack.size() - currPosition - 1;

//for (int i = componentStack.size() - 2; i >= 0; i--) {
for (int i = start; i >= 0; i--) {
Component component = (Component) componentStack.get(i);
if (clazz.isAssignableFrom(component.getClass()) && component != this) {
Expand Down Expand Up @@ -401,7 +400,7 @@ protected Object findValue(String expression, Class<?> toType) {
* @return the action url.
*/
protected String determineActionURL(String action, String namespace, String method,
HttpServletRequest req, HttpServletResponse res, Map parameters, String scheme,
HttpServletRequest req, HttpServletResponse res, Map<String, Object> parameters, String scheme,
boolean includeContext, boolean encodeResult, boolean forceAddSchemeHostAndPort,
boolean escapeAmp) {
String finalAction = findString(action);
Expand Down
29 changes: 15 additions & 14 deletions core/src/main/java/org/apache/struts2/components/Param.java
Expand Up @@ -125,29 +125,29 @@ public boolean end(Writer writer, String body) {
if (component instanceof UnnamedParametric) {
((UnnamedParametric) component).addParameter(findValue(value));
} else {
String name = findString(this.name);
String translatedName = findString(this.name);

if (name == null) {
if (translatedName == null) {
throw new StrutsException("No name found for following expression: " + this.name);
}

boolean evaluated = !name.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(name);
boolean evaluated = !translatedName.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
if (!reevaluate) {
throw new StrutsException("Excluded or not accepted name found: " + name);
throw new StrutsException("Excluded or not accepted name found: " + translatedName);
}

Object value = findValue(this.value);
Object foundValue = findValue(this.value);
if (suppressEmptyParameters) {
if (value != null && StringUtils.isNotBlank(value.toString())) {
component.addParameter(name, value);
if (foundValue != null && StringUtils.isNotBlank(foundValue.toString())) {
component.addParameter(translatedName, foundValue);
} else {
component.addParameter(name, null);
component.addParameter(translatedName, null);
}
} else if (value == null || StringUtils.isBlank(value.toString())) {
component.addParameter(name, "");
} else if (foundValue == null || StringUtils.isBlank(foundValue.toString())) {
component.addParameter(translatedName, "");
} else {
component.addParameter(name, value);
component.addParameter(translatedName, foundValue);
}
}
} else {
Expand All @@ -164,7 +164,8 @@ public boolean end(Writer writer, String body) {

return super.end(writer, "");
}


@Override
public boolean usesBody() {
return true;
}
Expand Down Expand Up @@ -199,7 +200,7 @@ public interface UnnamedParametric {
* Adds the given value as a parameter to the outer tag.
* @param value the value
*/
public void addParameter(Object value);
void addParameter(Object value);
}

}
129 changes: 62 additions & 67 deletions core/src/main/java/org/apache/struts2/components/UIBean.java
Expand Up @@ -563,16 +563,13 @@ public boolean end(Writer writer, String body) {
protected abstract String getDefaultTemplate();

protected Template buildTemplateName(String myTemplate, String myDefaultTemplate) {
String template = myDefaultTemplate;
String templateName = myDefaultTemplate;

if (myTemplate != null) {
template = findString(myTemplate);
templateName = findString(myTemplate);
}

String templateDir = getTemplateDir();
String theme = getTheme();

return new Template(templateDir, theme, template);
return new Template(getTemplateDir(), getTheme(), templateName);

}

Expand All @@ -589,73 +586,72 @@ protected void mergeTemplate(Writer writer, Template template) throws Exception
}

public String getTemplateDir() {
String templateDir = null;
String result = null;

if (this.templateDir != null) {
templateDir = findString(this.templateDir);
result = findString(this.templateDir);
}

// If templateDir is not explicitly given,
// try to find attribute which states the dir set to use
if (StringUtils.isBlank(templateDir)) {
templateDir = stack.findString("#attr.templateDir");
if (StringUtils.isBlank(result)) {
result = stack.findString("#attr.templateDir");
}

// Default template set
if (StringUtils.isBlank(templateDir)) {
templateDir = defaultTemplateDir;
if (StringUtils.isBlank(result)) {
result = defaultTemplateDir;
}

// Defaults to 'template'
if (StringUtils.isBlank(templateDir)) {
templateDir = "template";
if (StringUtils.isBlank(result)) {
result = "template";
}

return templateDir;
return result;
}

public String getTheme() {
String theme = null;
String result = null;

if (this.theme != null) {
theme = findString(this.theme);
result = findString(this.theme);
}

if (StringUtils.isBlank(theme)) {
if (StringUtils.isBlank(result)) {
Form form = (Form) findAncestor(Form.class);
if (form != null) {
theme = form.getTheme();
result = form.getTheme();
}
}

// If theme set is not explicitly given,
// try to find attribute which states the theme set to use
if (StringUtils.isBlank(theme)) {
theme = stack.findString("#attr.theme");
if (StringUtils.isBlank(result)) {
result = stack.findString("#attr.theme");
}

// Default theme set
if (StringUtils.isBlank(theme)) {
theme = defaultUITheme;
if (StringUtils.isBlank(result)) {
result = defaultUITheme;
}

return theme;
return result;
}

public void evaluateParams() {
String templateDir = getTemplateDir();
String theme = getTheme();
String gotTheme = getTheme();

addParameter("templateDir", templateDir);
addParameter("theme", theme);
addParameter("templateDir", getTemplateDir());
addParameter("theme", gotTheme);
addParameter("template", template != null ? findString(template) : getDefaultTemplate());
addParameter("dynamicAttributes", dynamicAttributes);
addParameter("themeExpansionToken", uiThemeExpansionToken);
addParameter("expandTheme", uiThemeExpansionToken + theme);
addParameter("expandTheme", uiThemeExpansionToken + gotTheme);

addParameter("staticContentPath", findString(uiStaticContentPath));

String name = null;
String translatedName = null;
String providedLabel = null;

if (this.key != null) {
Expand All @@ -671,8 +667,8 @@ public void evaluateParams() {
}

if (this.name != null) {
name = findString(this.name);
addParameter("name", name);
translatedName = findString(this.name);
addParameter("name", translatedName);
}

if (label != null) {
Expand Down Expand Up @@ -794,30 +790,31 @@ public void evaluateParams() {


// see if the value was specified as a parameter already
final String NAME_VALUE = "nameValue";
if (parameters.containsKey("value")) {
parameters.put("nameValue", parameters.get("value"));
parameters.put(NAME_VALUE, parameters.get("value"));
} else {
if (evaluateNameValue()) {
final Class<?> valueClazz = getValueClassType();

if (valueClazz != null) {
if (value != null) {
addParameter("nameValue", findValue(value, valueClazz));
} else if (name != null) {
boolean evaluated = !name.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(name);
addParameter(NAME_VALUE, findValue(value, valueClazz));
} else if (translatedName != null) {
boolean evaluated = !translatedName.equals(this.name);
boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
if (!reevaluate) {
addParameter("nameValue", name);
addParameter(NAME_VALUE, translatedName);
} else {
String expr = completeExpression(name);
addParameter("nameValue", findValue(expr, valueClazz));
String expr = completeExpression(translatedName);
addParameter(NAME_VALUE, findValue(expr, valueClazz));
}
}
} else {
if (value != null) {
addParameter("nameValue", findValue(value));
} else if (name != null) {
addParameter("nameValue", findValue(name));
addParameter(NAME_VALUE, findValue(value));
} else if (translatedName != null) {
addParameter(NAME_VALUE, findValue(translatedName));
}
}
}
Expand All @@ -831,10 +828,10 @@ public void evaluateParams() {
if (form != null ) {
addParameter("form", form.getParameters());

if ( name != null ) {
if ( translatedName != null ) {
// list should have been created by the form component
List<String> tags = (List<String>) form.getParameters().get("tagNames");
tags.add(name);
tags.add(translatedName);
}
}

Expand Down Expand Up @@ -897,11 +894,9 @@ public void evaluateParams() {

// to be used with the CSP interceptor - adds the nonce value as a parameter to be accessed from ftl files
Map<String, Object> session = stack.getActionContext().getSession();
if (session != null) {
if (session.containsKey("nonce")) {
String nonceValue = session.get("nonce").toString();
addParameter("nonce", nonceValue);
}
Object nonceValue = session != null ? session.get("nonce") : null;
if (nonceValue != null) {
addParameter("nonce", nonceValue.toString());
}

evaluateExtraParams();
Expand Down Expand Up @@ -961,14 +956,14 @@ protected void enableAncestorFormCustomOnsubmit() {

protected Map<String, String> getTooltipConfig(UIBean component) {
Object tooltipConfigObj = component.getParameters().get("tooltipConfig");
Map<String, String> tooltipConfig = new LinkedHashMap<>();
Map<String, String> result = new LinkedHashMap<>();

if (tooltipConfigObj instanceof Map) {
// we get this if its configured using
// 1] UI component's tooltipConfig attribute OR
// 2] <param name="tooltip" value="" /> param tag value attribute

tooltipConfig = new LinkedHashMap<>((Map) tooltipConfigObj);
result = new LinkedHashMap<>((Map) tooltipConfigObj);
} else if (tooltipConfigObj instanceof String) {

// we get this if its configured using
Expand All @@ -978,23 +973,23 @@ protected Map<String, String> getTooltipConfig(UIBean component) {

for (String aTooltipConfigArray : tooltipConfigArray) {
String[] configEntry = aTooltipConfigArray.trim().split("=");
String key = configEntry[0].trim();
String value;
String configKey = configEntry[0].trim();
String configValue;
if (configEntry.length > 1) {
value = configEntry[1].trim();
tooltipConfig.put(key, value);
configValue = configEntry[1].trim();
result.put(configKey, configValue);
} else {
LOG.warn("component {} tooltip config param {} has no value defined, skipped", component, key);
LOG.warn("component {} tooltip config param {} has no value defined, skipped", component, configKey);
}
}
}
if (component.javascriptTooltip != null)
tooltipConfig.put("jsTooltipEnabled", component.javascriptTooltip);
result.put("jsTooltipEnabled", component.javascriptTooltip);
if (component.tooltipIconPath != null)
tooltipConfig.put("tooltipIcon", component.tooltipIconPath);
result.put("tooltipIcon", component.tooltipIconPath);
if (component.tooltipDelay != null)
tooltipConfig.put("tooltipDelay", component.tooltipDelay);
return tooltipConfig;
result.put("tooltipDelay", component.tooltipDelay);
return result;
}

/**
Expand Down Expand Up @@ -1264,10 +1259,10 @@ public void setTooltipIconPath(String tooltipIconPath) {

public void setDynamicAttributes(Map<String, String> tagDynamicAttributes) {
for (Map.Entry<String, String> entry : tagDynamicAttributes.entrySet()) {
String key = entry.getKey();
String entryKey = entry.getKey();

if (!isValidTagAttribute(key)) {
dynamicAttributes.put(key, entry.getValue());
if (!isValidTagAttribute(entryKey)) {
dynamicAttributes.put(entryKey, entry.getValue());
}
}
}
Expand All @@ -1281,9 +1276,9 @@ public void setDynamicAttributes(Map<String, String> tagDynamicAttributes) {
public void copyParams(Map<String, Object> params) {
super.copyParams(params);
for (Map.Entry<String, Object>entry : params.entrySet()) {
String key = entry.getKey();
if (!isValidTagAttribute(key) && !key.equals("dynamicAttributes")) {
dynamicAttributes.put(key, entry.getValue());
String entryKey = entry.getKey();
if (!isValidTagAttribute(entryKey) && !entryKey.equals("dynamicAttributes")) {
dynamicAttributes.put(entryKey, entry.getValue());
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/apache/struts2/result/StreamResult.java
Expand Up @@ -240,15 +240,16 @@ protected void doExecute(String finalLocation, ActionInvocation invocation) thro

LOG.debug("Set the content length: {}", contentLength);
if (contentLength != null) {
String _contentLength = conditionalParse(contentLength, invocation);
int _contentLengthAsInt;
String translatedContentLength = conditionalParse(contentLength, invocation);
int contentLengthAsInt;
try {
_contentLengthAsInt = Integer.parseInt(_contentLength);
if (_contentLengthAsInt >= 0) {
oResponse.setContentLength(_contentLengthAsInt);
contentLengthAsInt = Integer.parseInt(translatedContentLength);
if (contentLengthAsInt >= 0) {
oResponse.setContentLength(contentLengthAsInt);
}
} catch (NumberFormatException e) {
LOG.warn("failed to recognize {} as a number, contentLength header will not be set", _contentLength, e);
LOG.warn("failed to recognize {} as a number, contentLength header will not be set",
translatedContentLength, e);
}
}

Expand Down

0 comments on commit 3a62e32

Please sign in to comment.