Skip to content

WW-5326 Upgrade to OGNL 3.5.x and StrutsContext #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
Expand Up @@ -20,10 +20,11 @@
*/
package org.apache.struts2.showcase.chat;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.conversion.impl.XWorkConverter;
import org.apache.struts2.inject.Inject;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.struts2.util.StrutsTypeConverter;

import java.text.ParseException;
Expand All @@ -33,36 +34,35 @@

public class DateConverter extends StrutsTypeConverter {

private static final Logger LOG = LogManager.getLogger(DateConverter.class);

private XWorkConverter fallbackConverter;
private static final Logger LOG = LogManager.getLogger(DateConverter.class);

@Inject
public void setXWorkConverter(XWorkConverter fallbackConverter) {
this.fallbackConverter = fallbackConverter;
}
private XWorkConverter fallbackConverter;

public Object convertFromString(Map context, String[] values, Class toClass) {
@Inject
public void setXWorkConverter(XWorkConverter fallbackConverter) {
this.fallbackConverter = fallbackConverter;
}

if (values.length > 0 && values[0] != null && values[0].trim().length() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat();
try {
return sdf.parse(values[0]);
} catch (ParseException e) {
LOG.warn("error converting value [" + values[0] + "] to Date. Trying fallback converter.");
return this.fallbackConverter.convertValue(context, values[0], toClass);
}
}
return null;
}
public Object convertFromString(StrutsContext context, String[] values, Class toClass) {

public String convertToString(Map context, Object o) {
if (values.length > 0 && values[0] != null && values[0].trim().length() > 0) {
SimpleDateFormat sdf = new SimpleDateFormat();
try {
return sdf.parse(values[0]);
} catch (ParseException e) {
LOG.warn("error converting value [" + values[0] + "] to Date. Trying fallback converter.");
return this.fallbackConverter.convertValue(context, values[0], toClass);
}
}
return null;
}

if (o instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
return sdf.format((Date) o);
}
return "";
}
public String convertToString(StrutsContext context, Object o) {
if (o instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
return sdf.format((Date) o);
}
return "";
}
}

22 changes: 19 additions & 3 deletions core/src/main/java/org/apache/struts2/ActionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.inject.Container;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.struts2.util.ValueStack;

import java.io.Serializable;
Expand Down Expand Up @@ -106,14 +107,23 @@ public class ActionContext implements Serializable {
*/
private static final String CONTAINER = "org.apache.struts2.ActionContext.container";

private final Map<String, Object> context;
private final StrutsContext context;

/**
* Creates a new ActionContext initialized with another context.
*
* @param context a context map.
*/
protected ActionContext(Map<String, Object> context) {
this.context = StrutsContext.of(context);
}

/**
* Creates a new ActionContext initialized with another context.
*
* @param context a StrutsContext
*/
protected ActionContext(StrutsContext context) {
this.context = context;
}

Expand All @@ -127,7 +137,7 @@ public static ActionContext of(Map<String, Object> context) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null!");
}
return new ActionContext(context);
return new ActionContext(StrutsContext.of(context));
}

/**
Expand All @@ -136,7 +146,7 @@ public static ActionContext of(Map<String, Object> context) {
* @return new ActionContext
*/
public static ActionContext of() {
return of(new HashMap<>());
return of(StrutsContext.empty());
}

/**
Expand Down Expand Up @@ -232,11 +242,17 @@ public Map<String, Object> getApplication() {
* Gets the context map.
*
* @return the context map.
* @deprecated since 7.1.0, use {@link #getStrutsContext()} instead
*/
@Deprecated(since = "7.1.0", forRemoval = true)
public Map<String, Object> getContextMap() {
return context;
}

public StrutsContext getStrutsContext() {
return context;
}

/**
* Sets conversion errors which occurred when executing the action.
*
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/apache/struts2/ActionProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.apache.struts2;

import java.util.Map;
import org.apache.struts2.ognl.StrutsContext;

/**
* The {@link ActionProxyFactory} is used to create {@link ActionProxy}s to be executed.
Expand Down Expand Up @@ -48,7 +48,7 @@ public interface ActionProxyFactory {
* @return ActionProxy the created action proxy
* @since 2.1.1
*/
ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext);
ActionProxy createActionProxy(String namespace, String actionName, String methodName, StrutsContext extraContext);

/**
* Creates an {@link ActionProxy} for the given namespace and action name by looking up the configuration.The ActionProxy
Expand All @@ -63,10 +63,10 @@ public interface ActionProxyFactory {
* @return ActionProxy the created action proxy
* @since 2.1.1
*/
ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext);
ActionProxy createActionProxy(String namespace, String actionName, String methodName, StrutsContext extraContext, boolean executeResult, boolean cleanupContext);


