This issues comes up when executing a particular workflow –
- User is logged in
- Session expires
- User tries to do something on the currently page which triggers an AJAX request
- Since session is expired the ajax request fails
- I need to display a message to this effect.
Now the problem appears. I have an interceptor which properly catches that the session expires. It also properly identifies that the original request was AJAX. So this means the interceptor needs to respond with a json response (accepted input format to any of my ajax calls).
Interceptor Code –
public class AuthenticationInterceptor implements Interceptor {
private InputStream jsonStream;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
... // boilerplate init code.
if (memberId == null
&& StringUtils.equals("XMLHttpRequest", request.getHeader("X-Requested-With"))) {
Map<String, Object> responseObject = Maps.newHashMap();
Set<String> errors = Sets.newHashSet();
errors.add("Your session has expired. Please refresh page and login.");
responseObject.put("errors", errors);
responseObject.put("success", false);
jsonStream = ResponseUtils.getJSONResponseStream(responseObject);
return "json";
}
}
public InputStream getJsonStream() {
return jsonStream;
}
public void setJsonStream(InputStream jsonStream) {
this.jsonStream = jsonStream;
}
struts.xml code –
<package name="raisin" namespace="/raisin" extends="secure">
<interceptors>
<interceptor name="authentication" class="interceptors.AuthenticationInterceptor"/>
</interceptors>
<global-results>
<result name="json" type="stream">
<param name="contentType">application/json</param>
<param name="inputName">jsonStream</param>
<param name="bufferSize">1024</param>
</result>
</global-results>
...
</package>
So when this workflow is executed I get the below exception –
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [jsonStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
I do not understand how is this possible since I create public get/set methods inthe interceptor for jsonStream. Also I put a sysout in getJsonStream method. It turns out it is never called.
Any pointers appreciated.
i believe you approach is wrong here and you are not using Interceptors the way they should be used.
Interceptors instances are shared among the Actions.For each request a new instance of action will be created by S2 but the Interceptors will be reused, which means Interceptors are stateless and you are trying to set state of request process in the interceptor.
My suggestion is Don’t try to store the data related to request processing as its not part of Interceptor role.
Just check the Ajax request response and try to see what result you are getting back, based on that you can show the error message.
Update
One of the possible solution to check session timeout handling.This has been discussed in following thread struts-2-session-timeout