/graylog2-server/src/main/java/org/graylog2/rest/resources/system/SystemShutdownResource.java

https://github.com/Graylog2/graylog2-server · Java · 77 lines · 48 code · 9 blank · 20 comment · 0 complexity · 1a1398b4b6187eb610eeda08e1a98368 MD5 · raw file

  1. /*
  2. * Copyright (C) 2020 Graylog, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  16. */
  17. package org.graylog2.rest.resources.system;
  18. import com.codahale.metrics.annotation.Timed;
  19. import com.google.common.collect.ImmutableMap;
  20. import org.apache.shiro.authz.annotation.RequiresAuthentication;
  21. import org.graylog2.audit.AuditEventTypes;
  22. import org.graylog2.audit.jersey.AuditEvent;
  23. import org.graylog2.plugin.ServerStatus;
  24. import org.graylog2.shared.rest.resources.RestResource;
  25. import org.graylog2.shared.security.RestPermissions;
  26. import org.graylog2.system.shutdown.GracefulShutdown;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import javax.inject.Inject;
  30. import javax.ws.rs.POST;
  31. import javax.ws.rs.Path;
  32. import javax.ws.rs.Produces;
  33. import javax.ws.rs.core.MediaType;
  34. import javax.ws.rs.core.Response;
  35. import static javax.ws.rs.core.Response.accepted;
  36. /**
  37. * @deprecated shutting down the node using an API request is discouraged in favor of using a service manager to
  38. * control the server process.
  39. */
  40. @RequiresAuthentication
  41. @Path("/system/shutdown")
  42. @Produces(MediaType.APPLICATION_JSON)
  43. @Deprecated
  44. public class SystemShutdownResource extends RestResource {
  45. private static final Logger LOG = LoggerFactory.getLogger(SystemShutdownResource.class);
  46. private final GracefulShutdown gracefulShutdown;
  47. private final ServerStatus serverStatus;
  48. @Inject
  49. public SystemShutdownResource(GracefulShutdown gracefulShutdown,
  50. ServerStatus serverStatus) {
  51. this.gracefulShutdown = gracefulShutdown;
  52. this.serverStatus = serverStatus;
  53. }
  54. @POST
  55. @Timed
  56. @Path("/shutdown")
  57. @AuditEvent(type = AuditEventTypes.NODE_SHUTDOWN_INITIATE)
  58. @Deprecated
  59. public Response shutdown() {
  60. checkPermission(RestPermissions.NODE_SHUTDOWN, serverStatus.getNodeId().toString());
  61. new Thread(gracefulShutdown).start();
  62. final String msg =
  63. "Deprecated API endpoint /system/shutdown/shutdown was called. Shutting down the node via the API is " +
  64. "discouraged in favor of using a service manager to control the server process.";
  65. LOG.warn(msg);
  66. return accepted(ImmutableMap.of("message", msg)).build();
  67. }
  68. }