In Struts2, Interceptors receive an exception from an instance variable that gets / sets correctly in an interceptor

These issues occur when performing a specific workflow -

  • User logged in
  • Session ends
  • The user is trying to do something on the current page that triggers an AJAX request
  • Since the session has expired, the ajax request is not working
  • I need to show a message about this.

Now the problem arises. I have an interceptor that correctly catches that the session expires. It also correctly detects that the original request was AJAX. So this means that the interceptor should respond with a json response (the accepted input format for 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 following exception -

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [jsonStream] in the invocation stack. Check the &lt;param name=&quot;inputName&quot;&gt; 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 don't understand how this is possible, as I am creating public get / set methods in the interceptor for jsonStream

. Also I put sysout in a method getJsonStream

. It turns out he is never called.

Any pointers appreciated.

+3


source to share


1 answer


I believe you are wrong and you are not using Interceptors the way they should be used. Interceptor instances are shared between Actions. For each request, a new instance of the action will be created by S2, but the Interceptors will be reused, which means the Interceptors are stateless and you are trying to set the state of the request process in the interceptor.

My suggestion: Don't try to store request processing data as non-role Interceptor. Just check the response of the Ajax request and try to figure out what kind of result you get back, based on which you can show an error message.



Update

One possible solution to check the session timeout handling. This was discussed in the following thread struts-2-session-timeout

+3


source







All Articles