Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access session data in servlet filter on app engine dev server

I was trying to write a session filter to centralize authorization check in my app engine web app.

My filter looks like this :

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest) request;
    HttpServletResponse httpResp = (HttpServletResponse) request;
    HttpSession session = httpReq.getSession();

    httpReq.setCharacterEncoding("UTF-8");
    httpResp.setCharacterEncoding("UTF-8");
    httpResp.setContentType("application/json");

    Boolean isAuthorized = (Boolean) session.getAttribute("isAuthorized");

    if (!isAuthorized || isAuthorized == null) {
        httpResp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    } else {
        chain.doFilter(request, response);
    }
}

But when I'm launching app engine dev server I'm greeted with this exception : "org.mortbay.jetty.request cannot be cast to javax.servlet.http.httpservletresponse". What am I doing wrong?

like image 769
NewlessClubie Avatar asked Dec 06 '22 21:12

NewlessClubie


1 Answers

You are trying to cast "request" object as response.

change as follows

HttpServletResponse httpResp = (HttpServletResponse) response;
like image 100
kensen john Avatar answered Dec 10 '22 11:12

kensen john