Skip to content

Commit

Permalink
WW-4873 Makes InvocationSessionStore$InvocationContext transient
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserzamani committed Dec 14, 2017
1 parent 4738d1d commit 2941416
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.ValueStack;

import java.io.Serializable;
import java.util.HashMap;
Expand Down Expand Up @@ -56,10 +55,14 @@ public static ActionInvocation loadInvocation(String key, String token) {
return null;
}

ValueStack stack = invocationContext.invocation.getStack();
ActionContext.getContext().setValueStack(stack);
ActionInvocation savedInvocation = null;
if (invocationContext.invocation != null) {
savedInvocation = invocationContext.invocation;
ActionContext.setContext(savedInvocation.getInvocationContext());
ActionContext.getContext().setValueStack(savedInvocation.getStack());
}

return invocationContext.invocation;
return savedInvocation;
}

/**
Expand Down Expand Up @@ -109,7 +112,9 @@ private static class InvocationContext implements Serializable {

private static final long serialVersionUID = -286697666275777888L;

ActionInvocation invocation;
//WW-4873 transient since 2.5.15
transient ActionInvocation invocation;

String token;

public InvocationContext(ActionInvocation invocation, String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsInternalTestCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -40,7 +44,7 @@ public class InvocationSessionStoreTest extends StrutsInternalTestCase {


private ActionInvocation invocation;
private Map session;
private Map<String, Object> session;
private Mock invocationMock;
private ValueStack stack;

Expand All @@ -62,20 +66,53 @@ public void testValueStackReset() {
assertEquals(stack, actionContext.getValueStack());
}

public void testActionContextReset() {
ActionContext actionContext = ActionContext.getContext();
InvocationSessionStore.storeInvocation(INVOCATION_KEY, TOKEN_VALUE, invocation);

ActionContext actionContext2 = new ActionContext(new HashMap<String, Object>());
actionContext2.setSession(session);
ActionContext.setContext(actionContext2);
assertEquals(actionContext2, ActionContext.getContext());

InvocationSessionStore.loadInvocation(INVOCATION_KEY, TOKEN_VALUE);
assertEquals(actionContext, ActionContext.getContext());
}

public void testStoreAndLoadFromDeserializedSession() throws Exception {
InvocationSessionStore.storeInvocation(INVOCATION_KEY, TOKEN_VALUE, invocation);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(session);//WW-4873 invocation is not serializable but we should not fail at this line
oos.close();
byte b[] = baos.toByteArray();
baos.close();

ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
session = (Map) ois.readObject();
ActionContext.getContext().setSession(session);
ois.close();
bais.close();

ActionInvocation savedInvocation = InvocationSessionStore.loadInvocation(INVOCATION_KEY, TOKEN_VALUE);
assertNull(savedInvocation);//Currently we don't support invocation restore from serialized session
}

protected void setUp() throws Exception {
super.setUp();
stack = ActionContext.getContext().getValueStack();

ActionContext actionContext = new ActionContext(stack.getContext());
ActionContext.setContext(actionContext);

session = new HashMap();
session = new HashMap<>();
actionContext.setSession(session);

invocationMock = new Mock(ActionInvocation.class);
invocation = (ActionInvocation) invocationMock.proxy();
invocationMock.matchAndReturn("serialize", invocation);
invocationMock.matchAndReturn("deserialize", actionContext, invocation);
invocationMock.matchAndReturn("getInvocationContext", actionContext);

actionContext.setValueStack(stack);
invocationMock.matchAndReturn("getStack", stack);
Expand Down

0 comments on commit 2941416

Please sign in to comment.