Clean up tidy up RequestProcessor

This commit is contained in:
Oleg Zhurakousky
2018-11-16 14:12:50 +01:00
parent c46f25a000
commit 417a6a75c1

View File

@@ -132,19 +132,12 @@ public class RequestProcessor {
if (StringUtils.hasText(body)) { if (StringUtils.hasText(body)) {
MediaType contentType = wrapper.headers.getContentType(); MediaType contentType = wrapper.headers.getContentType();
if (contentType != null && "text".equalsIgnoreCase(contentType.getType())) { if (this.shouldUseJsonConversion(body, contentType)) {
input = converter.convert(function, body); Type jsonType = body.startsWith("[") && Collection.class.isAssignableFrom(inputType) || body.startsWith("{") ? inputType : Collection.class;
} if (body.startsWith("[")) {
else if (body.startsWith("[")) { jsonType = ResolvableType.forClassWithGenerics((Class<?>)jsonType, (Class<?>) itemType).getType();
Class<?> collectionType = Collection.class.isAssignableFrom(inputType) ? inputType : Collection.class; }
input = mapper.toObject(body, input = mapper.toObject(body, jsonType);
ResolvableType.forClassWithGenerics(collectionType, (Class<?>) itemType).getType());
}
else if (body.startsWith("{")) {
input = mapper.toObject(body, inputType);
}
else if (body.startsWith("\"")) {
input = body.substring(1, body.length() - 2);
} }
else { else {
input = converter.convert(function, body); input = converter.convert(function, body);
@@ -154,6 +147,11 @@ public class RequestProcessor {
return response(wrapper, input, stream); return response(wrapper, input, stream);
} }
private boolean shouldUseJsonConversion(String body, MediaType contentType) {
return (body.startsWith("[") || body.startsWith("{"))
&& (contentType == null || (contentType != null && !"text".equalsIgnoreCase(contentType.getType())));
}
public Mono<ResponseEntity<?>> stream(FunctionWrapper request) { public Mono<ResponseEntity<?>> stream(FunctionWrapper request) {
Publisher<?> result = request.function() != null Publisher<?> result = request.function() != null
? value(request.function(), request.argument()) ? value(request.function(), request.argument())
@@ -161,6 +159,10 @@ public class RequestProcessor {
return stream(request, result); return stream(request, result);
} }
private List<HttpMessageReader<?>> getMessageReaders() {
return this.messageReaders;
}
private Mono<ResponseEntity<?>> response(FunctionWrapper wrapper, Object body, private Mono<ResponseEntity<?>> response(FunctionWrapper wrapper, Object body,
boolean stream) { boolean stream) {
@@ -347,10 +349,6 @@ public class RequestProcessor {
return ReflectionUtils.findMethod(handler.getClass(), "apply", (Class<?>[]) null); return ReflectionUtils.findMethod(handler.getClass(), "apply", (Class<?>[]) null);
} }
public List<HttpMessageReader<?>> getMessageReaders() {
return this.messageReaders;
}
private Throwable handleReadError(MethodParameter parameter, Throwable ex) { private Throwable handleReadError(MethodParameter parameter, Throwable ex) {
return (ex instanceof DecodingException return (ex instanceof DecodingException
? new ServerWebInputException("Failed to read HTTP message", parameter, ? new ServerWebInputException("Failed to read HTTP message", parameter,