001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.converter;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.lang.reflect.Type;
022
023import org.springframework.http.HttpHeaders;
024import org.springframework.http.HttpOutputMessage;
025import org.springframework.http.MediaType;
026import org.springframework.http.StreamingHttpOutputMessage;
027import org.springframework.lang.Nullable;
028
029/**
030 * Abstract base class for most {@link GenericHttpMessageConverter} implementations.
031 *
032 * @author Sebastien Deleuze
033 * @author Juergen Hoeller
034 * @since 4.2
035 * @param <T> the converted object type
036 */
037public abstract class AbstractGenericHttpMessageConverter<T> extends AbstractHttpMessageConverter<T>
038                implements GenericHttpMessageConverter<T> {
039
040        /**
041         * Construct an {@code AbstractGenericHttpMessageConverter} with no supported media types.
042         * @see #setSupportedMediaTypes
043         */
044        protected AbstractGenericHttpMessageConverter() {
045        }
046
047        /**
048         * Construct an {@code AbstractGenericHttpMessageConverter} with one supported media type.
049         * @param supportedMediaType the supported media type
050         */
051        protected AbstractGenericHttpMessageConverter(MediaType supportedMediaType) {
052                super(supportedMediaType);
053        }
054
055        /**
056         * Construct an {@code AbstractGenericHttpMessageConverter} with multiple supported media type.
057         * @param supportedMediaTypes the supported media types
058         */
059        protected AbstractGenericHttpMessageConverter(MediaType... supportedMediaTypes) {
060                super(supportedMediaTypes);
061        }
062
063
064        @Override
065        protected boolean supports(Class<?> clazz) {
066                return true;
067        }
068
069        @Override
070        public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
071                return (type instanceof Class ? canRead((Class<?>) type, mediaType) : canRead(mediaType));
072        }
073
074        @Override
075        public boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType) {
076                return canWrite(clazz, mediaType);
077        }
078
079        /**
080         * This implementation sets the default headers by calling {@link #addDefaultHeaders},
081         * and then calls {@link #writeInternal}.
082         */
083        @Override
084        public final void write(final T t, @Nullable final Type type, @Nullable MediaType contentType,
085                        HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
086
087                final HttpHeaders headers = outputMessage.getHeaders();
088                addDefaultHeaders(headers, t, contentType);
089
090                if (outputMessage instanceof StreamingHttpOutputMessage) {
091                        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
092                        streamingOutputMessage.setBody(outputStream -> writeInternal(t, type, new HttpOutputMessage() {
093                                @Override
094                                public OutputStream getBody() {
095                                        return outputStream;
096                                }
097                                @Override
098                                public HttpHeaders getHeaders() {
099                                        return headers;
100                                }
101                        }));
102                }
103                else {
104                        writeInternal(t, type, outputMessage);
105                        outputMessage.getBody().flush();
106                }
107        }
108
109        @Override
110        protected void writeInternal(T t, HttpOutputMessage outputMessage)
111                        throws IOException, HttpMessageNotWritableException {
112
113                writeInternal(t, null, outputMessage);
114        }
115
116        /**
117         * Abstract template method that writes the actual body. Invoked from {@link #write}.
118         * @param t the object to write to the output message
119         * @param type the type of object to write (may be {@code null})
120         * @param outputMessage the HTTP output message to write to
121         * @throws IOException in case of I/O errors
122         * @throws HttpMessageNotWritableException in case of conversion errors
123         */
124        protected abstract void writeInternal(T t, @Nullable Type type, HttpOutputMessage outputMessage)
125                        throws IOException, HttpMessageNotWritableException;
126
127}