GH-1025 Polish and test DELETE feature of s-c-f-web

This commit is contained in:
Oleg Zhurakousky
2023-05-30 14:04:14 +02:00
parent d87c3ad1bd
commit f6410af8e1
5 changed files with 184 additions and 8 deletions

View File

@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerRequest;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper;
@@ -32,6 +33,7 @@ import org.springframework.cloud.function.web.constants.WebRequestConstants;
import org.springframework.cloud.function.web.util.FunctionWebRequestProcessingHelper;
import org.springframework.cloud.function.web.util.FunctionWrapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
@@ -47,11 +49,14 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;
/**
* @author Dave Syer
* @author Mark Fisher
@@ -152,11 +157,11 @@ public class FunctionController {
}
@DeleteMapping(path = "/**")
@ResponseBody
public Object delete(WebRequest request, @RequestBody(required = false) String body) {
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void delete(WebRequest request, @RequestBody(required = false) String body) {
FunctionWrapper wrapper = wrapper(request);
if (FunctionWebRequestProcessingHelper.isValidFunction("DELETE", wrapper.getFunction().getFunctionDefinition(), this.functionHttpProperties)) {
return FunctionWebRequestProcessingHelper.processRequest(wrapper, body, false);
FunctionWebRequestProcessingHelper.processRequest(wrapper, wrapper.getArgument(), false);
}
else {
throw new IllegalArgumentException(FunctionWebRequestProcessingHelper.buildBadMappingErrorMessage("DELETE", wrapper.getFunction().getFunctionDefinition()));
@@ -178,7 +183,7 @@ public class FunctionController {
private FunctionWrapper wrapper(WebRequest request) {
FunctionInvocationWrapper function = (FunctionInvocationWrapper) request
.getAttribute(WebRequestConstants.HANDLER, WebRequest.SCOPE_REQUEST);
FunctionWrapper wrapper = new FunctionWrapper(function);
FunctionWrapper wrapper = new FunctionWrapper(function, (((ServletWebRequest) request).getRequest()).getMethod());
for (String key : request.getParameterMap().keySet()) {
wrapper.getParams().addAll(key, Arrays.asList(request.getParameterValues(key)));
}

View File

@@ -34,6 +34,7 @@ import org.springframework.cloud.function.web.FunctionHttpProperties;
import org.springframework.cloud.function.web.constants.WebRequestConstants;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity.HeadersBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.messaging.Message;
@@ -59,7 +60,7 @@ public final class FunctionWebRequestProcessingHelper {
public static FunctionInvocationWrapper findFunction(FunctionProperties functionProperties, HttpMethod method, FunctionCatalog functionCatalog,
Map<String, Object> attributes, String path) {
if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST) || method.equals(HttpMethod.PUT) || method.equals(HttpMethod.DELETE)) {
return doFindFunction(functionProperties.getDefinition(), method, functionCatalog, attributes, path);
}
else {
@@ -132,7 +133,8 @@ public final class FunctionWebRequestProcessingHelper {
if (result instanceof Publisher) {
Mono.from((Publisher) result).subscribe();
}
return Mono.just(ResponseEntity.accepted().headers(HeaderUtils.sanitize(headers)).build());
return "DELETE".equals(wrapper.getMethod()) ?
Mono.empty() : Mono.just(ResponseEntity.ok().headers(HeaderUtils.sanitize(headers)).build());
}
BodyBuilder responseOkBuilder = ResponseEntity.ok().headers(HeaderUtils.sanitize(headers));

View File

@@ -37,8 +37,21 @@ public class FunctionWrapper {
private Object argument;
private final String method;
/**
*
* @param function instance of {@link FunctionInvocationWrapper}
* @deprecated since 4.0.4 in favor of the constructor that takes Http method as second argument.
*/
@Deprecated
public FunctionWrapper(FunctionInvocationWrapper function) {
this(function, null);
}
public FunctionWrapper(FunctionInvocationWrapper function, String method) {
this.function = function;
this.method = method;
}
public HttpHeaders getHeaders() {
@@ -64,4 +77,8 @@ public class FunctionWrapper {
public MultiValueMap<String, String> getParams() {
return params;
}
public String getMethod() {
return method;
}
}