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.core.io.buffer.DataBufferFactory; 029import org.springframework.http.MediaType; 030import org.springframework.http.codec.json.AbstractJackson2Encoder; 031import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; 032import org.springframework.util.Assert; 033import org.springframework.util.MimeType; 034 035/** 036 * Encode from an {@code Object} to bytes of CBOR objects using Jackson. 037 * Stream encoding is not supported yet. 038 * 039 * @author Sebastien Deleuze 040 * @since 5.2 041 * @see Jackson2CborDecoder 042 * @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a> 043 */ 044public class Jackson2CborEncoder extends AbstractJackson2Encoder { 045 046 public Jackson2CborEncoder() { 047 this(Jackson2ObjectMapperBuilder.cbor().build(), MediaType.APPLICATION_CBOR); 048 } 049 050 public Jackson2CborEncoder(ObjectMapper mapper, MimeType... mimeTypes) { 051 super(mapper, mimeTypes); 052 Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass()); 053 } 054 055 056 @Override 057 public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { 058 throw new UnsupportedOperationException("Does not support stream encoding yet"); 059 } 060 061}