19 package org.turro.elephant.snippet;
21 import java.io.IOException;
22 import java.net.URLDecoder;
23 import java.nio.charset.StandardCharsets;
24 import javax.servlet.ServletException;
25 import javax.servlet.annotation.WebServlet;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.apache.commons.io.IOUtils;
30 import org.turro.collections.KeyValueMap;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantApplication;
33 import org.turro.log.WebLoggers;
39 @WebServlet(name =
"Snippets", urlPatterns = {
"/snippet_/*"})
42 private void processJson(
Application application, String path, KeyValueMap map) {
43 Snippets.
service(path).ifPresent(snippet -> snippet.process(application, map));
46 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
47 KeyValueMap map =
new KeyValueMap();
48 if(hasJsonBody(request)) {
51 for(String key : request.getParameterMap().keySet()) {
52 map.put(key, URLDecoder.decode(request.getParameter(key), StandardCharsets.UTF_8));
58 protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
59 processRequest(request, response);
63 protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
64 processRequest(request, response);
69 return "Elephant Snippets";
72 private boolean hasJsonBody(HttpServletRequest request) {
73 return "application/json".equals(request.getHeader(
"Content-Type"));
76 private String readBody(HttpServletRequest request) {
78 return IOUtils.toString(request.getReader());
79 }
catch (IOException ex) {
80 WebLoggers.warning(SnippetServlet.class).exception(ex).message(
"Error reading request body").log();
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
static Optional< ISnippetService > service(String path)
static final String JSONPAR