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.cbor;
018
019import java.util.Map;
020
021import com.fasterxml.jackson.databind.ObjectMapper;
022import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
023import org.reactivestreams.Publisher;
024import reactor.core.publisher.Flux;
025
026import org.springframework.core.ResolvableType;
027import org.springframework.core.io.buffer.DataBuffer;
028import org.springframework.http.MediaType;
029import org.springframework.http.codec.json.AbstractJackson2Decoder;
030import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
031import org.springframework.util.Assert;
032import org.springframework.util.MimeType;
033
034/**
035 * Decode bytes into CBOR and convert to Object's with Jackson.
036 * Stream decoding is not supported yet.
037 *
038 * @author Sebastien Deleuze
039 * @since 5.2
040 * @see Jackson2CborEncoder
041 * @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a>
042 */
043public class Jackson2CborDecoder extends AbstractJackson2Decoder {
044
045        public Jackson2CborDecoder() {
046                this(Jackson2ObjectMapperBuilder.cbor().build(), MediaType.APPLICATION_CBOR);
047        }
048
049        public Jackson2CborDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
050                super(mapper, mimeTypes);
051                Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass());
052        }
053
054
055        @Override
056        public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
057                throw new UnsupportedOperationException("Does not support stream decoding yet");
058        }
059
060}