/**
/**
* Creates an {@link ActionProxy} for the given namespace and action name by looking up the configuration.The ActionProxy
* should be fully initialized when it is returned, including passed {@link ActionInvocation} instance.
*
Expand All @@ -80,6 +80,6 @@ public interface ActionProxyFactory {
* @since 2.1.1
*/
ActionProxy createActionProxy(ActionInvocation actionInvocation, String namespace, String actionName, String methodName,
boolean executeResult, boolean cleanupContext);
boolean executeResult, boolean cleanupContext);

}
24 changes: 15 additions & 9 deletions core/src/main/java/org/apache/struts2/DefaultActionInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.struts2.interceptor.PreResultListener;
import org.apache.struts2.interceptor.WithLazyParams;
import org.apache.struts2.ognl.OgnlUtil;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.struts2.result.ActionChainResult;
import org.apache.struts2.result.Result;
import org.apache.struts2.util.ValueStack;
Expand All @@ -60,7 +61,7 @@ public class DefaultActionInvocation implements ActionInvocation {
protected Object action;
protected ActionProxy proxy;
protected List<PreResultListener> preResultListeners;
protected Map<String, Object> extraContext;
protected StrutsContext extraContext;
protected ActionContext invocationContext;
protected Iterator<InterceptorMapping> interceptors;
protected ValueStack stack;
Expand All @@ -79,7 +80,7 @@ public class DefaultActionInvocation implements ActionInvocation {
protected Callable<?> asyncAction;
protected WithLazyParams.LazyParamInjector lazyParamInjector;

public DefaultActionInvocation(final Map<String, Object> extraContext, final boolean pushAction) {
public DefaultActionInvocation(final StrutsContext extraContext, final boolean pushAction) {
this.extraContext = extraContext;
this.pushAction = pushAction;
}
Expand Down Expand Up @@ -234,7 +235,7 @@ public Result createResult() throws Exception {

if (resultConfig != null) {
try {
return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
return objectFactory.buildResult(resultConfig, invocationContext.getStrutsContext());
} catch (Exception e) {
LOG.error("There was an exception while instantiating the result of type {}", resultConfig.getClassName(), e);
throw new StrutsException(e, resultConfig);
Expand Down Expand Up @@ -320,10 +321,10 @@ public String invokeActionOnly() throws Exception {
return invokeAction(getAction(), proxy.getConfig());
}

protected void createAction(Map<String, Object> contextMap) {
protected void createAction(StrutsContext context) {
// load action
try {
action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);
action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), context);
} catch (InstantiationException e) {
throw new StrutsException("Unable to instantiate Action!", e, proxy.getConfig());
} catch (IllegalAccessException e) {
Expand All @@ -350,8 +351,13 @@ protected void createAction(Map<String, Object> contextMap) {
}
}

@Deprecated
protected Map<String, Object> createContextMap() {
ActionContext actionContext;
return createStrutsContext();
}

protected StrutsContext createStrutsContext() {
StrutsContext context;

if (ActionContext.containsValueStack(extraContext)) {
// In case the ValueStack was passed in
Expand All @@ -368,13 +374,13 @@ protected Map<String, Object> createContextMap() {

// create the action context
}
actionContext = stack.getActionContext();
ActionContext actionContext = stack.getActionContext();

return actionContext
.withExtraContext(extraContext)
.withActionInvocation(this)
.withContainer(container)
.getContextMap();
.getStrutsContext();
}

/**
Expand All @@ -400,7 +406,7 @@ private void executeResult() throws Exception {
@Override
public void init(ActionProxy proxy) {
this.proxy = proxy;
Map<String, Object> contextMap = createContextMap();
StrutsContext contextMap = createStrutsContext();

// Setting this so that other classes, like object factories, can use the ActionProxy and other
// contextual information to operate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

import org.apache.struts2.inject.Container;
import org.apache.struts2.inject.Inject;

import java.util.Map;
import org.apache.struts2.ognl.StrutsContext;

/**
* Default factory for {@link org.apache.struts2.ActionProxyFactory}.
Expand All @@ -41,27 +40,28 @@ public void setContainer(Container container) {
this.container = container;
}

public ActionProxy createActionProxy(String namespace, String actionName, Map<String, Object> extraContext) {
public ActionProxy createActionProxy(String namespace, String actionName, StrutsContext extraContext) {
return createActionProxy(namespace, actionName, null, extraContext, true, true);
}

public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext) {
@Override
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, StrutsContext extraContext) {
return createActionProxy(namespace, actionName, methodName, extraContext, true, true);
}

public ActionProxy createActionProxy(String namespace, String actionName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) {
public ActionProxy createActionProxy(String namespace, String actionName, StrutsContext extraContext, boolean executeResult, boolean cleanupContext) {
return createActionProxy(namespace, actionName, null, extraContext, executeResult, cleanupContext);
}

@Override
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) {
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, StrutsContext extraContext, boolean executeResult, boolean cleanupContext) {

ActionInvocation inv = createActionInvocation(extraContext, true);
container.inject(inv);
return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
}

protected ActionInvocation createActionInvocation(Map<String, Object> extraContext, boolean pushAction) {
protected ActionInvocation createActionInvocation(StrutsContext extraContext, boolean pushAction) {
return new DefaultActionInvocation(extraContext, pushAction);
}

Expand Down
Loading
Loading