Search in sources :

Example 41 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest 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 42 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class DownlinkRequestSerDes method jSerialize.

public static JsonObject jSerialize(DownlinkRequest<?> r) {
    final JsonObject o = Json.object();
    o.add("path", r.getPath().toString());
    r.accept(new DownLinkRequestVisitorAdapter() {

        @Override
        public void visit(ObserveRequest request) {
            o.add("kind", "observe");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }

        @Override
        public void visit(DeleteRequest request) {
            o.add("kind", "delete");
        }

        @Override
        public void visit(DiscoverRequest request) {
            o.add("kind", "discover");
        }

        @Override
        public void visit(CreateRequest request) {
            o.add("kind", "create");
            o.add("contentFormat", request.getContentFormat().getCode());
            if (request.getInstanceId() != null)
                o.add("instanceId", request.getInstanceId());
            JsonArray resources = new JsonArray();
            for (LwM2mResource resource : request.getResources()) {
                resources.add(LwM2mNodeSerDes.jSerialize(resource));
            }
            o.add("resources", resources);
        }

        @Override
        public void visit(ExecuteRequest request) {
            o.add("kind", "execute");
            o.add("parameters", request.getParameters());
        }

        @Override
        public void visit(WriteAttributesRequest request) {
            o.add("kind", "writeAttributes");
            o.add("observeSpec", request.getObserveSpec().toString());
        }

        @Override
        public void visit(WriteRequest request) {
            o.add("kind", "write");
            o.add("contentFormat", request.getContentFormat().getCode());
            o.add("mode", request.isPartialUpdateRequest() ? "UPDATE" : "REPLACE");
            o.add("node", LwM2mNodeSerDes.jSerialize(request.getNode()));
        }

        @Override
        public void visit(ReadRequest request) {
            o.add("kind", "read");
            if (request.getContentFormat() != null)
                o.add("contentFormat", request.getContentFormat().getCode());
        }
    });
    return o;
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) JsonObject(com.eclipsesource.json.JsonObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) DownLinkRequestVisitorAdapter(org.eclipse.leshan.core.request.DownLinkRequestVisitorAdapter) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 43 with ReadRequest

use of org.eclipse.leshan.core.request.ReadRequest in project leshan by eclipse.

the class DownlinkRequestSerDesTest method ser_and_des_read_request_then_compare_to_observe_request.

@Test
public void ser_and_des_read_request_then_compare_to_observe_request() throws Exception {
    ReadRequest readRequest = new ReadRequest(ContentFormat.TLV, 3, 0, 1);
    JsonObject ser = DownlinkRequestSerDes.jSerialize(readRequest);
    DownlinkRequest<?> r2 = DownlinkRequestSerDes.deserialize(ser);
    assertNotEquals(r2, new ObserveRequest(ContentFormat.TLV, 3, 0, 1));
}
Also used : JsonObject(com.eclipsesource.json.JsonObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

ReadRequest (org.eclipse.leshan.core.request.ReadRequest)43 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)34 Test (org.junit.Test)30 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)19 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)14 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)7 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)7 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)5 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)4 TimeoutException (org.eclipse.leshan.core.request.exception.TimeoutException)4 JsonObject (com.eclipsesource.json.JsonObject)3 Request (org.eclipse.californium.core.coap.Request)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3