001/*
002 * Copyright 2002-2014 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.web.servlet.mvc.method.annotation;
018
019import org.springframework.core.MethodParameter;
020import org.springframework.http.MediaType;
021import org.springframework.http.converter.HttpMessageConverter;
022import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
023import org.springframework.http.converter.json.MappingJacksonValue;
024import org.springframework.http.server.ServerHttpRequest;
025import org.springframework.http.server.ServerHttpResponse;
026import org.springframework.lang.Nullable;
027
028/**
029 * A convenient base class for {@code ResponseBodyAdvice} implementations
030 * that customize the response before JSON serialization with
031 * {@link AbstractJackson2HttpMessageConverter}'s concrete subclasses.
032 *
033 * @author Rossen Stoyanchev
034 * @author Sebastien Deleuze
035 * @since 4.1
036 */
037public abstract class AbstractMappingJacksonResponseBodyAdvice implements ResponseBodyAdvice<Object> {
038
039        @Override
040        public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
041                return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
042        }
043
044        @Override
045        @Nullable
046        public final Object beforeBodyWrite(@Nullable Object body, MethodParameter returnType,
047                        MediaType contentType, Class<? extends HttpMessageConverter<?>> converterType,
048                        ServerHttpRequest request, ServerHttpResponse response) {
049
050                if (body == null) {
051                        return null;
052                }
053                MappingJacksonValue container = getOrCreateContainer(body);
054                beforeBodyWriteInternal(container, contentType, returnType, request, response);
055                return container;
056        }
057
058        /**
059         * Wrap the body in a {@link MappingJacksonValue} value container (for providing
060         * additional serialization instructions) or simply cast it if already wrapped.
061         */
062        protected MappingJacksonValue getOrCreateContainer(Object body) {
063                return (body instanceof MappingJacksonValue ? (MappingJacksonValue) body : new MappingJacksonValue(body));
064        }
065
066        /**
067         * Invoked only if the converter type is {@code MappingJackson2HttpMessageConverter}.
068         */
069        protected abstract void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
070                        MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response);
071
072}