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