/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package anyframe.sample.struts.web.action;

import javax.security.auth.Subject;
import javax.security.auth.login.FailedLoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ExceptionConfig;

import anyframe.sample.struts.services.AuthenticationService;
import anyframe.sample.struts.services.UserVO;
import anyframe.sample.struts.web.form.UserForm;
import anyframe.web.struts.action.AbstractActionSupport;

public class LoginAction extends AbstractActionSupport{
	
	public Log getLogger() throws Exception {
		return LogFactory.getLog(this.getClass().getName());
	}
	
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			AuthenticationService authenticationService = (AuthenticationService) getService("authenticationService");

			UserForm userForm = (UserForm) form;
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(userVO, userForm);

			Subject subject = authenticationService.authenticate(userVO);

			HttpSession session = request.getSession();

			session.setAttribute("subject", subject);
			
		return (mapping.findForward("success"));
	}
}

