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?
You are trying to cast "request" object as response.
change as follows
HttpServletResponse httpResp = (HttpServletResponse) response;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With