package task.z06.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Base64;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import task.z04.DataSourceManager;
import task.z04.entity.AppUser;
import task.z04.exception.AuthorizationException;
import task.z04.exception.DBStructuralIntegrityException;
import task.z06.DoubleTuple;

public class AuthorizedServlet extends AbstractServlet {

	public static final String AUTHORIZATION_SERVLET = "authServlet";
	
	private DoubleTuple<String, String> extractCredentials(HttpServletRequest request) {
		String encodedValue =  request.getParameter("input-cred");
		if (encodedValue == null || encodedValue.isEmpty())
			return null;
		byte[] decodedValue = Base64.getDecoder().decode(encodedValue);
		String decoded = new String(decodedValue);
		String login = decoded.substring(0, decoded.indexOf(":"));
		String password = decoded.substring(decoded.indexOf(":") + 1);
		DoubleTuple<String, String> credentials = new DoubleTuple<String, String>(login, password);

		return credentials;
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html; charset=UTF-8");
		HttpSession session = req.getSession();
	    if (session.getAttribute("dbauth") == null) {
	    	DoubleTuple<String, String> credentials = extractCredentials(req);
	    	if (credentials == null)
	    		seeOther(resp, MainServlet.MAIN_SERVLET);
	    	try {
	    		//    
	    		DoubleTuple<DataSource, AppUser> loginData = DataSourceManager.tryLogIn(credentials.first, credentials.second);
	    		session.setAttribute("dbauth", loginData);
	    	} catch (SQLException | DBStructuralIntegrityException | AuthorizationException e) {
	    		PrintWriter writer = resp.getWriter();
	    		e.printStackTrace(writer);
	    	}
	    }
		seeOther(resp, MainServlet.MAIN_SERVLET);	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		seeOther(resp, MainServlet.MAIN_SERVLET);	
	}
	
	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
	}

	private static final long serialVersionUID = -3988422067060656591L;
}

@@ -1700,19 +1700,40 @@
    %09%09// 
-   
+%D0%9E%D1%81%D1%83%D1%89%D0%B5%D1%81%D1%82%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5 %D0%B2%D1%85%D0%BE%D0%B4%D0%B0 %D0%B2 %D0%91%D0%94
 %0A%09    %09%09

Update AuthorizedServlet.java