Search in sources :

Example 1 with DeleteResponse

use of org.eclipse.leshan.core.response.DeleteResponse 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 DeleteResponse

use of org.eclipse.leshan.core.response.DeleteResponse 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 DeleteResponse

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

Example 4 with DeleteResponse

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

the class DeleteTest method cannot_delete_single_manadatory_object_instance.

@Test
public void cannot_delete_single_manadatory_object_instance() throws InterruptedException {
    // try to create an instance of object 50
    DeleteResponse response = helper.server.send(helper.getCurrentRegistration(), new DeleteRequest(3, 0));
    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, 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)

Example 5 with DeleteResponse

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

the class ResponseSerDes method jSerialize.

public static JsonObject jSerialize(LwM2mResponse r) {
    final JsonObject o = Json.object();
    o.add("code", r.getCode().toString());
    if (r.isFailure()) {
        o.add("errorMessage", r.getErrorMessage());
        return o;
    }
    if (r instanceof ReadResponse) {
        o.add("kind", "read");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof ObserveResponse) {
        o.add("kind", "observe");
        o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
    } else if (r instanceof DiscoverResponse) {
        o.add("kind", "discover");
        o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks()));
    } else if (r instanceof DeleteResponse) {
        o.add("kind", "delete");
    } else if (r instanceof ExecuteResponse) {
        o.add("kind", "execute");
    } else if (r instanceof WriteResponse) {
        o.add("kind", "write");
    } else if (r instanceof WriteAttributesResponse) {
        o.add("kind", "writeAttributes");
    } else if (r instanceof CreateResponse) {
        o.add("kind", "create");
        o.add("location", ((CreateResponse) r).getLocation());
    }
    return o;
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) 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) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Aggregations

DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)9 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)7 Test (org.junit.Test)5 JsonObject (com.eclipsesource.json.JsonObject)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)2 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)2 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)2 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)2 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)2 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)2 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)2 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)2 ResponseCode (org.eclipse.leshan.ResponseCode)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)1 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)1 Registration (org.eclipse.leshan.server.registration.Registration)1