use of cn.taketoday.http.converter.HttpMessageNotReadableException in project today-infrastructure by TAKETODAY.
the class AbstractWireFeedHttpMessageConverter method readInternal.
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
WireFeedInput feedInput = new WireFeedInput();
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET;
try {
InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody());
Reader reader = new InputStreamReader(inputStream, charset);
return (T) feedInput.build(reader);
} catch (FeedException ex) {
throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage);
}
}
use of cn.taketoday.http.converter.HttpMessageNotReadableException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method httpMessageNotReadable.
@Test
@SuppressWarnings("deprecation")
public void httpMessageNotReadable() {
Exception ex = new HttpMessageNotReadableException("message");
testException(ex);
}
use of cn.taketoday.http.converter.HttpMessageNotReadableException in project today-framework by TAKETODAY.
the class AbstractWireFeedHttpMessageConverter method readInternal.
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
WireFeedInput feedInput = new WireFeedInput();
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET;
try {
InputStream inputStream = StreamUtils.nonClosing(inputMessage.getBody());
Reader reader = new InputStreamReader(inputStream, charset);
return (T) feedInput.build(reader);
} catch (FeedException ex) {
throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage);
}
}
use of cn.taketoday.http.converter.HttpMessageNotReadableException in project today-framework by TAKETODAY.
the class AbstractMessageConverterParameterResolver method readWithMessageConverters.
/**
* Create the method argument value of the expected parameter type by
* reading from the given request.
*
* @param context the current request context
* @param parameter the method parameter descriptor (may be {@code null})
* @param targetType the target type, not necessarily the same as the method
* parameter type, e.g. for {@code HttpEntity<String>}.
* @return the created method argument value
* @throws IOException if the reading from the request fails
* @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
*/
@Nullable
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(RequestContext context, MethodParameter parameter, Type targetType) throws //
IOException, //
HttpMediaTypeNotSupportedException, //
HttpMessageNotReadableException {
MediaType contentType;
boolean noContentType = false;
try {
contentType = context.requestHeaders().getContentType();
} catch (InvalidMediaTypeException ex) {
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}
if (contentType == null) {
noContentType = true;
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
Class<?> contextClass = parameter.getContainingClass();
Class<T> targetClass = targetType instanceof Class ? (Class<T>) targetType : null;
if (targetClass == null) {
ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
targetClass = (Class<T>) resolvableType.resolve();
}
Object body = NO_VALUE;
EmptyBodyCheckingHttpInputMessage message = null;
try {
message = new EmptyBodyCheckingHttpInputMessage(context);
RequestResponseBodyAdviceChain adviceChain = getAdvice();
for (HttpMessageConverter<?> converter : this.messageConverters) {
Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
GenericHttpMessageConverter<?> genericConverter = converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null;
if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) : targetClass != null && converter.canRead(targetClass, contentType)) {
if (message.hasBody()) {
HttpInputMessage msgToUse = adviceChain.beforeBodyRead(message, parameter, targetType, converterType);
body = genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse);
body = adviceChain.afterBodyRead(body, msgToUse, parameter, targetType, converterType);
} else {
body = adviceChain.handleEmptyBody(null, message, parameter, targetType, converterType);
}
break;
}
}
} catch (IOException ex) {
throw new HttpMessageNotReadableException("I/O error while reading input message", ex, context);
} finally {
if (message != null && message.hasBody()) {
closeStreamIfNecessary(message.getBody());
}
}
if (body == NO_VALUE) {
HttpMethod httpMethod = context.getMethod();
if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) || (noContentType && !message.hasBody())) {
return null;
}
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(targetClass != null ? targetClass : Object.class));
}
MediaType selectedContentType = contentType;
Object theBody = body;
LogFormatUtils.traceDebug(log, traceOn -> {
String formatted = LogFormatUtils.formatValue(theBody, !traceOn);
return "Read \"" + selectedContentType + "\" to [" + formatted + "]";
});
return body;
}
Aggregations