001package io.prometheus.cloudwatch;
002
003import jakarta.servlet.ServletException;
004import jakarta.servlet.http.HttpServlet;
005import jakarta.servlet.http.HttpServletRequest;
006import jakarta.servlet.http.HttpServletResponse;
007import java.io.IOException;
008
009public class DynamicReloadServlet extends HttpServlet {
010  private static final long serialVersionUID = 9078784531819993933L;
011  private final CloudWatchCollector collector;
012
013  public DynamicReloadServlet(CloudWatchCollector collector) {
014    this.collector = collector;
015  }
016
017  static final String CONTENT_TYPE = "text/plain";
018
019  @Override
020  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
021      throws ServletException, IOException {
022    resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
023    resp.setContentType(CONTENT_TYPE);
024    try {
025      resp.getWriter().print("Only POST requests allowed");
026    } catch (IOException e) {
027      // Ignored
028    }
029  }
030
031  @Override
032  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
033      throws ServletException, IOException {
034    try {
035      collector.reloadConfig();
036    } catch (IOException e) {
037      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
038      resp.setContentType(CONTENT_TYPE);
039      try {
040        resp.getWriter().print("Reloading config failed");
041      } catch (IOException ee) {
042        // Ignored
043      }
044      return;
045    }
046
047    resp.setContentType(CONTENT_TYPE);
048    try {
049      resp.getWriter().print("OK");
050    } catch (IOException e) {
051      // Ignored
052    }
053  }
054}