Skip to content

Commit 5cd6763

Browse files
committed
Add CBOR codec (single value only)
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-22767
1 parent 3d502d9 commit 5cd6763

File tree

4 files changed

+327
-0
lines changed

4 files changed

+327
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec.cbor;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
23+
import org.reactivestreams.Publisher;
24+
import reactor.core.publisher.Flux;
25+
26+
import org.springframework.core.ResolvableType;
27+
import org.springframework.core.io.buffer.DataBuffer;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.codec.json.AbstractJackson2Decoder;
30+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
31+
import org.springframework.util.Assert;
32+
import org.springframework.util.MimeType;
33+
34+
/**
35+
* Decode bytes into CBOR and convert to Object's with Jackson.
36+
* Stream decoding is not supported yet.
37+
*
38+
* @author Sebastien Deleuze
39+
* @since 5.2
40+
* @see Jackson2CborEncoder
41+
* @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a>
42+
*/
43+
public class Jackson2CborDecoder extends AbstractJackson2Decoder {
44+
45+
public Jackson2CborDecoder() {
46+
this(Jackson2ObjectMapperBuilder.cbor().build(), new MediaType("application", "cbor"));
47+
}
48+
49+
public Jackson2CborDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
50+
super(mapper, mimeTypes);
51+
Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass());
52+
}
53+
54+
@Override
55+
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
56+
throw new UnsupportedOperationException("Does not support stream decoding yet");
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec.cbor;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
23+
import org.reactivestreams.Publisher;
24+
import reactor.core.publisher.Flux;
25+
26+
import org.springframework.core.ResolvableType;
27+
import org.springframework.core.io.buffer.DataBuffer;
28+
import org.springframework.core.io.buffer.DataBufferFactory;
29+
import org.springframework.http.MediaType;
30+
import org.springframework.http.codec.json.AbstractJackson2Encoder;
31+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
32+
import org.springframework.util.Assert;
33+
import org.springframework.util.MimeType;
34+
35+
/**
36+
* Encode from an {@code Object} to bytes of CBOR objects using Jackson.
37+
* Stream encoding is not supported yet.
38+
*
39+
* @author Sebastien Deleuze
40+
* @since 5.2
41+
* @see Jackson2CborDecoder
42+
* @see <a href="https://github.com/spring-projects/spring-framework/issues/20513">Add CBOR support to WebFlux</a>
43+
*/
44+
public class Jackson2CborEncoder extends AbstractJackson2Encoder {
45+
46+
public Jackson2CborEncoder() {
47+
this(Jackson2ObjectMapperBuilder.cbor().build(), new MediaType("application", "cbor"));
48+
}
49+
50+
public Jackson2CborEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
51+
super(mapper, mimeTypes);
52+
Assert.isAssignable(CBORFactory.class, mapper.getFactory().getClass());
53+
}
54+
55+
@Override
56+
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
57+
throw new UnsupportedOperationException("Does not support stream encoding yet");
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec.cbor;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import com.fasterxml.jackson.core.JsonProcessingException;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.junit.Test;
25+
import reactor.core.publisher.Flux;
26+
27+
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.codec.AbstractDecoderTestCase;
29+
import org.springframework.core.io.buffer.DataBuffer;
30+
import org.springframework.http.codec.Pojo;
31+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
32+
import org.springframework.util.MimeType;
33+
34+
import static org.junit.Assert.assertFalse;
35+
import static org.junit.Assert.assertTrue;
36+
import static org.springframework.core.ResolvableType.forClass;
37+
import static org.springframework.http.MediaType.APPLICATION_JSON;
38+
39+
/**
40+
* Unit tests for {@link Jackson2CborDecoder}.
41+
*
42+
* @author Sebastien Deleuze
43+
*/
44+
public class Jackson2CborDecoderTests extends AbstractDecoderTestCase<Jackson2CborDecoder> {
45+
46+
private final static MimeType CBOR_MIME_TYPE = new MimeType("application", "cbor");
47+
48+
private Pojo pojo1 = new Pojo("f1", "b1");
49+
50+
private Pojo pojo2 = new Pojo("f2", "b2");
51+
52+
private ObjectMapper mapper = Jackson2ObjectMapperBuilder.cbor().build();
53+
54+
public Jackson2CborDecoderTests() {
55+
super(new Jackson2CborDecoder());
56+
}
57+
58+
@Override
59+
@Test
60+
public void canDecode() {
61+
assertTrue(decoder.canDecode(forClass(Pojo.class), CBOR_MIME_TYPE));
62+
assertTrue(decoder.canDecode(forClass(Pojo.class), null));
63+
64+
assertFalse(decoder.canDecode(forClass(String.class), null));
65+
assertFalse(decoder.canDecode(forClass(Pojo.class), APPLICATION_JSON));
66+
}
67+
68+
@Override
69+
@Test(expected = UnsupportedOperationException.class)
70+
public void decode() {
71+
Flux<DataBuffer> input = Flux.just(this.pojo1, this.pojo2)
72+
.map(this::writeObject)
73+
.flatMap(this::dataBuffer);
74+
75+
testDecodeAll(input, Pojo.class, step -> step
76+
.expectNext(pojo1)
77+
.expectNext(pojo2)
78+
.verifyComplete());
79+
80+
}
81+
82+
private byte[] writeObject(Object o) {
83+
try {
84+
return this.mapper.writer().writeValueAsBytes(o);
85+
}
86+
catch (JsonProcessingException e) {
87+
throw new AssertionError(e);
88+
}
89+
90+
}
91+
92+
@Override
93+
public void decodeToMono() {
94+
List<Pojo> expected = Arrays.asList(pojo1, pojo2);
95+
96+
Flux<DataBuffer> input = Flux.just(expected)
97+
.map(this::writeObject)
98+
.flatMap(this::dataBuffer);
99+
100+
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
101+
testDecodeToMono(input, elementType, step -> step
102+
.expectNext(expected)
103+
.expectComplete()
104+
.verify(), null, null);
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec.cbor;
18+
19+
import java.io.IOException;
20+
import java.io.UncheckedIOException;
21+
import java.util.function.Consumer;
22+
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.junit.Test;
25+
import reactor.core.publisher.Flux;
26+
27+
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.io.buffer.AbstractLeakCheckingTestCase;
29+
import org.springframework.core.io.buffer.DataBuffer;
30+
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
31+
import org.springframework.http.codec.Pojo;
32+
import org.springframework.http.codec.ServerSentEvent;
33+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
34+
import org.springframework.util.MimeType;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertFalse;
38+
import static org.junit.Assert.assertTrue;
39+
import static org.springframework.core.io.buffer.DataBufferUtils.release;
40+
import static org.springframework.http.MediaType.APPLICATION_XML;
41+
42+
/**
43+
* Unit tests for {@link Jackson2CborEncoder}.
44+
*
45+
* @author Sebastien Deleuze
46+
*/
47+
public class Jackson2CborEncoderTests extends AbstractLeakCheckingTestCase {
48+
49+
private final static MimeType CBOR_MIME_TYPE = new MimeType("application", "cbor");
50+
51+
private final ObjectMapper mapper = Jackson2ObjectMapperBuilder.cbor().build();
52+
53+
private final Jackson2CborEncoder encoder = new Jackson2CborEncoder();
54+
55+
private Consumer<DataBuffer> pojoConsumer(Pojo expected) {
56+
return dataBuffer -> {
57+
try {
58+
Pojo actual = this.mapper.reader().forType(Pojo.class)
59+
.readValue(DataBufferTestUtils.dumpBytes(dataBuffer));
60+
assertEquals(expected, actual);
61+
release(dataBuffer);
62+
}
63+
catch (IOException ex) {
64+
throw new UncheckedIOException(ex);
65+
}
66+
};
67+
}
68+
69+
@Test
70+
public void canEncode() {
71+
ResolvableType pojoType = ResolvableType.forClass(Pojo.class);
72+
assertTrue(this.encoder.canEncode(pojoType, CBOR_MIME_TYPE));
73+
assertTrue(this.encoder.canEncode(pojoType, null));
74+
75+
// SPR-15464
76+
assertTrue(this.encoder.canEncode(ResolvableType.NONE, null));
77+
}
78+
79+
@Test
80+
public void canNotEncode() {
81+
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), null));
82+
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Pojo.class), APPLICATION_XML));
83+
84+
ResolvableType sseType = ResolvableType.forClass(ServerSentEvent.class);
85+
assertFalse(this.encoder.canEncode(sseType, CBOR_MIME_TYPE));
86+
}
87+
88+
@Test
89+
public void encode() {
90+
Pojo value = new Pojo("foo", "bar");
91+
DataBuffer result = encoder.encodeValue(value, this.bufferFactory, ResolvableType.forClass(Pojo.class), CBOR_MIME_TYPE, null);
92+
pojoConsumer(value).accept(result);
93+
}
94+
95+
@Test(expected = UnsupportedOperationException.class)
96+
public void encodeStream() {
97+
Pojo pojo1 = new Pojo("foo", "bar");
98+
Pojo pojo2 = new Pojo("foofoo", "barbar");
99+
Pojo pojo3 = new Pojo("foofoofoo", "barbarbar");
100+
Flux<Pojo> input = Flux.just(pojo1, pojo2, pojo3);
101+
ResolvableType type = ResolvableType.forClass(Pojo.class);
102+
encoder.encode(input, this.bufferFactory, type, CBOR_MIME_TYPE, null);
103+
}
104+
}

0 commit comments

Comments
 (0)