/** * This implementation checks if the given class is {@linkplain #supports(Class) supported}, * and if the {@linkplain #getSupportedMediaTypes() supported media types} * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return supports(clazz) && canRead(mediaType); }
/** * This implementation checks if the given class is * {@linkplain #supports(Class) supported}, and if the * {@linkplain #getSupportedMediaTypes() supported} media types * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return supports(clazz) && canWrite(mediaType); }
/** * Construct an {@code AbstractHttpMessageConverter} with one supported media type. * @param supportedMediaType the supported media type */ protected AbstractHttpMessageConverter(MediaType supportedMediaType) { setSupportedMediaTypes(Collections.singletonList(supportedMediaType)); }
/** * This implementation sets the default headers by calling {@link #addDefaultHeaders}, * and then calls {@link #writeInternal}. */ @Override public final void write(final T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { final HttpHeaders headers = outputMessage.getHeaders(); addDefaultHeaders(headers, t, contentType); if (outputMessage instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage; streamingOutputMessage.setBody(outputStream -> writeInternal(t, new HttpOutputMessage() { @Override public OutputStream getBody() { return outputStream; } @Override public HttpHeaders getHeaders() { return headers; } })); } else { writeInternal(t, outputMessage); outputMessage.getBody().flush(); } }
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) { return super.canRead(contextClass, mediaType); }
MediaType contentTypeToUse = contentType; if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(t); MediaType mediaType = getDefaultContentType(t); contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse); Charset defaultCharset = getDefaultCharset(); if (defaultCharset != null) { contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset); Long contentLength = getContentLength(t, headers.getContentType()); if (contentLength != null) { headers.setContentLength(contentLength);
/** * This implementation delegates to {@link #getDefaultContentType(Object)} if a content * type was not provided, calls {@link #getContentLength}, and sets the corresponding headers * on the output message. It then calls {@link #writeInternal}. */ public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { HttpHeaders headers = outputMessage.getHeaders(); if (headers.getContentType() == null) { if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentType = getDefaultContentType(t); } if (contentType != null) { headers.setContentType(contentType); } } if (headers.getContentLength() == -1) { Long contentLength = getContentLength(t, headers.getContentType()); if (contentLength != null) { headers.setContentLength(contentLength); } } writeInternal(t, outputMessage); outputMessage.getBody().flush(); }
/** * Returns the default content type for the given type. Called when {@link #write} * is invoked without a specified content type parameter. * <p>By default, this returns the first element of the * {@link #setSupportedMediaTypes(List) supportedMediaTypes} property, if any. * Can be overridden in subclasses. * @param t the type to return the content type for * @return the content type, or {@code null} if not known */ @Nullable protected MediaType getDefaultContentType(T t) throws IOException { List<MediaType> mediaTypes = getSupportedMediaTypes(); return (!mediaTypes.isEmpty() ? mediaTypes.get(0) : null); }
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) { return super.canWrite(clazz, mediaType); }
MediaType contentTypeToUse = contentType; if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(t); MediaType mediaType = getDefaultContentType(t); contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse); Charset defaultCharset = getDefaultCharset(); if (defaultCharset != null) { contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset);
/** * This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. * Future implementations might add some default behavior, however. */ @Override public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return readInternal(clazz, inputMessage); }
@Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return super.canRead(mediaType); }
MediaType contentTypeToUse = contentType; if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(t); MediaType mediaType = getDefaultContentType(t); contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse); Charset defaultCharset = getDefaultCharset(); if (defaultCharset != null) { contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset); Long contentLength = getContentLength(t, headers.getContentType()); if (contentLength != null) { headers.setContentLength(contentLength);
/** * Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List) * supported} media types {@link MediaType#includes(MediaType) include} the * given media type. * @param mediaType the media type to read, can be {@code null} if not specified. * Typically the value of a {@code Content-Type} header. * @return {@code true} if the supported media types include the media type, * or if the media type is {@code null} */ protected boolean canRead(@Nullable MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.includes(mediaType)) { return true; } } return false; }
@Override protected boolean canWrite(@Nullable MediaType mediaType) { return (super.canWrite(mediaType) || (this.protobufFormatSupport != null && this.protobufFormatSupport.supportsWriteOnly(mediaType))); }
/** * This implementation sets the default headers by calling {@link #addDefaultHeaders}, * and then calls {@link #writeInternal}. */ @Override public final void write(final T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { final HttpHeaders headers = outputMessage.getHeaders(); addDefaultHeaders(headers, t, contentType); if (outputMessage instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage; streamingOutputMessage.setBody(outputStream -> writeInternal(t, new HttpOutputMessage() { @Override public OutputStream getBody() { return outputStream; } @Override public HttpHeaders getHeaders() { return headers; } })); } else { writeInternal(t, outputMessage); outputMessage.getBody().flush(); } }
/** * This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. * Future implementations might add some default behavior, however. */ @Override public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return readInternal(clazz, inputMessage); }
/** * This implementation checks if the given class is {@linkplain #supports(Class) supported}, * and if the {@linkplain #getSupportedMediaTypes() supported media types} * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { return supports(clazz) && canRead(mediaType); }
/** * This implementation checks if the given class is * {@linkplain #supports(Class) supported}, and if the * {@linkplain #getSupportedMediaTypes() supported} media types * {@linkplain MediaType#includes(MediaType) include} the given media type. */ @Override public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { return supports(clazz) && canWrite(mediaType); }
@Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return super.canRead(mediaType); }