Skip to content

Commit

Permalink
Remove support of old TLV/JSON code by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent ae3f4c2 commit 13cc434
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,41 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A default {@link LwM2mNodeDecoder} which support default {@link ContentFormat} :
* <p>
* <ul>
* <li>{@link ContentFormat#TLV}</li>
* <li>{@link ContentFormat#JSON}</li>
* <li>{@link ContentFormat#TEXT}</li>
* <li>{@link ContentFormat#OPAQUE}</li>
* </ul>
*/
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.
* <p>
* 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 {
Expand All @@ -58,6 +89,10 @@ public <T extends LwM2mNode> 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:
Expand All @@ -70,8 +105,6 @@ public <T extends LwM2mNode> 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);
}
Expand All @@ -87,6 +120,10 @@ public List<TimestampedLwM2mNode> 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:
Expand All @@ -99,8 +136,6 @@ public List<TimestampedLwM2mNode> 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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,49 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A default {@link LwM2mNodeEncoder} which support default {@link ContentFormat} :
* <p>
* <ul>
* <li>{@link ContentFormat#TLV}</li>
* <li>{@link ContentFormat#JSON}</li>
* <li>{@link ContentFormat#TEXT}</li>
* <li>{@link ContentFormat#OPAQUE}</li>
* </ul>
*/
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.
* <p>
* 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
Expand All @@ -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:
Expand All @@ -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;
Expand All @@ -86,11 +120,15 @@ public byte[] encodeTimestampedData(List<TimestampedLwM2mNode> 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:
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,6 +159,7 @@ public void createClient(Map<String, String> additionalAttributes) {

// Build Client
LeshanClientBuilder builder = new LeshanClientBuilder(currentEndpointIdentifier.get());
builder.setDecoder(new DefaultLwM2mNodeDecoder(true));
builder.setAdditionalAttributes(additionalAttributes);
builder.setObjects(objects);
client = builder.build();
Expand All @@ -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));
Expand Down

0 comments on commit 13cc434

Please sign in to comment.