From 13cc434b45d92ef6bc0143737d7d779a1e544549 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Mon, 1 Jul 2019 17:19:27 +0200 Subject: [PATCH] Remove support of old TLV/JSON code by default. --- .../node/codec/DefaultLwM2mNodeDecoder.java | 48 ++++++++++++++--- .../node/codec/DefaultLwM2mNodeEncoder.java | 51 ++++++++++++++++--- .../tests/IntegrationTestHelper.java | 4 ++ 3 files changed, 91 insertions(+), 12 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeDecoder.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeDecoder.java index 05b1235aa1..6e2af28efb 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeDecoder.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeDecoder.java @@ -36,10 +36,41 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * A default {@link LwM2mNodeDecoder} which support default {@link ContentFormat} : + *

+ *

+ */ public class DefaultLwM2mNodeDecoder implements LwM2mNodeDecoder { private static final Logger LOG = LoggerFactory.getLogger(DefaultLwM2mNodeDecoder.class); + protected final boolean supportDeprecatedContentFormat; + + /** + * Create {@link DefaultLwM2mNodeDecoder} without support of old TLV and JSON code. + */ + public DefaultLwM2mNodeDecoder() { + this(false); + } + + /** + * Create {@link DefaultLwM2mNodeDecoder} allowing to enable support for old TLV and JSON code. + *

+ * Those old codes was used by the LWM2M specification before the official v1.0.0 release and could still be needed + * for backward compatibility. + * + * @param supportDeprecatedContentFormat True to accept to decode old code. + */ + public DefaultLwM2mNodeDecoder(boolean supportDeprecatedContentFormat) { + this.supportDeprecatedContentFormat = supportDeprecatedContentFormat; + } + @Override public LwM2mNode decode(byte[] content, ContentFormat format, LwM2mPath path, LwM2mModel model) throws CodecException { @@ -58,6 +89,10 @@ public T decode(byte[] content, ContentFormat format, LwM2 throw new CodecException("Content format is mandatory. [%s]", path); } + if (!isSupported(format)) { + throw new CodecException("Content format %s is not supported [%s]", format, path); + } + // Decode content. switch (format.getCode()) { case ContentFormat.TEXT_CODE: @@ -70,8 +105,6 @@ public T decode(byte[] content, ContentFormat format, LwM2 case ContentFormat.JSON_CODE: case ContentFormat.OLD_JSON_CODE: return LwM2mNodeJsonDecoder.decode(content, path, model, nodeClass); - case ContentFormat.LINK_CODE: - throw new CodecException("Content format %s not yet implemented [%s]", format, path); default: throw new CodecException("Content format %s is not supported [%s]", format, path); } @@ -87,6 +120,10 @@ public List decodeTimestampedData(byte[] content, ContentF throw new CodecException("Content format is mandatory. [%s]", path); } + if (!isSupported(format)) { + throw new CodecException("Content format %s is not supported [%s]", format, path); + } + // Decode content. switch (format.getCode()) { case ContentFormat.TEXT_CODE: @@ -99,8 +136,6 @@ public List decodeTimestampedData(byte[] content, ContentF case ContentFormat.JSON_CODE: case ContentFormat.OLD_JSON_CODE: return LwM2mNodeJsonDecoder.decodeTimestamped(content, path, model, nodeClassFromPath(path)); - case ContentFormat.LINK_CODE: - throw new CodecException("Content format %s not yet implemented [%s]", format, path); default: throw new CodecException("Content format %s is not supported [%s]", format, path); } @@ -131,11 +166,12 @@ public boolean isSupported(ContentFormat format) { switch (format.getCode()) { case ContentFormat.TEXT_CODE: case ContentFormat.TLV_CODE: - case ContentFormat.OLD_TLV_CODE: case ContentFormat.OPAQUE_CODE: case ContentFormat.JSON_CODE: - case ContentFormat.OLD_JSON_CODE: return true; + case ContentFormat.OLD_TLV_CODE: + case ContentFormat.OLD_JSON_CODE: + return supportDeprecatedContentFormat; default: return false; } diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeEncoder.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeEncoder.java index 0a9c7db9e0..e83f5c0b12 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeEncoder.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/codec/DefaultLwM2mNodeEncoder.java @@ -31,18 +31,49 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * A default {@link LwM2mNodeEncoder} which support default {@link ContentFormat} : + *

+ *

+ */ public class DefaultLwM2mNodeEncoder implements LwM2mNodeEncoder { private static final Logger LOG = LoggerFactory.getLogger(DefaultLwM2mNodeEncoder.class); - private final LwM2mValueConverter converter; + protected final LwM2mValueConverter converter; + protected final boolean supportDeprecatedContentFormat; + /** + * Create {@link DefaultLwM2mNodeEncoder} without support of old TLV and JSON code. + */ public DefaultLwM2mNodeEncoder() { this(new DefaultLwM2mValueConverter()); } public DefaultLwM2mNodeEncoder(LwM2mValueConverter converter) { + this(converter, false); + } + + /** + * Create {@link DefaultLwM2mNodeEncoder} allowing to enable support for old TLV and JSON code. + *

+ * Those old codes was used by the LWM2M specification before the official v1.0.0 release and could still be needed + * for backward compatibility. + * + * @param supportDeprecatedContentFormat True to accept to encode old code. + */ + public DefaultLwM2mNodeEncoder(boolean supportDeprecatedContentFormat) { + this(new DefaultLwM2mValueConverter(), supportDeprecatedContentFormat); + } + + public DefaultLwM2mNodeEncoder(LwM2mValueConverter converter, boolean supportDeprecatedContentFormat) { this.converter = converter; + this.supportDeprecatedContentFormat = supportDeprecatedContentFormat; } @Override @@ -53,8 +84,11 @@ public byte[] encode(LwM2mNode node, ContentFormat format, LwM2mPath path, LwM2m throw new CodecException("Content format is mandatory. [%s]", path); } - LOG.debug("Encoding node {} for path {} and format {}", node, path, format); + if (!isSupported(format)) { + throw new CodecException("Content format %s is not supported [%s]", format, path); + } + LOG.trace("Encoding node {} for path {} and format {}", node, path, format); byte[] encoded; switch (format.getCode()) { case ContentFormat.TLV_CODE: @@ -72,7 +106,7 @@ public byte[] encode(LwM2mNode node, ContentFormat format, LwM2mPath path, LwM2m encoded = LwM2mNodeJsonEncoder.encode(node, path, model, converter); break; default: - throw new CodecException("Cannot encode %s:%s with format %s.", path, node, format); + throw new CodecException("Content format %s is not supported [%s]", format, path); } LOG.trace("Encoded node {}: {}", node, encoded); return encoded; @@ -86,11 +120,15 @@ public byte[] encodeTimestampedData(List timestampedNodes, throw new CodecException("Content format is mandatory. [%s]", path); } - LOG.debug("Encoding time-stamped nodes for path {} and format {}", timestampedNodes, path, format); + if (!isSupported(format)) { + throw new CodecException("Content format %s is not supported [%s]", format, path); + } + LOG.trace("Encoding time-stamped nodes for path {} and format {}", timestampedNodes, path, format); byte[] encoded; switch (format.getCode()) { case ContentFormat.JSON_CODE: + case ContentFormat.OLD_JSON_CODE: encoded = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, path, model, converter); break; default: @@ -106,11 +144,12 @@ public boolean isSupported(ContentFormat format) { switch (format.getCode()) { case ContentFormat.TEXT_CODE: case ContentFormat.TLV_CODE: - case ContentFormat.OLD_TLV_CODE: case ContentFormat.OPAQUE_CODE: case ContentFormat.JSON_CODE: - case ContentFormat.OLD_JSON_CODE: return true; + case ContentFormat.OLD_TLV_CODE: + case ContentFormat.OLD_JSON_CODE: + return supportDeprecatedContentFormat; default: return false; } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/IntegrationTestHelper.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/IntegrationTestHelper.java index 57123cad53..d4e5edb557 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/IntegrationTestHelper.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/IntegrationTestHelper.java @@ -43,6 +43,8 @@ import org.eclipse.leshan.core.model.ResourceModel; import org.eclipse.leshan.core.model.ResourceModel.Operations; import org.eclipse.leshan.core.model.ResourceModel.Type; +import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder; +import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder; import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.core.response.ExecuteResponse; import org.eclipse.leshan.server.californium.LeshanServerBuilder; @@ -157,6 +159,7 @@ public void createClient(Map additionalAttributes) { // Build Client LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get()); + builder.setDecoder(new DefaultLwM2mNodeDecoder(true)); builder.setAdditionalAttributes(additionalAttributes); builder.setObjects(objects); client = builder.build(); @@ -171,6 +174,7 @@ public void createServer() { protected LeshanServerBuilder createServerBuilder() { LeshanServerBuilder builder = new LeshanServerBuilder(); + builder.setEncoder(new DefaultLwM2mNodeEncoder(true)); builder.setObjectModelProvider(new StaticModelProvider(createObjectModels())); builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));