org.zalando.riptide.DefaultMessageWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of riptide-core Show documentation
Show all versions of riptide-core Show documentation
Client side response routing
The newest version!
package org.zalando.riptide;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestClientException;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.List;
import static java.lang.String.format;
@AllArgsConstructor
final class DefaultMessageWriter implements MessageWriter {
private final List> converters;
@Override
public void write(final HttpOutputMessage request, final RequestArguments arguments)
throws IOException {
@Nullable final Object body = arguments.getBody();
if (body == null) {
return;
}
final Class> type = body.getClass();
@Nullable final MediaType contentType = request.getHeaders().getContentType();
converters.stream()
.filter(converter -> converter.canWrite(type, contentType))
.map(this::cast)
.findFirst()
.orElseThrow(() -> fail(type, contentType))
.write(body, contentType, request);
}
@SuppressWarnings("unchecked") // guarded by HttpMessageConverter#canWrite
private HttpMessageConverter cast(final HttpMessageConverter> converter) {
return (HttpMessageConverter) converter;
}
private RestClientException fail(final Class> type, @Nullable final MediaType contentType) {
final String message = format(
"Could not write request: no suitable HttpMessageConverter found for request type [%s]",
type.getName());
if (contentType == null) {
return new RestClientException(message);
} else {
return new RestClientException(format("%s and content type [%s]", message, contentType));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy