package yanagishima.servlet;

import yanagishima.config.YanagishimaConfig;
import yanagishima.util.AccessControlUtil;
import yanagishima.util.HttpRequestUtil;
import yanagishima.util.YarnUtil;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;

import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;

@Singleton
public class HiveQueryDetailServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private YanagishimaConfig yanagishimaConfig;

    @Inject
    public HiveQueryDetailServlet(YanagishimaConfig yanagishimaConfig) {
        this.yanagishimaConfig = yanagishimaConfig;
    }

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

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

        String datasource = HttpRequestUtil.getParam(request, "datasource");
        if (yanagishimaConfig.isCheckDatasource()) {
            if (!AccessControlUtil.validateDatasource(request, datasource)) {
                try {
                    response.sendError(SC_FORBIDDEN);
                    return;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        String resourceManagerUrl = yanagishimaConfig.getResourceManagerUrl(datasource).get();
        Optional<String> idOptinal = Optional.ofNullable(request.getParameter("id"));
        idOptinal.ifPresent(id -> {
            if (id.startsWith("application_")) {
                try {
                    response.sendRedirect(resourceManagerUrl + "/cluster/app/" + id);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                String userName = null;
                Optional<String> hiveUser = Optional.ofNullable(request.getParameter("user"));
                Optional<String> hivePassword = Optional.ofNullable(request.getParameter("password"));
                if(yanagishimaConfig.isUseAuditHttpHeaderName()) {
                    userName = request.getHeader(yanagishimaConfig.getAuditHttpHeaderName());
                } else {
                    if (hiveUser.isPresent() && hivePassword.isPresent()) {
                        userName = hiveUser.get();
                    }
                }
                Optional<Map> applicationOptional = YarnUtil.getApplication(resourceManagerUrl, id, userName, yanagishimaConfig.getResourceManagerBegin(datasource));
                applicationOptional.ifPresent(application -> {
                    String applicationId = (String) application.get("id");
                    try {
                        response.sendRedirect(resourceManagerUrl + "/cluster/app/" + applicationId);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        });
    }

}

@@ -1045,212 +1045,8 @@
 on %7B
-%0A        doPost(request, response);%0A    %7D%0A%0A    @Override%0A    protected void doPost(HttpServletRequest request,%0A                         HttpServletResponse response) throws ServletException, IOException %7B
 %0A%0A  
@@ -2135,111 +2135,8 @@
 ));%0A
-                Optional%3CString%3E hivePassword = Optional.ofNullable(request.getParameter(%22password%22));%0A
     
@@ -2365,36 +2365,8 @@
 nt()
- && hivePassword.isPresent()
 ) %7B%0A

use GET