BrightSide Workbench Full Report + Source Code
SnippetServlet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant.snippet;
20 
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;
34 
39 @WebServlet(name = "Snippets", urlPatterns = {"/snippet_/*"})
40 public class SnippetServlet extends HttpServlet {
41 
42  private void processJson(Application application, String path, KeyValueMap map) {
43  Snippets.service(path).ifPresent(snippet -> snippet.process(application, map));
44  }
45 
46  protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47  KeyValueMap map = new KeyValueMap();
48  if(hasJsonBody(request)) {
49  map.put(Snippets.JSONPAR, readBody(request));
50  }
51  for(String key : request.getParameterMap().keySet()) {
52  map.put(key, URLDecoder.decode(request.getParameter(key), StandardCharsets.UTF_8));
53  }
54  processJson(new ElephantApplication(request, response), request.getPathInfo(), map);
55  }
56 
57  @Override
58  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
59  processRequest(request, response);
60  }
61 
62  @Override
63  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64  processRequest(request, response);
65  }
66 
67  @Override
68  public String getServletInfo() {
69  return "Elephant Snippets";
70  }
71 
72  private boolean hasJsonBody(HttpServletRequest request) {
73  return "application/json".equals(request.getHeader("Content-Type"));
74  }
75 
76  private String readBody(HttpServletRequest request) {
77  try {
78  return IOUtils.toString(request.getReader());
79  } catch (IOException ex) {
80  WebLoggers.warning(SnippetServlet.class).exception(ex).message("Error reading request body").log();
81  }
82  return null;
83  }
84 
85 }
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doGet(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
static Optional< ISnippetService > service(String path)
Definition: Snippets.java:36