Search in sources :

Example 1 with DeleteRequest

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

the class ObjectResource method handleDELETE.

@Override
public void handleDELETE(CoapExchange coapExchange) {
    // Manage Delete Request
    String URI = coapExchange.getRequestOptions().getUriPathString();
    ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
    DeleteResponse response = nodeEnabler.delete(identity, new DeleteRequest(URI));
    if (response.getCode().isError()) {
        coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
    } else {
        coapExchange.respond(toCoapResponseCode(response.getCode()));
    }
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest)

Example 2 with DeleteRequest

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

the class ClientServlet method doDelete.

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // /clients/endPoint/LWRequest/observe : cancel observation for the given resource.
    if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
        try {
            String target = StringUtils.substringsBetween(req.getPathInfo(), clientEndpoint, "/observe")[0];
            Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
            if (registration != null) {
                server.getObservationService().cancelObservations(registration, target);
                resp.setStatus(HttpServletResponse.SC_OK);
            } else {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
            }
        } catch (RuntimeException e) {
            handleException(e, resp);
        }
        return;
    }
    // /clients/endPoint/LWRequest/ : delete instance
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            DeleteRequest request = new DeleteRequest(target);
            DeleteResponse 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 : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) Registration(org.eclipse.leshan.server.registration.Registration) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest)

Example 3 with DeleteRequest

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

the class CoapRequestBuilderTest method build_delete_request.

@Test
public void build_delete_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    DeleteRequest request = new DeleteRequest(12, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.DELETE, coapRequest.getCode());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) Test(org.junit.Test)

Example 4 with DeleteRequest

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

the class DownlinkRequestSerDes method deserialize.

public static DownlinkRequest<?> deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    String path = o.getString("path", null);
    switch(kind) {
        case "observe":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ObserveRequest(ContentFormat.fromCode(format), path);
            }
        case "delete":
            return new DeleteRequest(path);
        case "discover":
            return new DiscoverRequest(path);
        case "create":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonArray jResources = (JsonArray) o.get("resources");
                for (JsonValue jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
                    resources.add(resource);
                }
                return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
            }
        case "execute":
            String parameters = o.getString("parameters", null);
            return new ExecuteRequest(path, parameters);
        case "writeAttributes":
            {
                String observeSpec = o.getString("observeSpec", null);
                return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
            }
        case "write":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
                LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
                return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
            }
        case "read":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ReadRequest(ContentFormat.fromCode(format), path);
            }
        default:
            throw new IllegalStateException("Invalid request missing kind attribute");
    }
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Collection(java.util.Collection) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 5 with DeleteRequest

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

the class DeleteTest method cannot_delete_security_object_instance.

@Test
public void cannot_delete_security_object_instance() throws InterruptedException {
    DeleteResponse response = helper.server.send(helper.getCurrentRegistration(), new DeleteRequest(0, 0));
    // verify result
    assertEquals(ResponseCode.NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) Test(org.junit.Test)

Aggregations

DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)11 DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)7 Test (org.junit.Test)6 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)5 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)3 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)3 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)3 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)3 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 JsonArray (com.eclipsesource.json.JsonArray)2 JsonObject (com.eclipsesource.json.JsonObject)2 Registration (org.eclipse.leshan.server.registration.Registration)2 JsonValue (com.eclipsesource.json.JsonValue)1 Collection (java.util.Collection)1 Request (org.eclipse.californium.core.coap.Request)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)1