Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: how to access (session) Manager from servlet

I need to access the Manager from the servlet (or filter) in Tomcat to load the custom session by custom session ID.

Answering your next question: why do I need it. There's an old bug in Flash that causes it to send cookies from IE and not from the current browser. So, if I'm in FF and I'm trying to upload the file with SWFUpload I end up with the wrong session and an error.

I want to add the magic parameter to POST that should override the default (wrong) session id, then load the custom session instead of one loaded by Tomcat. I can't use URL rewriting since cookies are resolved first, and when flash sends wrong cookie from IE, Tomcat doesn't try to load the session from url-rewritten address.

I'd appreciate any other hint how to access Manager from context or a solution of the original problem.

Thanks in advance, Juriy

like image 721
Juriy Avatar asked Apr 27 '11 16:04

Juriy


1 Answers

for Tomcat:

   ApplicationContextFacade appContextFacadeObj = (ApplicationContextFacade)    request.getSession().getServletContext();

    try
    {
        Field applicationContextField = appContextFacadeObj.getClass().getDeclaredField("context");
        applicationContextField.setAccessible(true);
        ApplicationContext appContextObj = (ApplicationContext) applicationContextField.get(appContextFacadeObj);
        Field standardContextField = appContextObj.getClass().getDeclaredField("context");
        standardContextField.setAccessible(true);
        StandardContext standardContextObj = (StandardContext) standardContextField.get(appContextObj);
        Manager persistenceManager = standardContextObj.getManager();
    }
    catch(SecurityException e)
    {
        logger.error(e);
    }
    catch(NoSuchFieldException e)
    {
        logger.error(e);
    }
    catch(IllegalArgumentException e)
    {
        logger.error(e);
    }
    catch(IllegalAccessException e)
    {
        logger.error(e);
    }
like image 70
Ihor M. Avatar answered Sep 17 '22 23:09

Ihor M.