Search in sources :

Example 16 with CreateRequest

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

the class ClientServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // /clients/endPoint/LWRequest/observe : do LightWeight M2M observe request on a given client.
    if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
        try {
            String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/observe");
            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
                ObserveRequest request = new ObserveRequest(contentFormat, target);
                ObserveResponse 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;
    }
    String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
    // /clients/endPoint/LWRequest : do LightWeight M2M execute request on a given client.
    if (path.length == 4) {
        try {
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                ExecuteRequest request = new ExecuteRequest(target, IOUtils.toString(req.getInputStream()));
                ExecuteResponse 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 create request on a given client.
    if (2 <= path.length && path.length <= 3) {
        try {
            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
                LwM2mNode node = extractLwM2mNode(target, req);
                if (node instanceof LwM2mObjectInstance) {
                    CreateRequest request = new CreateRequest(contentFormat, target, (LwM2mObjectInstance) node);
                    CreateResponse cResponse = server.send(registration, request, TIMEOUT);
                    processDeviceResponse(req, resp, cResponse);
                } else {
                    throw new IllegalArgumentException("payload must contain an object instance");
                }
            } 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;
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Registration(org.eclipse.leshan.server.registration.Registration)

Example 17 with CreateRequest

use of org.eclipse.leshan.core.request.CreateRequest 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)

Aggregations

CreateRequest (org.eclipse.leshan.core.request.CreateRequest)17 Test (org.junit.Test)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)10 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)10 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)6 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)4 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 Registration (org.eclipse.leshan.server.registration.Registration)3 JsonArray (com.eclipsesource.json.JsonArray)2 JsonObject (com.eclipsesource.json.JsonObject)2 Request (org.eclipse.californium.core.coap.Request)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2