public void doService(Request req, Response res)
throws Exception
{
Context ctx=req.getContext();
ServerSession session=req.getSession( false );
// we didn't had a session
boolean noSession= ( session==null );
if( debug>0 ) log( "Form handler called with no session ");
String page=ctx.getFormLoginPage();
String errorPage=ctx.getFormErrorPage();
// assert errorPage!=null ( AccessInterceptor will check
// that and enable form login only if everything is ok
session=(ServerSession)req.getSession( true );
String username=(String)session.getAttribute( "j_username" );
if( debug>0) log( "Username = " + username);
String originalLocation = req.requestURI().toString();
//XXX is needed to put the JVM route too?
if (noSession
|| Request.SESSIONID_FROM_URL.equals(req.getSessionIdSource())) {
// If new session we have no way to know if cookies are supported
String id=";jsessionid="+req.getSessionId() ;
originalLocation += id ;
page += id ;
}
if (req.queryString().toString() != null
&& !req.queryString().toString().equals(""))
originalLocation += "?" + req.queryString().toString();
session.setAttribute( "tomcat.auth.originalLocation",
originalLocation);
if( username != null ) {
// 401 with existing j_username - that means wrong credentials.
// Next time we'll have a fresh start
session.removeAttribute( "j_username");
session.removeAttribute( "j_password");
req.setAttribute("javax.servlet.error.message",
errorPage );
if( debug>0) log( "Redirecting to " + errorPage );
contextM.handleStatus( req, res, 302 ); // redirect
return;