Search in sources :

Example 6 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_explicit_empty_root_path.

@Test
public void encode_objectModel_to_linkObject_with_explicit_empty_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/");
    String strLinks = Link.serialize(links);
    assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 7 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class RegistrationSortObjectLinksTest method sort_link_object_on_get.

@Test
public void sort_link_object_on_get() throws UnknownHostException {
    Link[] objs = new Link[3];
    objs[0] = new Link("/0/1024/2");
    objs[1] = new Link("/0/2");
    objs[2] = null;
    Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", Identity.unsecure(Inet4Address.getLocalHost(), 1), new InetSocketAddress(212)).objectLinks(objs);
    Registration r = builder.build();
    Link[] res = r.getSortedObjectLinks();
    Assert.assertEquals(3, res.length);
    Assert.assertNull(res[0]);
    Assert.assertEquals("/0/2", res[1].getUrl());
    Assert.assertEquals("/0/1024/2", res[2].getUrl());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 8 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class RegistrationUpdateSerDes method jSerialize.

public static JsonObject jSerialize(RegistrationUpdate u) {
    JsonObject o = Json.object();
    // mandatory fields
    o.add("regId", u.getRegistrationId());
    o.add("identity", IdentitySerDes.serialize(u.getIdentity()));
    // optional fields
    if (u.getLifeTimeInSec() != null)
        o.add("lt", u.getLifeTimeInSec());
    if (u.getSmsNumber() != null)
        o.add("sms", u.getSmsNumber());
    if (u.getBindingMode() != null)
        o.add("bnd", u.getBindingMode().name());
    if (u.getObjectLinks() != null) {
        JsonArray links = new JsonArray();
        for (Link l : u.getObjectLinks()) {
            JsonObject ol = Json.object();
            ol.add("url", l.getUrl());
            JsonObject at = Json.object();
            for (Map.Entry<String, Object> e : l.getAttributes().entrySet()) {
                if (e.getValue() == null) {
                    at.add(e.getKey(), Json.NULL);
                } else if (e.getValue() instanceof Integer) {
                    at.add(e.getKey(), (int) e.getValue());
                } else {
                    at.add(e.getKey(), e.getValue().toString());
                }
            }
            ol.add("at", at);
            links.add(ol);
        }
        o.add("objLink", links);
    }
    if (u.getAdditionalAttributes() != null) {
        JsonObject addAttr = Json.object();
        for (Map.Entry<String, String> e : u.getAdditionalAttributes().entrySet()) {
            addAttr.add(e.getKey(), e.getValue());
        }
        o.add("addAttr", addAttr);
    }
    return o;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap) Link(org.eclipse.leshan.Link)

Example 9 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class RegistrationUpdateSerDes method deserialize.

public static RegistrationUpdate deserialize(byte[] data) throws UnknownHostException {
    JsonObject v = (JsonObject) Json.parse(new String(data));
    // mandatory fields
    String regId = v.getString("regId", null);
    Identity identity = IdentitySerDes.deserialize(v.get("identity").asObject());
    // optional fields
    BindingMode b = null;
    if (v.get("bnd") != null) {
        b = BindingMode.valueOf(v.getString("bnd", null));
    }
    Long lifetime = null;
    if (v.get("lt") != null) {
        lifetime = v.getLong("lt", 0);
    }
    String sms = null;
    if (v.get("sms") != null) {
        sms = v.getString("sms", "");
    }
    // parse object link
    JsonArray links = (JsonArray) v.get("objLink");
    Link[] linkObjs = null;
    if (links != null) {
        linkObjs = new Link[links.size()];
        for (int i = 0; i < links.size(); i++) {
            JsonObject ol = (JsonObject) links.get(i);
            Map<String, Object> attMap = new HashMap<>();
            JsonObject att = (JsonObject) ol.get("at");
            for (String k : att.names()) {
                JsonValue jsonValue = att.get(k);
                if (jsonValue.isNull()) {
                    attMap.put(k, null);
                } else if (jsonValue.isNumber()) {
                    attMap.put(k, jsonValue.asInt());
                } else {
                    attMap.put(k, jsonValue.asString());
                }
            }
            Link o = new Link(ol.getString("url", null), attMap);
            linkObjs[i] = o;
        }
    }
    Map<String, String> addAttr = new HashMap<>();
    JsonObject o = (JsonObject) v.get("addAttr");
    for (String k : o.names()) {
        addAttr.put(k, o.getString(k, ""));
    }
    return new RegistrationUpdate(regId, identity, lifetime, sms, b, linkObjs, addAttr);
}
Also used : HashMap(java.util.HashMap) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) BindingMode(org.eclipse.leshan.core.request.BindingMode) JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) Identity(org.eclipse.leshan.core.request.Identity) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link)

Example 10 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class RegistrationUpdateSerDesTest method ser_and_des_are_equals.

@Test
public void ser_and_des_are_equals() throws Exception {
    Link[] objs = new Link[2];
    Map<String, Object> att = new HashMap<>();
    att.put("ts", 12);
    att.put("rt", "test");
    att.put("hb", null);
    objs[0] = new Link("/0/1024/2", att);
    objs[1] = new Link("/0/2");
    Map<String, String> additionalAtt = new HashMap<>();
    additionalAtt.put("at", "5000");
    RegistrationUpdate ru = new RegistrationUpdate("myId", Identity.unsecure(Inet4Address.getByName("127.0.0.1"), LwM2m.DEFAULT_COAP_PORT), 60000l, null, BindingMode.U, objs, additionalAtt);
    byte[] ser = RegistrationUpdateSerDes.bSerialize(ru);
    RegistrationUpdate ru2 = RegistrationUpdateSerDes.deserialize(ser);
    assertEquals(ru, ru2);
}
Also used : HashMap(java.util.HashMap) RegistrationUpdate(org.eclipse.leshan.server.registration.RegistrationUpdate) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Aggregations

Link (org.eclipse.leshan.Link)30 Test (org.junit.Test)17 HashMap (java.util.HashMap)16 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)6 BindingMode (org.eclipse.leshan.core.request.BindingMode)6 JsonObject (com.eclipsesource.json.JsonObject)5 InetSocketAddress (java.net.InetSocketAddress)5 ArrayList (java.util.ArrayList)5 JsonArray (com.eclipsesource.json.JsonArray)4 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)4 Identity (org.eclipse.leshan.core.request.Identity)4 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)4 Date (java.util.Date)3 LwM2mInstanceEnabler (org.eclipse.leshan.client.resource.LwM2mInstanceEnabler)3 ObjectEnabler (org.eclipse.leshan.client.resource.ObjectEnabler)3 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)3 JsonValue (com.eclipsesource.json.JsonValue)2 Map (java.util.Map)2 BaseInstanceEnabler (org.eclipse.leshan.client.resource.BaseInstanceEnabler)2 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)2