1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package com.example.demo;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet( name = "welcomeServlet", value = {"/"} ) public class welcomeServlet extends HttpServlet { private static final String DEFAULT_NAME = "guest"; private static final String DEFAULT_WORD = "welcome"; private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String requestUri = request.getRequestURI(); String contextPath = request.getContextPath(); String pathWithinApp = requestUri.substring(contextPath.length()); if (this.shouldDelegate(pathWithinApp)) { this.delegateToDefaultResource(pathWithinApp, request, response); } else { String jsonPayload = request.getParameter("json"); String nameParam = request.getParameter("name"); String wordParam = request.getParameter("word"); String urlParam = request.getParameter("url"); if (this.isBlank(jsonPayload) && !this.isBlank(nameParam) && !this.isBlank(wordParam)) { ObjectNode composed = OBJECT_MAPPER.createObjectNode(); composed.put("name", nameParam); composed.put("word", wordParam); if (!this.isBlank(urlParam)) { composed.put("url", urlParam); }
jsonPayload = composed.toString(); }
if (this.isBlank(jsonPayload)) { response.sendRedirect(this.defaultRedirectTarget(request)); } else { try { User user = (User)OBJECT_MAPPER.readValue(jsonPayload, User.class); String name = user.getName(); String word = user.getWord(); String url = user.getUrl(); if (this.isBlank(name) || this.isBlank(word)) { response.sendRedirect(this.defaultRedirectTarget(request)); return; }
this.renderResponse(response, name, word, url); } catch (JsonProcessingException var14) { response.sendError(400, "Invalid JSON payload"); } catch (RuntimeException var15) { response.sendError(400, "Invalid user data"); }
} } }
private boolean shouldDelegate(String pathWithinApp) { return pathWithinApp != null && !pathWithinApp.isEmpty() && !"/".equals(pathWithinApp); }
private void delegateToDefaultResource(String pathWithinApp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher defaultDispatcher = this.getServletContext().getNamedDispatcher("default"); if (defaultDispatcher != null) { defaultDispatcher.forward(request, response); } else { request.getRequestDispatcher(pathWithinApp).forward(request, response); }
}
private void renderResponse(HttpServletResponse response, String name, String word, String url) throws IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
try { out.println("<html><body>"); String var10001 = this.escapeHtml(name); out.println("<h1>" + var10001 + "</h1>"); var10001 = this.escapeHtml(word); out.println("<p>" + var10001 + "</p>"); if (!this.isBlank(url)) { var10001 = this.escapeHtml(url); out.println("<p>URL: " + var10001 + "</p>"); }
out.println("</body></html>"); } catch (Throwable var9) { if (out != null) { try { out.close(); } catch (Throwable var8) { var9.addSuppressed(var8); } }
throw var9; }
if (out != null) { out.close(); }
}
private String escapeHtml(String input) { return input == null ? "" : input.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'"); }
private String defaultRedirectTarget(HttpServletRequest request) { String var10000 = request.getContextPath(); return var10000 + "/?name=" + this.urlEncode("guest") + "&word=" + this.urlEncode("welcome"); }
private boolean isBlank(String value) { return value == null || value.trim().isEmpty(); }
private String urlEncode(String value) { return URLEncoder.encode(value, StandardCharsets.UTF_8); } }
|