/**
 * Created on 2006-10-7 上午12:05:13
 */
package cn.net.openid.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import cn.net.openid.User;
import cn.net.openid.dao.DaoFacade;

/**
 * @author Shutra
 * 
 */
public class LoginController extends SimpleFormController {
	@SuppressWarnings("unused")
	private DaoFacade daoFacade;

	private User check(LoginForm lf) {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.BaseCommandController#onBindAndValidate(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void onBindAndValidate(HttpServletRequest request,
			Object command, BindException errors) throws Exception {
		LoginForm lf = (LoginForm) command;
		User user = this.check(lf);
		if (user == null) {
			errors.rejectValue("username", "error.login.failed");
		} else {
			HttpSession session = request.getSession();
			UserSession userSession = new UserSession(user);
			userSession.setLoggedIn(true);
			userSession.setOpenidUrl(this.daoFacade.buildOpenidUrl(lf
					.getUsername()));
			session.setAttribute("userSession", userSession);

			session.setAttribute("cn.net.openid.username", lf.getUsername()
					.toLowerCase());
			session.setAttribute("cn.net.openid.identity", this.daoFacade
					.buildOpenidUrl(lf.getUsername()));
		}
		super.onBindAndValidate(request, command, errors);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		HttpSession session = request.getSession();

		Map<String, String[]> pm = (Map<String, String[]>) request.getSession()
				.getAttribute("parameterMap");
		if (pm != null) {
			return null;
		} else {
			if (session.getAttribute("parameterlist") == null) {
				return super.onSubmit(request, response, command, errors);
			} else {
				response.sendRedirect("provider-authorization");
				return null;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.Errors)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		LoginForm form = (LoginForm) command;

		if (StringUtils.isEmpty(form.getUsername())) {
			form.setUsername(request.getParameter("username"));
		}

		HttpSession session = request.getSession();

		Map<String, String[]> parameterMap = (Map<String, String[]>) session
				.getAttribute("parameterMap");
		if (parameterMap != null) {
		}

		if (StringUtils.isEmpty(form.getUsername())) {
		}

		return super.referenceData(request, command, errors);
	}

	/**
	 * @param daoFacade
	 *            the daoFacade to set
	 */
	public void setDaoFacade(DaoFacade daoFacade) {
		this.daoFacade = daoFacade;
	}
}

@@ -64,16 +64,39 @@
 d.web;%0A%0A
+import java.util.List;%0A
 import j
@@ -287,16 +287,101 @@
 gUtils;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -590,16 +590,49 @@
 oller;%0A%0A
+import cn.net.openid.Credential;%0A
 import c
@@ -707,13 +707,17 @@
 or S
-h
 utra
+ Zhou
 %0A * 
@@ -786,102 +786,481 @@
  %7B%0A%09
-@SuppressWarnings(%22unused%22)%0A%09private DaoFacade daoFacade;%0A%0A%09private User check(LoginForm lf) %7B
+private static final Log log = LogFactory.getLog(LoginController.class);%0A%0A%09private DaoFacade daoFacade;%0A%0A%09private User check(LoginForm lf) %7B%0A%09%09User user = daoFacade.getUserByUsername(lf.getUsername());%0A%09%09List%3CCredential%3E credentials = daoFacade.getCredentials(user.getId());%0A%09%09for (Credential c : credentials) %7B%0A%09%09%09log.debug(%22Password: %22 + new String(c.getInfo()));%0A%09%09%09if (new String(c.getInfo()).equals(new String(lf.getPassword().getBytes()))) %7B%0A%09%09%09%09return user;%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%09r

Fix login controller.