package at.gv.egiz.simpleSigning; import java.io.IOException; import java.io.OutputStream; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.simpleSigning.helper.SessionHelper; @WebServlet("/Error") public class ErrorSignature extends HttpServlet { /** * */ private static final long serialVersionUID = 7338000172211889255L; public static final String PARAM_ERROR = "error"; public static final String PARAM_CAUSE = "cause"; private static final Logger logger = LoggerFactory .getLogger(ErrorSignature.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doProcess(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doProcess(req, resp); } protected void doProcess(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { logger.info("[" + req.getSession().getId() + "]: error Signature"); String error = req.getParameter(PARAM_ERROR); if (error == null) { if (req.getAttribute(PARAM_ERROR) != null) { error = req.getAttribute(PARAM_ERROR).toString(); } } if(error != null) { error = URLDecoder.decode(error, "UTF-8"); } if (error == null) { error = "UNKNOWN ERROR"; } logger.info("[" + req.getSession().getId() + "]: Error: {}", error); String cause = req.getParameter(PARAM_CAUSE); if (cause == null) { if (req.getAttribute(PARAM_CAUSE) != null) { cause = req.getAttribute(PARAM_CAUSE).toString(); } } if(cause != null) { cause = URLDecoder.decode(cause, "UTF-8"); } if (cause == null) { cause = "UNKNOWN CAUSE"; } logger.info("[" + req.getSession().getId() + "]: Cause: {}", cause); String errorJS = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/js/error_response.js"))); String targetURL = SessionHelper.getTargetURL(req); if (targetURL == null) { targetURL = "invalidURL"; } logger.info("[" + req.getSession().getId() + "]: TargetURL: {}", targetURL); errorJS = errorJS.replace("##REPLACE##ERROR##", error); errorJS = errorJS.replace("##REPLACE##CAUSE##", cause); errorJS = errorJS.replace("##REPLACE##TARGETURL##", targetURL); String postman = FileUtils .readFileToString(FileUtils.toFile(PDFASJsServlet.class .getResource("/js/postman.js"))); String combined = postman + errorJS; String html = FileUtils.readFileToString(FileUtils .toFile(PDFASJsServlet.class .getResource("/html/script_exec.html"))); html = html.replace("##REPLACE##SCRIPT##", combined); SessionHelper.killSession(req); resp.setContentType("text/html"); OutputStream os = resp.getOutputStream(); os.write(html.getBytes()); os.close(); } catch (Throwable e) { logger.error("[" + req.getSession().getId() + "]: failed to finish error signature", e); throw new ServletException("Generic Exception"); } } }