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()));
}
}
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);
}
}
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)));
}
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)));
}
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;
}
Aggregations