2

I get the below exception after running my webportal for a while. I need to re login to eliminate this error. Because of this I am not able to run my development environment smoothly on eclipse.

This is my AuthenticationFilter.java

public class AuthenticationFilter implements Filter {
    private static final Logger log = Logger.getLogger(AuthenticationFilter.class) ;
    static Map<String, String> permissionsMapping = new HashMap<String, String>();
    private static int domainOrGroupParentId = 0;
    static {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest servletRequest = (HttpServletRequest) request;

        if (servletRequest.getRequestURI().contains("onLoadOfLoginPage")
                || servletRequest.getRequestURI().contains("Corporate")
                || servletRequest.getRequestURI().contains("mainDashboard")
                // ||
                // servletRequest.getRequestURI().contains("mainDashboardWithoutDomainOrGroupParent")
                || servletRequest.getRequestURI().contains("loginButtonPressedForSuperAdminView")
                // ||
                // servletRequest.getRequestURI().contains("loginButtonPressedForNonSuperAdmin")
                || servletRequest.getRequestURI().contains("resources")
                || servletRequest.getRequestURI().contains("session")
                || servletRequest.getRequestURI().contains("forgot_password")
                || servletRequest.getRequestURI().contains("resetPassword")) {
>>Line52>>      chain.doFilter(request, response);

        } else {
            HttpSession session = servletRequest.getSession(false);
            try {
                if (null == session || SessionDataManager.getInstance().getLoggedInWebAdminUser().getWebAdminUserPojo()
                        .getUsername() == null) {
                    RequestDispatcher dispatcher = request.getRequestDispatcher("onLoadOfLoginPage");
                    dispatcher.forward(request, response);
                    return;

                } else if (!servletRequest.getRequestURI().contains("get")) {

                    String username = (String) SessionDataManager.getInstance().getLoggedInWebAdminUser()
                            .getWebAdminUserPojo().getUsername();
                    String sessionID = session.getId();
                    domainOrGroupParentId = SessionDataManager.getInstance().getDomainIdFromRequest(servletRequest);
                    log.info("Attempting to maintain Session, with domainParent "+domainOrGroupParentId);
                    if (!WebAdminHelper.maintainSession(username, sessionID, domainOrGroupParentId)) {
                        RequestDispatcher dispatcher = request.getRequestDispatcher("onLoadOfLoginPage");
                        dispatcher.forward(request, response);
                        return;
                    }
                }
            } catch (Exception e) {

            }
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}

Exception I get

type Exception report

message Handler processing failed; nested exception is java.lang.StackOverflowError

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
    org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1276)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:958)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    net.codejava.spring.AuthenticationFilter.doFilter(AuthenticationFilter.java:52)
root cause

java.lang.StackOverflowError
    com.google.gson.stream.JsonWriter.beforeName(JsonWriter.java:586)
    com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:401)
    com.google.gson.stream.JsonWriter.value(JsonWriter.java:417)
    com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:362)
    com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:346)
    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:195)
    com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)

EDIT I have identified the root causing line.

response = new Gson().toJson(invException);

Type of

InvalidObjectStateException invException

public class InvalidObjectStateException extends Exception {
    private static final long serialVersionUID = -9086960433236073019L;
    private static final Logger log = Logger.getLogger(InvalidObjectStateException.class);
    private String customErrorMessage = null;
    private Object exceptionCausingObject = null;

    public InvalidObjectStateException(String errorMessage, Object exceptionCausingObject) {
        super(errorMessage);
        this.customErrorMessage = errorMessage;
        log.info("InvalidObjectStateException : exceptionCausingObject"
                + ((exceptionCausingObject != null) ? exceptionCausingObject.getClass().toString() : ""));
        this.exceptionCausingObject = exceptionCausingObject;
    }

    public String getCustomErrorMessage() {
        return customErrorMessage;
    }

    public void setCustomErrorMessage(String customErrorMessage) {
        this.customErrorMessage = customErrorMessage;
    }

    public Object getExceptionCausingObject() {
        return exceptionCausingObject;
    }

    public void setExceptionCausingObject(Object exceptionCausingObject) {
        this.exceptionCausingObject = exceptionCausingObject;
    }
}

What am I doing wrong here ? Why cant Gson parse the Exception ?

Siddharth
  • 8,842
  • 13
  • 79
  • 133

2 Answers2

2

I think this is not the right source of exception. net.codejava.spring.AuthenticationFilter.doFilter(AuthenticationFilter.java:52). It just failed at this location due to low memory.

There should be another process/request which is converting a response(maybe)/object to json string. Look in the response object it should have a nested infinite object reference.

Siddharth
  • 8,842
  • 13
  • 79
  • 133
Gaurav
  • 498
  • 4
  • 7
  • ok, yeah that sounds correct. the response object may have a circular reference.. i'll look into that and report back – Siddharth Jun 05 '17 at 10:51
  • I have found the root cause.. I wonder if that helps you answer itbetter – Siddharth Jun 08 '17 at 13:15
  • Not really! You need to find out the object type is in this Exception class - Object exceptionCausingObject. Maybe log the class name when the object is created. – Gaurav Jun 09 '17 at 04:49
  • I'm a little distressed seeing a `Logger` in the exception. Maybe the logger's implementation has a reference to the `InvalidObjectStateException`class (which has a reference to the logger) -> circular reference. An easy fix would be to alter the json serialization of the exception (maybe only marshall message & stacktrace) – Jeremy Grand Jun 09 '17 at 13:23
2

The problem in the Object exceptionCausingObject

It has circular references so Gson parser cannot parse it correctly and throws StackOverflowError

You can mark some fields of exceptionCausingObject classes as transient

private transient String name;

That will exclude them from Gson serialization.

Another option is to use GraphAdapterBuilder. See here

Mike Adamenko
  • 2,727
  • 1
  • 11
  • 24