Skip to content

Commit

Permalink
Do not do type conversion at encoding time by default anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 9, 2019
1 parent 5e344d3 commit 680ee7d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DefaultLwM2mNodeEncoder implements LwM2mNodeEncoder {
* Create {@link DefaultLwM2mNodeEncoder} without support of old TLV and JSON code.
*/
public DefaultLwM2mNodeEncoder() {
this(new DefaultLwM2mValueConverter());
this(new LwM2mValueChecker());
}

public DefaultLwM2mNodeEncoder(LwM2mValueConverter converter) {
Expand All @@ -68,7 +68,7 @@ public DefaultLwM2mNodeEncoder(LwM2mValueConverter converter) {
* @param supportDeprecatedContentFormat True to accept to encode old code.
*/
public DefaultLwM2mNodeEncoder(boolean supportDeprecatedContentFormat) {
this(new DefaultLwM2mValueConverter(), supportDeprecatedContentFormat);
this(new LwM2mValueChecker(), supportDeprecatedContentFormat);
}

public DefaultLwM2mNodeEncoder(LwM2mValueConverter converter, boolean supportDeprecatedContentFormat) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2019 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.node.codec;

import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mPath;

/**
* A {@link LwM2mValueConverter} which do no conversion but raise a {@link CodecException} if type is not the expected
* one.
*
*/
public class LwM2mValueChecker implements LwM2mValueConverter {

@Override
public Object convertValue(Object value, Type currentType, Type expectedType, LwM2mPath resourcePath) {
if (expectedType == null) {
// unknown resource, trusted value
return value;
}

if (currentType == expectedType) {
// expected type
return value;
}

throw new CodecException("Invalid value type for resource %s, expected %s, got %s", resourcePath, expectedType,
currentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*******************************************************************************/
package org.eclipse.leshan.core.node.codec;

import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mResource;

/**
* Helper to convert value "magically" from one type to another.
* <p>
* This is used by {@link LwM2mNodeEncoder} to fix {@link LwM2mResource} which would used a different
* {@link ResourceModel.Type} than the one defined in the {@link ResourceModel}.
*/
public interface LwM2mValueConverter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void loadModel() {
}

@Test
public void text_encode_single_resource() {
public void text_encode_single_resource_float() {

byte[] encoded = encoder.encode(LwM2mSingleResource.newFloatResource(15, 56.4D), ContentFormat.TEXT,
new LwM2mPath("/323/0/15"), model);
Expand All @@ -63,20 +63,11 @@ public void text_encode_single_resource() {
}

@Test
public void text_encode_date_as_long() {
public void text_encode_single_resource_date() {

byte[] encoded = encoder.encode(LwM2mSingleResource.newStringResource(13, "2010-01-01T12:00:00+01:00"),
byte[] encoded = encoder.encode(LwM2mSingleResource.newDateResource(13, new Date(1367491215000L)),
ContentFormat.TEXT, new LwM2mPath("/3/0/13"), model);

Assert.assertEquals("1262343600", new String(encoded, StandardCharsets.UTF_8));
}

@Test
public void text_encode_date_as_iso_string() {

byte[] encoded = encoder.encode(LwM2mSingleResource.newIntegerResource(13, 1367491215000L), ContentFormat.TEXT,
new LwM2mPath("/3/0/13"), model);

Assert.assertEquals("1367491215", new String(encoded, StandardCharsets.UTF_8));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void cannot_create_instance_with_non_writable_resource() {
public void cannot_create_mandatory_single_object() throws InterruptedException {
// try to create another instance of device object
CreateResponse response = helper.server.send(helper.getCurrentRegistration(),
new CreateRequest(3, new LwM2mResource[] { LwM2mSingleResource.newIntegerResource(3, 123) }));
new CreateRequest(3, new LwM2mResource[] { LwM2mSingleResource.newStringResource(3, "v123") }));

// verify result
assertEquals(ResponseCode.BAD_REQUEST, response.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.TimestampedLwM2mNode;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter;
import org.eclipse.leshan.core.node.codec.LwM2mValueChecker;
import org.eclipse.leshan.core.node.codec.json.LwM2mNodeJsonEncoder;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.request.CancelObservationRequest;
Expand Down Expand Up @@ -324,8 +324,7 @@ public void can_handle_error_on_notification() throws InterruptedException {

// *** HACK send a notification with unsupported content format *** //
byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"),
new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()),
new DefaultLwM2mValueConverter());
new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666); // 666 is not a supported //
// contentFormat.
Expand Down Expand Up @@ -367,7 +366,7 @@ public void can_observe_timestamped_resource() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
LwM2mSingleResource.newStringResource(15, "Londres")));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3/0/15"),
new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
// *** Hack End *** //
Expand Down Expand Up @@ -410,7 +409,7 @@ public void can_observe_timestamped_instance() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres"))));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3/0"),
new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
// *** Hack End *** //
Expand Down Expand Up @@ -453,7 +452,7 @@ public void can_observe_timestamped_object() throws InterruptedException {
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2,
new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"),
new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
new LwM2mModel(helper.createObjectModels()), new LwM2mValueChecker());

Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.eclipse.leshan.server.demo.servlet.EventServlet;
import org.eclipse.leshan.server.demo.servlet.ObjectSpecServlet;
import org.eclipse.leshan.server.demo.servlet.SecurityServlet;
import org.eclipse.leshan.server.demo.utils.MagicLwM2mValueConverter;
import org.eclipse.leshan.server.impl.FileSecurityStore;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.model.StaticModelProvider;
Expand Down Expand Up @@ -375,6 +376,9 @@ public X509Certificate[] getAcceptedIssuers() {
}
builder.setSecurityStore(securityStore);

// use a magic converter to support bad type send by the UI.
builder.setEncoder(new DefaultLwM2mNodeEncoder(new MagicLwM2mValueConverter()));

// Create and start LWM2M server
LeshanServer lwServer = builder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* javax.xml.bind.DatatypeConverter
* which is not available on Android
*******************************************************************************/
package org.eclipse.leshan.core.node.codec;
package org.eclipse.leshan.server.demo.utils;

import java.util.Date;

Expand All @@ -26,14 +26,19 @@

import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.LwM2mValueConverter;
import org.eclipse.leshan.util.Hex;
import org.eclipse.leshan.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultLwM2mValueConverter implements LwM2mValueConverter {
/**
* A {@link LwM2mValueConverter} which will do some magic conversion.
*/
public class MagicLwM2mValueConverter implements LwM2mValueConverter {

private static final Logger LOG = LoggerFactory.getLogger(DefaultLwM2mValueConverter.class);
private static final Logger LOG = LoggerFactory.getLogger(MagicLwM2mValueConverter.class);

@Override
public Object convertValue(Object value, Type currentType, Type expectedType, LwM2mPath resourcePath)
Expand Down

0 comments on commit 680ee7d

Please sign in to comment.