Search in sources :

Example 1 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse 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;
            }
        }
    }
}
Also used : ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 2 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.

the class ResponseSerializer method serialize.

@Override
public JsonElement serialize(LwM2mResponse src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject element = new JsonObject();
    element.addProperty("status", src.getCode().toString());
    element.addProperty("valid", src.isValid());
    element.addProperty("success", src.isSuccess());
    element.addProperty("failure", src.isFailure());
    if (typeOfSrc instanceof Class<?>) {
        if (ReadResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) {
            element.add("content", context.serialize(((ReadResponse) src).getContent()));
        } else if (DiscoverResponse.class.isAssignableFrom((Class<?>) typeOfSrc)) {
            element.add("objectLinks", context.serialize(((DiscoverResponse) src).getObjectLinks()));
        }
    }
    return element;
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) JsonObject(com.google.gson.JsonObject) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse)

Example 3 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse 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)));
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Test(org.junit.Test)

Example 4 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse 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)));
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Test(org.junit.Test)

Example 5 with DiscoverResponse

use of org.eclipse.leshan.core.response.DiscoverResponse 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);
}
Also used : DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Aggregations

DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)15 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)11 Test (org.junit.Test)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 Link (org.eclipse.leshan.Link)4 JsonObject (com.eclipsesource.json.JsonObject)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)2 DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)2 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)2 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)2 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResponseCode (org.eclipse.leshan.ResponseCode)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1