001/*
002 * Copyright 2002-2019 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.codec.json;
018
019import java.util.Collections;
020import java.util.List;
021
022import com.fasterxml.jackson.databind.ObjectMapper;
023import com.fasterxml.jackson.dataformat.smile.SmileFactory;
024import reactor.core.publisher.Flux;
025
026import org.springframework.http.MediaType;
027import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
028import org.springframework.util.Assert;
029import org.springframework.util.MimeType;
030
031/**
032 * Encode from an {@code Object} stream to a byte stream of Smile objects using Jackson 2.9.
033 * For non-streaming use cases, {@link Flux} elements are collected into a {@link List}
034 * before serialization for performance reason.
035 *
036 * @author Sebastien Deleuze
037 * @since 5.0
038 * @see Jackson2SmileDecoder
039 */
040public class Jackson2SmileEncoder extends AbstractJackson2Encoder {
041
042        private static final MimeType[] DEFAULT_SMILE_MIME_TYPES = new MimeType[] {
043                        new MimeType("application", "x-jackson-smile"),
044                        new MimeType("application", "*+x-jackson-smile")};
045
046
047        public Jackson2SmileEncoder() {
048                this(Jackson2ObjectMapperBuilder.smile().build(), DEFAULT_SMILE_MIME_TYPES);
049        }
050
051        public Jackson2SmileEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
052                super(mapper, mimeTypes);
053                Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
054                setStreamingMediaTypes(Collections.singletonList(new MediaType("application", "stream+x-jackson-smile")));
055        }
056
057}