use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegisterResource method handleRegister.
private void handleRegister(CoapExchange exchange, Request request) {
// Get identity
// --------------------------------
Identity sender = extractIdentity(request.getSourceContext());
// Create LwM2m request from CoAP request
// --------------------------------
// We don't check content media type is APPLICATION LINK FORMAT for now as this is the only format we can expect
String endpoint = null;
Long lifetime = null;
String smsNumber = null;
String lwVersion = null;
BindingMode binding = null;
// Get object Links
Link[] objectLinks = Link.parse(request.getPayload());
Map<String, String> additionalParams = new HashMap<>();
// Get parameters
for (String param : request.getOptions().getUriQuery()) {
if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
endpoint = param.substring(3);
} else if (param.startsWith(QUERY_PARAM_LIFETIME)) {
lifetime = Long.valueOf(param.substring(3));
} else if (param.startsWith(QUERY_PARAM_SMS)) {
smsNumber = param.substring(4);
} else if (param.startsWith(QUERY_PARAM_LWM2M_VERSION)) {
lwVersion = param.substring(6);
} else if (param.startsWith(QUERY_PARAM_BINDING_MODE)) {
binding = BindingMode.valueOf(param.substring(2));
} else {
String[] tokens = param.split("\\=");
if (tokens != null && tokens.length == 2) {
additionalParams.put(tokens[0], tokens[1]);
}
}
}
// Create request
RegisterRequest registerRequest = new RegisterRequest(endpoint, lifetime, lwVersion, binding, smsNumber, objectLinks, additionalParams);
// Handle request
// -------------------------------
InetSocketAddress serverEndpoint = exchange.advanced().getEndpoint().getAddress();
final SendableResponse<RegisterResponse> sendableResponse = registrationHandler.register(sender, registerRequest, serverEndpoint);
RegisterResponse response = sendableResponse.getResponse();
// -------------------------------
if (response.getCode() == org.eclipse.leshan.ResponseCode.CREATED) {
exchange.setLocationPath(RESOURCE_NAME + "/" + response.getRegistrationID());
exchange.respond(ResponseCode.CREATED);
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
sendableResponse.sent();
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationSerDes method jSerialize.
public static JsonObject jSerialize(Registration r) {
JsonObject o = Json.object();
o.add("regDate", r.getRegistrationDate().getTime());
o.add("identity", IdentitySerDes.serialize(r.getIdentity()));
o.add("regAddr", r.getRegistrationEndpointAddress().getHostString());
o.add("regPort", r.getRegistrationEndpointAddress().getPort());
o.add("lt", r.getLifeTimeInSec());
if (r.getSmsNumber() != null) {
o.add("sms", r.getSmsNumber());
}
o.add("ver", r.getLwM2mVersion());
o.add("bnd", r.getBindingMode().name());
o.add("ep", r.getEndpoint());
o.add("regId", r.getId());
JsonArray links = new JsonArray();
for (Link l : r.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);
JsonObject addAttr = Json.object();
for (Map.Entry<String, String> e : r.getAdditionalRegistrationAttributes().entrySet()) {
addAttr.add(e.getKey(), e.getValue());
}
o.add("addAttr", addAttr);
o.add("root", r.getRootPath());
o.add("lastUp", r.getLastUpdate().getTime());
return o;
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationSerDes method deserialize.
public static Registration deserialize(JsonObject jObj) {
Registration.Builder b = new Registration.Builder(jObj.getString("regId", null), jObj.getString("ep", null), IdentitySerDes.deserialize(jObj.get("identity").asObject()), new InetSocketAddress(jObj.getString("regAddr", null), jObj.getInt("regPort", 0)));
b.bindingMode(BindingMode.valueOf(jObj.getString("bnd", null)));
b.lastUpdate(new Date(jObj.getLong("lastUp", 0)));
b.lifeTimeInSec(jObj.getLong("lt", 0));
b.lwM2mVersion(jObj.getString("ver", "1.0"));
b.registrationDate(new Date(jObj.getLong("regDate", 0)));
if (jObj.get("sms") != null) {
b.smsNumber(jObj.getString("sms", ""));
}
JsonArray links = (JsonArray) jObj.get("objLink");
Link[] 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;
}
b.objectLinks(linkObjs);
Map<String, String> addAttr = new HashMap<>();
JsonObject o = (JsonObject) jObj.get("addAttr");
for (String k : o.names()) {
addAttr.put(k, o.getString(k, ""));
}
b.additionalRegistrationAttributes(addAttr);
return b.build();
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationUpdate method update.
/**
* Returns an updated version of the registration.
*
* @param registration the registration to update
* @return the updated registration
*/
public Registration update(Registration registration) {
Identity identity = this.identity != null ? this.identity : registration.getIdentity();
Link[] linkObject = this.objectLinks != null ? this.objectLinks : registration.getObjectLinks();
long lifeTimeInSec = this.lifeTimeInSec != null ? this.lifeTimeInSec : registration.getLifeTimeInSec();
BindingMode bindingMode = this.bindingMode != null ? this.bindingMode : registration.getBindingMode();
String smsNumber = this.smsNumber != null ? this.smsNumber : registration.getSmsNumber();
Map<String, String> additionalAttributes = this.additionalAttributes.isEmpty() ? registration.getAdditionalRegistrationAttributes() : updateAdditionalAttributes(registration.getAdditionalRegistrationAttributes());
// this needs to be done in any case, even if no properties have changed, in order
// to extend the client registration time-to-live period ...
Date lastUpdate = new Date();
Registration.Builder builder = new Registration.Builder(registration.getId(), registration.getEndpoint(), identity, registration.getRegistrationEndpointAddress());
builder.lwM2mVersion(registration.getLwM2mVersion()).lifeTimeInSec(lifeTimeInSec).smsNumber(smsNumber).bindingMode(bindingMode).objectLinks(linkObject).registrationDate(registration.getRegistrationDate()).lastUpdate(lastUpdate).additionalRegistrationAttributes(additionalAttributes);
return builder.build();
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class RegistrationSerDesTest 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");
Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", Identity.unsecure(Inet4Address.getLoopbackAddress(), 1), new InetSocketAddress(212)).objectLinks(objs);
builder.registrationDate(new Date(100L));
builder.lastUpdate(new Date(101L));
Registration r = builder.build();
byte[] ser = RegistrationSerDes.bSerialize(r);
Registration r2 = RegistrationSerDes.deserialize(ser);
assertEquals(r, r2);
}
Aggregations