0

I am currently working on an organization's framework to migrate it to Struts 2 from Webworks using IBM WebSphere 7. I followed the steps listed below:

1) Imported the struts2-core-2.3.16.jar along with other dependencies JAR.

2) Renamed xwork.xml to struts.xml and changed the setting in web.xml to use StrutsPrepareAndExecuteFilter.

3)Replaced webwork JAR code with struts2 code and xwork code to xwork2 code respectively in the Interceptors and Controllers.

The server starts up fine and the deployment takes place fine. When I start the Application, I get a NullPointerException which happens because the code in one of the Interceptors tries to retrieve the session information and gets a null response. I debugged it and saw that the session information is not available in the ActionContext or the ActionInvocation instance. What could be the possible reasons for this?

Stack trace: (For confidentiality purposes, I have hidden the name of the organization)

java.lang.NullPointerException 
    com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.getBusinessServicesString(AbstractAuthorizationInterceptor.java:57)
    com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.initializeBusinessServices(AbstractAuthorizationInterceptor.java:63)
    com.somecompany.merchandiseplanning.controller.AbstractAuthorizationInterceptor.intercept(AbstractAuthorizationInterceptor.java:35)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:188)
    com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:116)
    com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:77)
    com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
    com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:997)
    com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.invokeFilters(DefaultExtensionProcessor.java:1043)
    com.ibm.ws.webcontainer.extension.DefaultExtensionProcessor.handleRequest(DefaultExtensionProcessor.java:963)
    com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)
    com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
    com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
    com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)
    com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
    com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:445)
    com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:504)
    com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:301)
    com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:275)
    com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)

The piece of code that is enclosed in the error stack and which tries to retrieve the session is :

private String getBusinessServicesString(){

Map session = ActionContext.getContext().getSession();  //Error : No session retrived

UserInfo userInfo = (UserInfo) session.get("userinfo");
return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
    }

I tried replacing it with

Map session = actionInvocation.getInvocationContext().getSession();

with actionInvocation being an instance of ActionInvocation passed in the intercept method of the Interceptor but I get the same null response there.

Any help will be much appreciated!

Gas
  • 17,601
  • 4
  • 46
  • 93
  • This needs some more details - what is `ActionContext` for starters... – vikramls Nov 20 '14 at 18:34
  • Hi, ActionContext is a core feature of struts2 in which certain information like the session are stored. You may refer to the following documentation link for more clarification: http://struts.apache.org/release/2.0.x/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html – NAMIT MOHAN BARMAN Nov 20 '14 at 18:38
  • Session may not be available, what are you trying to get at? – BillFromHawaii Nov 20 '14 at 19:03
  • Well, previously this whole framework was on Webworks and the application was working fine. Since I have tried to migrate it to struts2 from the corresponding Webworks, I am afraid I might have missed something because of which I am not able to get the session information from the ActionContext, which is throwing the NullPointerException. I don't have much of an understanding how Webworks might have set the session information and if, by migrating it to struts 2, I unintentionally might have deleted it. – NAMIT MOHAN BARMAN Nov 20 '14 at 19:09
  • Just to add, I have to login into a portal to get to this Application and then start it. The login works fine. When I try to start the Application, the whole exception is thrown. – NAMIT MOHAN BARMAN Nov 20 '14 at 19:14
  • If memory serves, the session is set by an interceptor in the stack so that it is available to action classes that are SessionAware. If you have put your interceptor in front of that in the stack it may not be available. I think its the CreateSession interceptor but don't hold me to it. – BillFromHawaii Nov 20 '14 at 19:24
  • The problem in your interceptor that you getting a session from non-existed action context that doesn't have a session information. The scope of the local objects is thread. If you need a session scope then you should implement a scope strategy like in [this](http://stackoverflow.com/a/17394920/573032) answer. – Roman C Nov 20 '14 at 20:31
  • Did you get an answer to this? I am having the same problem in my interceptor when migrating to WAS 8.5. But this code works fine on Tomcat. – dev4life Nov 11 '15 at 16:43

1 Answers1

0

You will get session from ActionContext in the action class only when the action is initiated by the struts2 filter.

In this case implement SessionAware interface in your class(containing method getBusinessServicesString()) and use the session attribute of the interface to get session. Suppose ABC is your action class then it can be done as below.

public class ABC extends ActionSupport implements SessionAware {

    private Map session;

 public void setSession(Map session) {
  this.session = session;
 }

 public Map getSession() {
  return session;
 }

    private String getBusinessServicesString(){
      session = ActionContext.getContext().getSession();  
      UserInfo userInfo = (UserInfo) session.get("userinfo");
      return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
    }

}
BK Elizabeth
  • 479
  • 5
  • 15