Search in sources :

Example 11 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_resource.

@Test
public void cant_discover_resource_of_non_existent_resource() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0, 42));
    // 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 12 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.

@Test
public void cant_discover_resource_of_non_existent_instance() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 1, 0));
    // 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 13 with DiscoverResponse

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

the class DiscoverTest method cant_discover_non_existent_instance.

@Test
public void cant_discover_non_existent_instance() throws InterruptedException {
    // read ACL object
    DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 1));
    // 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 14 with DiscoverResponse

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

the class ClientServlet method doGet.

/**
 * {@inheritDoc}
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // all registered clients
    if (req.getPathInfo() == null) {
        Collection<Registration> registrations = new ArrayList<>();
        for (Iterator<Registration> iterator = server.getRegistrationService().getAllRegistrations(); iterator.hasNext(); ) {
            registrations.add(iterator.next());
        }
        String json = this.gson.toJson(registrations.toArray(new Registration[] {}));
        resp.setContentType("application/json");
        resp.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
        resp.setStatus(HttpServletResponse.SC_OK);
        return;
    }
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    if (path.length < 1) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    String clientEndpoint = path[0];
    // /endPoint : get client
    if (path.length == 1) {
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            resp.setContentType("application/json");
            resp.getOutputStream().write(this.gson.toJson(registration).getBytes(StandardCharsets.UTF_8));
            resp.setStatus(HttpServletResponse.SC_OK);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
        }
        return;
    }
    // /clients/endPoint/LWRequest/discover : do LightWeight M2M discover request on a given client.
    if (path.length >= 3 && "discover".equals(path[path.length - 1])) {
        String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/discover");
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                // create & process request
                DiscoverRequest request = new DiscoverRequest(target);
                DiscoverResponse cResponse = server.send(registration, request, TIMEOUT);
                processDeviceResponse(req, resp, cResponse);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("No registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException | InterruptedException e) {
            handleException(e, resp);
        }
        return;
    }
    // /clients/endPoint/LWRequest : do LightWeight M2M read request on a given client.
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            // get content format
            String contentFormatParam = req.getParameter(FORMAT_PARAM);
            ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
            // create & process request
            ReadRequest request = new ReadRequest(contentFormat, target);
            ReadResponse cResponse = server.send(registration, request, TIMEOUT);
            processDeviceResponse(req, resp, cResponse);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("No registered client with id '%s'", clientEndpoint).flush();
        }
    } catch (RuntimeException | InterruptedException e) {
        handleException(e, resp);
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) ArrayList(java.util.ArrayList) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) Registration(org.eclipse.leshan.server.registration.Registration) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 15 with DiscoverResponse

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

the class ResponseSerDes method deserialize.

public static LwM2mResponse deserialize(JsonObject o) {
    String sCode = o.getString("code", null);
    if (sCode == null)
        throw new IllegalStateException("Invalid response missing code attribute");
    ResponseCode code = ResponseCode.fromName(sCode);
    String errorMessage = o.getString("errorMessage", null);
    String kind = o.getString("kind", null);
    switch(kind) {
        case "observe":
            {
                // TODO ser Observation
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ObserveResponse(code, content, null, null, errorMessage);
            }
        case "delete":
            return new DeleteResponse(code, errorMessage);
        case "discover":
            String objectLinks = o.getString("objectLinks", "");
            return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage);
        case "create":
            {
                String location = o.getString("location", null);
                return new CreateResponse(code, location, errorMessage);
            }
        case "execute":
            return new ExecuteResponse(code, errorMessage);
        case "writeAttributes":
            {
                return new WriteAttributesResponse(code, errorMessage);
            }
        case "write":
            {
                return new WriteResponse(code, errorMessage);
            }
        case "read":
            {
                LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
                return new ReadResponse(code, content, errorMessage);
            }
        default:
            throw new IllegalStateException("Invalid response missing kind attribute");
    }
}
Also used : ResponseCode(org.eclipse.leshan.ResponseCode) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse)

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