1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.blackbox.joran.spi;
16
17 import jakarta.servlet.ServletException;
18 import jakarta.servlet.http.HttpServlet;
19 import jakarta.servlet.http.HttpServletRequest;
20 import jakarta.servlet.http.HttpServletResponse;
21
22 import java.io.IOException;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 public class ConfigFileServlet extends HttpServlet {
28
29 Map<String, String> headers = new HashMap<String, String>();
30 static final String DEFAULT_CONTENT = "That was all";
31 String contents;
32 static final String LAST_MODIFIED = "last-modified";
33 public final static String CONTENT_KEY = "content";
34
35 public ConfigFileServlet(String contents) {
36 this.contents = contents;
37 }
38
39 public ConfigFileServlet() {
40 this(DEFAULT_CONTENT);
41 }
42
43
44
45
46
47
48
49
50 @Override
51 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
52 response.setContentType("text/txt;charset=utf-8");
53
54 String lastModifiedHeaderValue = headers.get(LAST_MODIFIED);
55 if(lastModifiedHeaderValue != null) {
56 response.setHeader(LAST_MODIFIED, lastModifiedHeaderValue);
57 }
58
59 response.setStatus(HttpServletResponse.SC_OK);
60 response.getWriter().println(contents);
61 }
62
63
64
65
66
67
68
69
70 @Override
71 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
72 Enumeration<String> paramNames = request.getParameterNames();
73 while (paramNames.hasMoreElements()) {
74 String paramName = paramNames.nextElement();
75 String[] paramValues = request.getParameterValues(paramName);
76
77 if (paramValues.length >= 1) {
78 if(CONTENT_KEY.equals(paramName)) {
79 contents = paramValues[0];
80 } else {
81 headers.put(paramName, paramValues[0]);
82 }
83 }
84 response.setStatus(HttpServletResponse.SC_OK);
85 response.getWriter().println(contents);
86 }
87
88
89 }
90
91 }