use of org.eclipse.leshan.Link in project leshan by eclipse.
the class CoapRequestBuilder method visit.
@Override
public void visit(RegisterRequest request) {
coapRequest = Request.newPost();
buildRequestSettings();
coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
coapRequest.getOptions().addUriPath("rd");
HashMap<String, String> attributes = new HashMap<>();
attributes.putAll(request.getAdditionalAttributes());
attributes.put("ep", request.getEndpointName());
Long lifetime = request.getLifetime();
if (lifetime != null)
attributes.put("lt", lifetime.toString());
String smsNumber = request.getSmsNumber();
if (smsNumber != null)
attributes.put("sms", smsNumber);
String lwVersion = request.getLwVersion();
if (lwVersion != null)
attributes.put("lwm2m", lwVersion);
BindingMode bindingMode = request.getBindingMode();
if (bindingMode != null)
attributes.put("b", bindingMode.toString());
for (Entry<String, String> attr : attributes.entrySet()) {
coapRequest.getOptions().addUriQuery(attr.getKey() + "=" + attr.getValue());
}
Link[] objectLinks = request.getObjectLinks();
if (objectLinks != null)
coapRequest.setPayload(Link.serialize(objectLinks));
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class CoapRequestBuilder method visit.
@Override
public void visit(UpdateRequest request) {
coapRequest = Request.newPost();
buildRequestSettings();
coapRequest.getOptions().setUriPath(request.getRegistrationId());
Long lifetime = request.getLifeTimeInSec();
if (lifetime != null)
coapRequest.getOptions().addUriQuery("lt=" + lifetime);
String smsNumber = request.getSmsNumber();
if (smsNumber != null)
coapRequest.getOptions().addUriQuery("sms=" + smsNumber);
BindingMode bindingMode = request.getBindingMode();
if (bindingMode != null)
coapRequest.getOptions().addUriQuery("b=" + bindingMode.toString());
Link[] linkObjects = request.getObjectLinks();
if (linkObjects != null) {
coapRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode());
coapRequest.setPayload(Link.serialize(linkObjects));
}
}
use of org.eclipse.leshan.Link 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.Link in project leshan by eclipse.
the class CoapRequestBuilderTest method newRegistration.
private Registration newRegistration(String rootpath) throws UnknownHostException {
Builder b = new Registration.Builder("regid", "endpoint", Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354), new InetSocketAddress(0));
if (rootpath != null) {
Map<String, String> attr = new HashMap<>();
attr.put("rt", "oma.lwm2m");
b.objectLinks(new Link[] { new Link(rootpath, attr) });
}
return b.build();
}
use of org.eclipse.leshan.Link in project leshan by eclipse.
the class DiscoverTest method can_discover_object_instance.
@Test
public void can_discover_object_instance() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0));
// verify result
assertEquals(CONTENT, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
Link[] payload = response.getObjectLinks();
assertArrayEquals(Link.parse("</3/0>".getBytes()), payload);
}
Aggregations