Dropwizard : New admin resource

£可爱£侵袭症+ 提交于 2021-02-18 10:27:26

问题


I'm using Drowpizard 0.7.1, but perhaps I will upgrade to 0.8.4 very soon.

Does anyone know how to add a admin resource to dropwizard, that is shown in Operational Menu like the example below?

Operational Menu

    Metrics
    Ping
    Threads
    Healthcheck
    CustomAdminXy

回答1:


I don't think you can do this easily.

The AdminServlet is created when the ServerFactory is built. It may be possible to extend DefaultServerFactory and override createAdminServlet to create a custom Admin servlet with your links etc... (You would then have to set your server factory via configuration.)

It seems like this would involve some duplication of code and could be quite fragile.

It might be easier to just register your own admin servlet (in addition to the regular one), e.g.:

environment.admin().addServlet("custom-admin", new CustomAdminServlet())
    .addMapping("/custom-admin");

Probably not ideal either.




回答2:


Using .addMapping("") with Dropwizard version 0.9.1 allows you to override the menu without conflicting with the default AdminServlet mapping at "/*".

In the Application:

public void run(final NetworkModelApplicationConfiguration configuration, final Environment environment) {
    environment.admin().addServlet("my-admin-menu", new MyAdminServlet()).addMapping("");
    environment.admin().addServlet("my-admin-feature", new MyAdminFeatureServlet()).addMapping("/myAdminFeature");
}

Extending AdminServlet isn't very useful since all the properties are private. I built an HTTPServlet that reads a resource as a template:

public class MyAdminServlet extends HttpServlet {
  private String serviceName;

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    this.serviceName = config.getInitParameter("service-name");
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String path = req.getContextPath() + req.getServletPath();
    resp.setStatus(200);
    resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
    resp.setContentType("text/html");
    PrintWriter writer = resp.getWriter();

    try {
      String template = getResourceAsString("/admin.html", "UTF-8");
      String serviceName = this.serviceName == null?"":" (" + this.serviceName + ")";

      writer.println(MessageFormat.format(template, new Object[] { path, serviceName }));
    } finally {
      writer.close();
    }
  }

  String getResourceAsString(String resource, String charSet) throws IOException {
    InputStream in = this.getClass().getResourceAsStream(resource);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    while ((len = in.read(buffer)) != -1) {
      out.write(buffer, 0, len);
    }
    return out.toString(charSet);
  }
}

My /admin.html resource looks like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Operational Menu{1}</title>
  </head>
  <body>
    <h1>Operational Menu{1}</h1>
    <ul>
      <li><a href="{0}/metrics?pretty=true">Metrics</a></li>
      <li><a href="{0}/ping">Ping</a></li>
      <li><a href="{0}/threads">Threads</a></li>
      <li><a href="{0}/healthcheck?pretty=true">Healthcheck</a></li>
      <li><a href="{0}/myAdminFeature">My Admin Feature</a></li>
    </ul>
  </body>
</html>


来源:https://stackoverflow.com/questions/32764337/dropwizard-new-admin-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!