use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.
the class ObjectResource method handleGET.
@Override
public void handleGET(CoapExchange exchange) {
ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler);
String URI = exchange.getRequestOptions().getUriPathString();
// Manage Discover Request
if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) {
DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI));
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()), MediaTypeRegistry.APPLICATION_LINK_FORMAT);
}
} else {
// handle content format for Read and Observe Request
// use TLV as default format
ContentFormat format = ContentFormat.TLV;
if (exchange.getRequestOptions().hasAccept()) {
format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
if (!encoder.isSupported(format)) {
exchange.respond(ResponseCode.NOT_ACCEPTABLE);
return;
}
}
// Manage Observe Request
if (exchange.getRequestOptions().hasObserve()) {
ObserveResponse response = nodeEnabler.observe(identity, new ObserveRequest(URI));
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
return;
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
} else // Manage Read Request
{
ReadResponse response = nodeEnabler.read(identity, new ReadRequest(URI));
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
return;
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
}
}
}
use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_discover_request.
@Test
public void build_discover_request() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
DiscoverRequest request = new DiscoverRequest(3, 0);
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.GET, coapRequest.getCode());
assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
assertEquals(MediaTypeRegistry.APPLICATION_LINK_FORMAT, coapRequest.getOptions().getAccept());
assertEquals("coap://127.0.0.1:12354/3/0", coapRequest.getURI());
}
use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.
the class DiscoverTest method cant_discover_non_existent_object.
@Test
public void cant_discover_non_existent_object() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(4));
// verify result
assertEquals(NOT_FOUND, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.request.DiscoverRequest in project leshan by eclipse.
the class DiscoverTest method cant_discover_resource_of_non_existent_instance_and_resource.
@Test
public void cant_discover_resource_of_non_existent_instance_and_resource() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 1, 20));
// verify result
assertEquals(NOT_FOUND, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.request.DiscoverRequest 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