Merge pull request #10350 from vpavic:gh-10348
* pr/10350: Polish "Remove usage of `HttpStatus` in Web Endpoints" Remove usage of `HttpStatus` in Web Endpoints
This commit is contained in:
commit
92beba10fe
|
|
@ -22,7 +22,6 @@ import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDes
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
||||||
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
|
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link WebEndpointExtension} for the {@link AuditEventsEndpoint}.
|
* {@link WebEndpointExtension} for the {@link AuditEventsEndpoint}.
|
||||||
|
|
@ -43,7 +42,7 @@ public class AuditEventsWebEndpointExtension {
|
||||||
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType(
|
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType(
|
||||||
String principal, Date after, String type) {
|
String principal, Date after, String type) {
|
||||||
if (after == null) {
|
if (after == null) {
|
||||||
return new WebEndpointResponse<>(HttpStatus.BAD_REQUEST.value());
|
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
AuditEventsDescriptor auditEvents = this.delegate
|
AuditEventsDescriptor auditEvents = this.delegate
|
||||||
.eventsWithPrincipalDateAfterAndType(principal, after, type);
|
.eventsWithPrincipalDateAfterAndType(principal, after, type);
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,41 @@ import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExten
|
||||||
* @param <T> the type of the response body
|
* @param <T> the type of the response body
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Vedran Pavic
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public final class WebEndpointResponse<T> {
|
public final class WebEndpointResponse<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 200 OK}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_OK = 200;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 400 Bad Request}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_BAD_REQUEST = 400;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 404 Not Found}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_NOT_FOUND = 404;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 429 Too Many Requests}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_TOO_MANY_REQUESTS = 429;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 500 Internal Server Error}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code 503 Service Unavailable}.
|
||||||
|
*/
|
||||||
|
public static final int STATUS_SERVICE_UNAVAILABLE = 503;
|
||||||
|
|
||||||
private final T body;
|
private final T body;
|
||||||
|
|
||||||
private final int status;
|
private final int status;
|
||||||
|
|
@ -56,7 +87,7 @@ public final class WebEndpointResponse<T> {
|
||||||
* @param body the body
|
* @param body the body
|
||||||
*/
|
*/
|
||||||
public WebEndpointResponse(T body) {
|
public WebEndpointResponse(T body) {
|
||||||
this(body, 200);
|
this(body, STATUS_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,8 +41,9 @@ public class HealthStatusHttpMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupDefaultStatusMapping() {
|
private void setupDefaultStatusMapping() {
|
||||||
addStatusMapping(Status.DOWN, 503);
|
addStatusMapping(Status.DOWN, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
|
||||||
addStatusMapping(Status.OUT_OF_SERVICE, 503);
|
addStatusMapping(Status.OUT_OF_SERVICE,
|
||||||
|
WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -102,9 +104,10 @@ public class HealthStatusHttpMapper {
|
||||||
if (code != null) {
|
if (code != null) {
|
||||||
return this.statusMapping.keySet().stream()
|
return this.statusMapping.keySet().stream()
|
||||||
.filter((key) -> code.equals(getUniformValue(key)))
|
.filter((key) -> code.equals(getUniformValue(key)))
|
||||||
.map(this.statusMapping::get).findFirst().orElse(200);
|
.map(this.statusMapping::get).findFirst()
|
||||||
|
.orElse(WebEndpointResponse.STATUS_OK);
|
||||||
}
|
}
|
||||||
return 200;
|
return WebEndpointResponse.STATUS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getUniformValue(String code) {
|
private String getUniformValue(String code) {
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,6 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
|
@ -89,12 +88,14 @@ public class HeapDumpWebEndpoint {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
return new WebEndpointResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
return new WebEndpointResponse<>(
|
||||||
|
WebEndpointResponse.STATUS_INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
catch (HeapDumperUnavailableException ex) {
|
catch (HeapDumperUnavailableException ex) {
|
||||||
return new WebEndpointResponse<>(HttpStatus.SERVICE_UNAVAILABLE.value());
|
return new WebEndpointResponse<>(
|
||||||
|
WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
|
||||||
}
|
}
|
||||||
return new WebEndpointResponse<>(HttpStatus.TOO_MANY_REQUESTS.value());
|
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_TOO_MANY_REQUESTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource dumpHeap(boolean live) throws IOException, InterruptedException {
|
private Resource dumpHeap(boolean live) throws IOException, InterruptedException {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue