Browse Source
This commit adds CBOR reactive support for single value only in order to allow CBOR usage in RSocket. Notice that no CBOR support is configured on WebFlux, this will require gh-20513 to be resolved. Closes gh-22767pull/22870/head
Sebastien Deleuze
6 years ago
4 changed files with 327 additions and 0 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
/* |
||||
* Copyright 2002-2019 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.cbor; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory; |
||||
import org.reactivestreams.Publisher; |
||||
import reactor.core.publisher.Flux; |
||||
|
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.codec.json.AbstractJackson2Decoder; |
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.MimeType; |
||||
|
||||
/** |
||||
* Decode bytes into CBOR and convert to Object's with Jackson. |
||||
* Stream decoding is not supported yet. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 5.2 |
||||
* @see Jackson2CborEncoder |
||||
* @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a> |
||||
*/ |
||||
public class Jackson2CborDecoder extends AbstractJackson2Decoder { |
||||
|
||||
public Jackson2CborDecoder() { |
||||
this(Jackson2ObjectMapperBuilder.cbor().build(), new MediaType("application", "cbor")); |
||||
} |
||||
|
||||
public Jackson2CborDecoder(ObjectMapper mapper, MimeType... mimeTypes) { |
||||
super(mapper, mimeTypes); |
||||
Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass()); |
||||
} |
||||
|
||||
@Override |
||||
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { |
||||
throw new UnsupportedOperationException("Does not support stream decoding yet"); |
||||
} |
||||
} |
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
/* |
||||
* Copyright 2002-2019 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.cbor; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory; |
||||
import org.reactivestreams.Publisher; |
||||
import reactor.core.publisher.Flux; |
||||
|
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.core.io.buffer.DataBufferFactory; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.codec.json.AbstractJackson2Encoder; |
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.MimeType; |
||||
|
||||
/** |
||||
* Encode from an {@code Object} to bytes of CBOR objects using Jackson. |
||||
* Stream encoding is not supported yet. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 5.2 |
||||
* @see Jackson2CborDecoder |
||||
* @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a> |
||||
*/ |
||||
public class Jackson2CborEncoder extends AbstractJackson2Encoder { |
||||
|
||||
public Jackson2CborEncoder() { |
||||
this(Jackson2ObjectMapperBuilder.cbor().build(), new MediaType("application", "cbor")); |
||||
} |
||||
|
||||
public Jackson2CborEncoder(ObjectMapper mapper, MimeType... mimeTypes) { |
||||
super(mapper, mimeTypes); |
||||
Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass()); |
||||
} |
||||
|
||||
@Override |
||||
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { |
||||
throw new UnsupportedOperationException("Does not support stream encoding yet"); |
||||
} |
||||
} |
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
/* |
||||
* Copyright 2002-2019 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.cbor; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Flux; |
||||
|
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.core.codec.AbstractDecoderTestCase; |
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.http.codec.Pojo; |
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
||||
import org.springframework.util.MimeType; |
||||
|
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.springframework.core.ResolvableType.forClass; |
||||
import static org.springframework.http.MediaType.APPLICATION_JSON; |
||||
|
||||
/** |
||||
* Unit tests for {@link Jackson2CborDecoder}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
public class Jackson2CborDecoderTests extends AbstractDecoderTestCase<Jackson2CborDecoder> { |
||||
|
||||
private final static MimeType CBOR_MIME_TYPE = new MimeType("application", "cbor"); |
||||
|
||||
private Pojo pojo1 = new Pojo("f1", "b1"); |
||||
|
||||
private Pojo pojo2 = new Pojo("f2", "b2"); |
||||
|
||||
private ObjectMapper mapper = Jackson2ObjectMapperBuilder.cbor().build(); |
||||
|
||||
public Jackson2CborDecoderTests() { |
||||
super(new Jackson2CborDecoder()); |
||||
} |
||||
|
||||
@Override |
||||
@Test |
||||
public void canDecode() { |
||||
assertTrue(decoder.canDecode(forClass(Pojo.class), CBOR_MIME_TYPE)); |
||||
assertTrue(decoder.canDecode(forClass(Pojo.class), null)); |
||||
|
||||
assertFalse(decoder.canDecode(forClass(String.class), null)); |
||||
assertFalse(decoder.canDecode(forClass(Pojo.class), APPLICATION_JSON)); |
||||
} |
||||
|
||||
@Override |
||||
@Test(expected = UnsupportedOperationException.class) |
||||
public void decode() { |
||||
Flux<DataBuffer> input = Flux.just(this.pojo1, this.pojo2) |
||||
.map(this::writeObject) |
||||
.flatMap(this::dataBuffer); |
||||
|
||||
testDecodeAll(input, Pojo.class, step -> step |
||||
.expectNext(pojo1) |
||||
.expectNext(pojo2) |
||||
.verifyComplete()); |
||||
|
||||
} |
||||
|
||||
private byte[] writeObject(Object o) { |
||||
try { |
||||
return this.mapper.writer().writeValueAsBytes(o); |
||||
} |
||||
catch (JsonProcessingException e) { |
||||
throw new AssertionError(e); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void decodeToMono() { |
||||
List<Pojo> expected = Arrays.asList(pojo1, pojo2); |
||||
|
||||
Flux<DataBuffer> input = Flux.just(expected) |
||||
.map(this::writeObject) |
||||
.flatMap(this::dataBuffer); |
||||
|
||||
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class); |
||||
testDecodeToMono(input, elementType, step -> step |
||||
.expectNext(expected) |
||||
.expectComplete() |
||||
.verify(), null, null); |
||||
} |
||||
} |
@ -0,0 +1,104 @@
@@ -0,0 +1,104 @@
|
||||
/* |
||||
* Copyright 2002-2019 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.http.codec.cbor; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.UncheckedIOException; |
||||
import java.util.function.Consumer; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Flux; |
||||
|
||||
import org.springframework.core.ResolvableType; |
||||
import org.springframework.core.io.buffer.AbstractLeakCheckingTestCase; |
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils; |
||||
import org.springframework.http.codec.Pojo; |
||||
import org.springframework.http.codec.ServerSentEvent; |
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
||||
import org.springframework.util.MimeType; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
import static org.springframework.core.io.buffer.DataBufferUtils.release; |
||||
import static org.springframework.http.MediaType.APPLICATION_XML; |
||||
|
||||
/** |
||||
* Unit tests for {@link Jackson2CborEncoder}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
public class Jackson2CborEncoderTests extends AbstractLeakCheckingTestCase { |
||||
|
||||
private final static MimeType CBOR_MIME_TYPE = new MimeType("application", "cbor"); |
||||
|
||||
private final ObjectMapper mapper = Jackson2ObjectMapperBuilder.cbor().build(); |
||||
|
||||
private final Jackson2CborEncoder encoder = new Jackson2CborEncoder(); |
||||
|
||||
private Consumer<DataBuffer> pojoConsumer(Pojo expected) { |
||||
return dataBuffer -> { |
||||
try { |
||||
Pojo actual = this.mapper.reader().forType(Pojo.class) |
||||
.readValue(DataBufferTestUtils.dumpBytes(dataBuffer)); |
||||
assertEquals(expected, actual); |
||||
release(dataBuffer); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new UncheckedIOException(ex); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
@Test |
||||
public void canEncode() { |
||||
ResolvableType pojoType = ResolvableType.forClass(Pojo.class); |
||||
assertTrue(this.encoder.canEncode(pojoType, CBOR_MIME_TYPE)); |
||||
assertTrue(this.encoder.canEncode(pojoType, null)); |
||||
|
||||
// SPR-15464
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.NONE, null)); |
||||
} |
||||
|
||||
@Test |
||||
public void canNotEncode() { |
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), null)); |
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class), APPLICATION_XML)); |
||||
|
||||
ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class); |
||||
assertFalse(this.encoder.canEncode(sseType, CBOR_MIME_TYPE)); |
||||
} |
||||
|
||||
@Test |
||||
public void encode() { |
||||
Pojo value = new Pojo("foo", "bar"); |
||||
DataBuffer result = encoder.encodeValue(value, this.bufferFactory, ResolvableType.forClass(Pojo.class), CBOR_MIME_TYPE, null); |
||||
pojoConsumer(value).accept(result); |
||||
} |
||||
|
||||
@Test(expected = UnsupportedOperationException.class) |
||||
public void encodeStream() { |
||||
Pojo pojo1 = new Pojo("foo", "bar"); |
||||
Pojo pojo2 = new Pojo("foofoo", "barbar"); |
||||
Pojo pojo3 = new Pojo("foofoofoo", "barbarbar"); |
||||
Flux<Pojo> input = Flux.just(pojo1, pojo2, pojo3); |
||||
ResolvableType type = ResolvableType.forClass(Pojo.class); |
||||
encoder.encode(input, this.bufferFactory, type, CBOR_MIME_TYPE, null); |
||||
} |
||||
} |
Loading…
Reference in new issue