1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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       * Returns data set when {@link #doPost(HttpServletRequest, HttpServletResponse)} was called
45       * @param request
46       * @param response
47       * @throws ServletException
48       * @throws IOException
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       * Remembers posted values to be returned when {@link #doGet(HttpServletRequest, HttpServletResponse)} is called.
65       * @param request
66       * @param response
67       * @throws ServletException
68       * @throws IOException
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              // Handle single or multiple values for the parameter
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  }