Skip to content

Commit

Permalink
#574: Add API to choose default content type as client side
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Dec 3, 2018
1 parent 9f54a26 commit 3a565f8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.leshan.core.request.CreateRequest;
import org.eclipse.leshan.core.request.DeleteRequest;
import org.eclipse.leshan.core.request.DiscoverRequest;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.ExecuteRequest;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.ReadRequest;
Expand Down Expand Up @@ -128,22 +129,25 @@ public void handleGET(CoapExchange exchange) {
}
} else {
// handle content format for Read and Observe Request
ContentFormat format = ContentFormat.TLV; // use TLV as default format
ContentFormat requestedContentFormat = null;
if (exchange.getRequestOptions().hasAccept()) {
format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
if (!encoder.isSupported(format)) {
// If an request ask for a specific content format, use it (if we support it)
requestedContentFormat = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
if (!encoder.isSupported(requestedContentFormat)) {
exchange.respond(ResponseCode.NOT_ACCEPTABLE);
return;
}
}

// Manage Observe Request
if (exchange.getRequestOptions().hasObserve()) {
ObserveResponse response = nodeEnabler.observe(identity, new ObserveRequest(URI));
ObserveRequest observeRequest = new ObserveRequest(URI);
ObserveResponse response = nodeEnabler.observe(identity, observeRequest);
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(observeRequest, requestedContentFormat);
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
return;
Expand All @@ -154,11 +158,13 @@ public void handleGET(CoapExchange exchange) {
}
// Manage Read Request
else {
ReadResponse response = nodeEnabler.read(identity, new ReadRequest(URI));
ReadRequest readRequest = new ReadRequest(URI);
ReadResponse response = nodeEnabler.read(identity, readRequest);
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(readRequest, requestedContentFormat);
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model),
format.getCode());
return;
Expand All @@ -170,6 +176,16 @@ public void handleGET(CoapExchange exchange) {
}
}

private ContentFormat getContentFormat(DownlinkRequest<?> request, ContentFormat requestedContentFormat) {
if (requestedContentFormat != null) {
// we already check before this content format is supported.
return requestedContentFormat;
}

ContentFormat format = nodeEnabler.getDefaultEncodingFormat(request);
return format == null ? ContentFormat.DEFAULT : format;
}

@Override
public void handlePUT(CoapExchange coapExchange) {
ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.request.BootstrapWriteRequest;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.CreateRequest;
import org.eclipse.leshan.core.request.DeleteRequest;
import org.eclipse.leshan.core.request.DiscoverRequest;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.ExecuteRequest;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.ReadRequest;
Expand Down Expand Up @@ -387,4 +389,9 @@ public void setNotifySender(NotifySender sender) {
public NotifySender getNotifySender() {
return notifySender;
}

@Override
public ContentFormat getDefaultEncodingFormat(DownlinkRequest<?> request) {
return ContentFormat.DEFAULT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.request.BootstrapWriteRequest;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.CreateRequest;
import org.eclipse.leshan.core.request.DeleteRequest;
import org.eclipse.leshan.core.request.DiscoverRequest;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.ExecuteRequest;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.ReadRequest;
Expand Down Expand Up @@ -72,4 +74,6 @@ public interface LwM2mObjectEnabler {
ObserveResponse observe(ServerIdentity identity, ObserveRequest request);

void setNotifySender(NotifySender sender);

ContentFormat getDefaultEncodingFormat(DownlinkRequest<?> request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.request.BootstrapDeleteRequest;
import org.eclipse.leshan.core.request.BootstrapWriteRequest;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.CreateRequest;
import org.eclipse.leshan.core.request.DeleteRequest;
import org.eclipse.leshan.core.request.DownlinkRequest;
import org.eclipse.leshan.core.request.ExecuteRequest;
import org.eclipse.leshan.core.request.ObserveRequest;
import org.eclipse.leshan.core.request.ReadRequest;
Expand All @@ -56,12 +58,14 @@ public class ObjectEnabler extends BaseObjectEnabler {

private Map<Integer, LwM2mInstanceEnabler> instances;
private LwM2mInstanceEnablerFactory instanceFactory;
private ContentFormat defaultContentFormat;

public ObjectEnabler(int id, ObjectModel objectModel, Map<Integer, LwM2mInstanceEnabler> instances,
LwM2mInstanceEnablerFactory instanceFactory) {
LwM2mInstanceEnablerFactory instanceFactory, ContentFormat defaultContentFormat) {
super(id, objectModel);
this.instances = new HashMap<>(instances);
this.instanceFactory = instanceFactory;
this.defaultContentFormat = defaultContentFormat;
for (Entry<Integer, LwM2mInstanceEnabler> entry : this.instances.entrySet()) {
addInstance(entry.getKey(), entry.getValue());
}
Expand Down Expand Up @@ -346,4 +350,8 @@ public void resourcesChanged(int... resourceIds) {
});
}

@Override
public ContentFormat getDefaultEncodingFormat(DownlinkRequest<?> request) {
return defaultContentFormat;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.util.Validate;

public class ObjectsInitializer {
Expand All @@ -39,6 +40,7 @@ public LwM2mInstanceEnabler create(ObjectModel model) {

protected Map<Integer, LwM2mInstanceEnablerFactory> factories = new HashMap<>();
protected Map<Integer, LwM2mInstanceEnabler[]> instances = new HashMap<>();
protected Map<Integer, ContentFormat> defaultContentFormat = new HashMap<>();
protected LwM2mModel model;

public ObjectsInitializer() {
Expand Down Expand Up @@ -88,6 +90,10 @@ public void setInstancesForObject(int objectId, LwM2mInstanceEnabler... instance
this.instances.put(objectId, instances);
}

public void setDefaultContentFormat(int objectId, ContentFormat format) {
defaultContentFormat.put(objectId, format);
}

public List<LwM2mObjectEnabler> createMandatory() {
Collection<ObjectModel> objectModels = model.getObjectModels();

Expand Down Expand Up @@ -151,7 +157,16 @@ protected ObjectEnabler createNodeEnabler(ObjectModel objectModel) {
for (int i = 0; i < newInstances.length; i++) {
instances.put(i, newInstances[i]);
}
return new ObjectEnabler(objectModel.id, objectModel, instances, getFactoryFor(objectModel));
return new ObjectEnabler(objectModel.id, objectModel, instances, getFactoryFor(objectModel),
getContentFormat(objectModel.id));
}

protected ContentFormat getContentFormat(int id) {
ContentFormat contentFormat = defaultContentFormat.get(id);
if (contentFormat != null) {
return contentFormat;
}
return ContentFormat.DEFAULT;
}

protected LwM2mInstanceEnabler[] createInstances(ObjectModel objectModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ObjectModel;
import org.eclipse.leshan.core.request.ContentFormat;
import org.junit.Test;

public class LinkFormatHelperTest {
Expand Down Expand Up @@ -106,7 +107,7 @@ public void encode_client_description_with_version_1_0() {

Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
instancesMap.put(0, new BaseInstanceEnabler());
objectEnablers.add(new ObjectEnabler(6, getObjectModel(6), instancesMap, null));
objectEnablers.add(new ObjectEnabler(6, getObjectModel(6), instancesMap, null, ContentFormat.DEFAULT));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);
Expand All @@ -121,7 +122,8 @@ public void encode_client_description_with_version_2_0() {
Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
instancesMap.put(0, new BaseInstanceEnabler());
instancesMap.put(1, new BaseInstanceEnabler());
objectEnablers.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null));
objectEnablers
.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null, ContentFormat.DEFAULT));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);
Expand All @@ -134,7 +136,8 @@ public void encode_client_description_with_version_2_0_no_instances() {
List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();

Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
objectEnablers.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null));
objectEnablers
.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null, ContentFormat.DEFAULT));

Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);
Expand Down Expand Up @@ -183,6 +186,6 @@ public LwM2mInstanceEnabler create(ObjectModel model) {
instances.put(0, factory.create(objectModel));

// create objectEnabler
return new ObjectEnabler(objectModel.id, objectModel, instances, factory);
return new ObjectEnabler(objectModel.id, objectModel, instances, factory, ContentFormat.TLV);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ContentFormat {
public static final ContentFormat OPAQUE = new ContentFormat("OPAQUE", "application/octet-stream", OPAQUE_CODE);
public static final ContentFormat LINK = new ContentFormat("LINK", "application/link-format", LINK_CODE);

public static final ContentFormat DEFAULT = TLV;

private static final ContentFormat knownContentFormat[] = new ContentFormat[] { TLV, JSON, TEXT, OPAQUE, LINK };

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ public void register_observe_deregister_observe() throws NonUniqueSecurityInfoEx
// TODO not really a registration test
@Test(expected = IllegalArgumentException.class)
public void fail_to_create_client_with_same_object_twice() {
ObjectEnabler objectEnabler = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
ObjectEnabler objectEnabler2 = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
ObjectEnabler objectEnabler = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null,
ContentFormat.DEFAULT);
ObjectEnabler objectEnabler2 = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null,
ContentFormat.DEFAULT);
ArrayList<LwM2mObjectEnabler> objects = new ArrayList<>();
objects.add(objectEnabler);
objects.add(objectEnabler2);
Expand Down

0 comments on commit 3a565f8

Please sign in to comment.