package com.jolocom.webidproxy;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jolocom.webidproxy.email.ForgotPasswordEmail;
import com.jolocom.webidproxy.users.User;

public class ForgotPasswordServlet extends BaseServlet {

	private static final long serialVersionUID = 3793048689633131588L;

	private static final Log log = LogFactory.getLog(ForgotPasswordServlet.class);

	private static final int RECOVERYCODE_LENGTH = 8;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("username");

		User user = WebIDProxyServlet.users.get(username);

		// create recovery code

		if (user != null) {

			String recoverycode = generateRecoverycode();
			user.setRecoverycode(recoverycode);
			WebIDProxyServlet.users.put(user);
		}

		log.debug("User " + username + " forgot password, created recovery code.");

		// send e-mail

		ForgotPasswordEmail email = new ForgotPasswordEmail(user);

		try {

			email.send();
		} catch (MessagingException ex) {

			throw new ServletException("Cannot send e-mail: " + ex.getMessage(), ex);
		}

		// done

		String content = "{}";

		this.success(request, response, content, "application/json");
	}

	private static String generateRecoverycode() {

		return RandomStringUtils.randomAlphanumeric(RECOVERYCODE_LENGTH);
	}
}

@@ -1001,61 +1001,186 @@
 %0D%0A%09%09
-// create recovery code%0D%0A%0D%0A%09%09if (user != null) %7B
+if (user == null) %7B%0D%0A%0D%0A%09%09%09this.error(request, response, HttpServletResponse.SC_BAD_REQUEST, %22User %22 + username + %22 not found.%22);%0D%0A%09%09%09return;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09// create recovery code
 %0D%0A%0D%0A
-%09
 %09%09St
@@ -1226,17 +1226,16 @@
 e();%0D%0A%09%09
-%09
 user.set
@@ -1263,17 +1263,16 @@
 code);%0D%0A
-%09
 %09%09WebIDP
@@ -1300,21 +1300,16 @@
 (user);%0D
-%0A%09%09%7D%0D
 %0A%0D%0A%09%09log

return error if user is not found