package com.oreilly.struts.storefront.security; import java.util.Locale; import javax.servlet.http.*; import org.apache.struts.action.*; import com.oreilly.struts.storefront.customer.view.UserView; import com.oreilly.struts.storefront.framework.exceptions.BaseException; import com.oreilly.struts.storefront.framework.UserContainer; import com.oreilly.struts.storefront.framework.StorefrontBaseAction; import com.oreilly.struts.storefront.framework.util.IConstants; import com.oreilly.struts.storefront.service.IStorefrontService; /** * Implements the logic to authenticate a user for the Storefront application. */ public class LoginAction extends StorefrontBaseAction { /** * Called by the controller when the user attempts to log in to the * Storefront application. */ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception{ // The email and password should have already been validated by the ActionForm String email = ((LoginForm)form).getEmail( ); String password = ((LoginForm)form).getPassword( ); // Log in through the security service IStorefrontService serviceImpl = getStorefrontService( ); UserView userView = serviceImpl.authenticate(email, password); // Create a single container object to store user data UserContainer existingContainer = null; HttpSession session = request.getSession(false); if ( session != null ){ existingContainer = getUserContainer(request); session.invalidate( ); }else{ existingContainer = new UserContainer( ); } // Create a new session for the user session = request.getSession(true); // Store the UserView in the container and store the container in the session existingContainer.setUserView(userView); session.setAttribute(IConstants.USER_CONTAINER_KEY, existingContainer); // Return a Success forward return mapping.findForward(IConstants.SUCCESS_KEY); } } |