package edu.emory.cci.aiw.cvrg.eureka.servlet.worker.useracct;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

import edu.emory.cci.aiw.cvrg.eureka.common.comm.CommUtils;
import edu.emory.cci.aiw.cvrg.eureka.servlet.worker.ServletWorker;

public class SaveUserAcctWorker implements ServletWorker {
	
	private static Logger LOGGER = LoggerFactory.getLogger(SaveUserAcctWorker.class);
	
	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String eurekaServicesUrl = req.getSession().getServletContext()
				.getInitParameter("eureka-services-url");

		String id = req.getParameter("id");
		String oldPassword = req.getParameter("oldPassword");
		String newPassword = req.getParameter("newPassword");
		
		// TODO: validate verifyPassword equals newPassword
		String verifyPassword = req.getParameter("verifyPassword");
		
		Client c;
		try {
			c = CommUtils.getClient();
			LOGGER.debug("id = " + id);
			LOGGER.debug("old = " + oldPassword);
			LOGGER.debug("new = " + newPassword);

			WebResource webResource = c.resource(eurekaServicesUrl);
			
			ClientResponse response = webResource
					.path("/api/user/passwd/" + id)
					.queryParam("oldPassword", oldPassword)
					.queryParam("newPassword", newPassword)
					.get(ClientResponse.class);
			
			LOGGER.debug("status = " + response.getClientResponseStatus().getStatusCode());
			
			resp.setContentType("text/html");
			if (response.getClientResponseStatus().getStatusCode() == resp.SC_OK) {
				resp.setStatus(resp.SC_OK);				
			} else {
				resp.setStatus(resp.SC_BAD_REQUEST);								
			}
			resp.getWriter().write(response.getClientResponseStatus().getStatusCode());
			resp.getWriter().close();
		} catch (KeyManagementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//resp.sendRedirect(req.getContextPath() + "/protected/user_acct?action=list");
	}
}

@@ -1179,14 +1179,8 @@
 %09%09//
- TODO:
  val
@@ -1287,169 +1287,219 @@
 ;%0A%09%09
-%0A%09%09Client c;%0A%09%09try %7B%0A%09%09%09c = CommUtils.getClient();%0A%09%09%09LOGGER.debug(%22id = %22 + id);%0A%09%09%09LOGGER.debug(%22old = %22 + oldPassword);%0A%09%09%09LOGGER.debug(%22new = %22 + newPassword
+if (!verifyPassword.equals(newPassword)) %7B%0A%09%09%09resp.setContentType(%22text/html%22);%0A%09%09%09resp.setStatus(resp.SC_BAD_REQUEST);%0A%09%09%09resp.getWriter().close();%0A%09%09%09return;%0A%09%09%7D%0A%09%09Client c;%0A%09%09try %7B%0A%09%09%09c = CommUtils.getClient(
 );%0A%0A

Validate the verifyPassword field.