use of org.eclipse.leshan.core.request.Identity 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);
}
use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.
the class RegisterResource method handleUpdate.
private void handleUpdate(CoapExchange exchange, Request request, String registrationId) {
// Get identity
Identity sender = extractIdentity(request.getSourceContext());
// Create LwM2m request from CoAP request
Long lifetime = null;
String smsNumber = null;
BindingMode binding = null;
Link[] objectLinks = null;
Map<String, String> additionalParams = new HashMap<>();
for (String param : request.getOptions().getUriQuery()) {
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_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]);
}
}
}
if (request.getPayload() != null && request.getPayload().length > 0) {
objectLinks = Link.parse(request.getPayload());
}
UpdateRequest updateRequest = new UpdateRequest(registrationId, lifetime, smsNumber, binding, objectLinks, additionalParams);
// Handle request
final SendableResponse<UpdateResponse> sendableResponse = registrationHandler.update(sender, updateRequest);
UpdateResponse updateResponse = sendableResponse.getResponse();
// Create CoAP Response from LwM2m request
if (updateResponse.getCode().isError()) {
exchange.respond(toCoapResponseCode(updateResponse.getCode()), updateResponse.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(updateResponse.getCode()));
}
sendableResponse.sent();
}
use of org.eclipse.leshan.core.request.Identity in project leshan by eclipse.
the class BootstrapResource method handlePOST.
@Override
public void handlePOST(CoapExchange exchange) {
Request request = exchange.advanced().getRequest();
LOG.debug("POST received : {}", request);
// messages
if (!Type.CON.equals(request.getType())) {
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
// which endpoint?
String endpoint = null;
for (String param : request.getOptions().getUriQuery()) {
if (param.startsWith(QUERY_PARAM_ENDPOINT)) {
endpoint = param.substring(QUERY_PARAM_ENDPOINT.length());
break;
}
}
// Extract client identity
Identity clientIdentity = EndpointContextUtil.extractIdentity(request.getSourceContext());
// handle bootstrap request
BootstrapResponse response = bootstrapHandler.bootstrap(clientIdentity, new BootstrapRequest(endpoint));
if (response.isSuccess()) {
exchange.respond(toCoapResponseCode(response.getCode()));
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
}
}
use of org.eclipse.leshan.core.request.Identity 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.core.request.Identity in project leshan by eclipse.
the class RegisterResource method handleDeregister.
private void handleDeregister(CoapExchange exchange, String registrationId) {
// Get identity
Identity sender = extractIdentity(exchange.advanced().getRequest().getSourceContext());
// Create request
DeregisterRequest deregisterRequest = new DeregisterRequest(registrationId);
// Handle request
final SendableResponse<DeregisterResponse> sendableResponse = registrationHandler.deregister(sender, deregisterRequest);
DeregisterResponse deregisterResponse = sendableResponse.getResponse();
// Create CoAP Response from LwM2m request
if (deregisterResponse.getCode().isError()) {
exchange.respond(toCoapResponseCode(deregisterResponse.getCode()), deregisterResponse.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(deregisterResponse.getCode()));
}
sendableResponse.sent();
}
Aggregations