org.zodiac.actuate.application.maintenance.AppMaintenanceEndpoint Maven / Gradle / Ivy
The newest version!
package org.zodiac.actuate.application.maintenance;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zodiac.commons.util.web.ServletRequests;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
@RestControllerEndpoint(id = AppMaintenanceEndpoint.ENDPOINT_NAME)
public class AppMaintenanceEndpoint {
public static final String ENDPOINT_NAME = "app-maintenance";
private AppMaintenanceOperatorRegistry maintenanceOperatorRegistry;
public AppMaintenanceEndpoint(AppMaintenanceOperatorRegistry maintenanceOperatorRegistry) {
this.maintenanceOperatorRegistry = maintenanceOperatorRegistry;
}
@RequestMapping(path = {"/{operator}"}, method = {RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
public ResponseEntity> process(@PathVariable("operator") String operator,
@RequestBody(required = false) Map params) {
try {
RequestMethod method = ServletRequests.getRequestMethod();
AppMaintenanceOperator maintenanceOperator = maintenanceOperatorRegistry.getOperator(operator, method).orElseThrow(
(Supplier)() -> new UnsupportedOperationException("Unsupported maintenance operation"));
maintenanceOperator.process(params);
return ResponseEntity.ok(AppMaintenanceResult.successOfData("success"));
} catch (Throwable throwable) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(AppMaintenanceResult.failOfMessage(throwable.getMessage()));
}
}
@GetMapping(path = {"/status"})
public ResponseEntity>> status() {
try {
return ResponseEntity.of(Optional.ofNullable(AppMaintenanceResult.successOfData(maintenanceOperatorRegistry.getStatus())));
} catch (Throwable throwable) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(AppMaintenanceResult.failOfMessage(throwable.getMessage()));
}
}
}