Search in sources :

Example 26 with WriteRequest

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

the class ClientServlet method doPut.

/**
 * {@inheritDoc}
 */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // at least /endpoint/objectId/instanceId
    if (path.length < 3) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    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
            LwM2mNode node = extractLwM2mNode(target, req);
            WriteRequest request = new WriteRequest(Mode.REPLACE, contentFormat, target, node);
            WriteResponse 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 : Registration(org.eclipse.leshan.server.registration.Registration) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode)

Example 27 with WriteRequest

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

WriteRequest (org.eclipse.leshan.core.request.WriteRequest)27 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)19 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)18 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)14 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 Test (org.junit.Test)13 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)7 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)7 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)5 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)5 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)4 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)3 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 Observation (org.eclipse.leshan.core.observation.Observation)3 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)3 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)3