use of org.eclipse.leshan.core.request.WriteAttributesRequest in project leshan by eclipse.
the class ObjectResource method handlePUT.
@Override
public void handlePUT(CoapExchange coapExchange) {
ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
String URI = coapExchange.getRequestOptions().getUriPathString();
// get Observe Spec
ObserveSpec spec = null;
if (coapExchange.advanced().getRequest().getOptions().getURIQueryCount() != 0) {
List<String> uriQueries = coapExchange.advanced().getRequest().getOptions().getUriQuery();
spec = ObserveSpec.parse(uriQueries);
}
// Manage Write Attributes Request
if (spec != null) {
WriteAttributesResponse response = nodeEnabler.writeAttributes(identity, new WriteAttributesRequest(URI, spec));
if (response.getCode().isError()) {
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
coapExchange.respond(toCoapResponseCode(response.getCode()));
}
return;
} else // Manage Write and Bootstrap Write Request (replace)
{
LwM2mPath path = new LwM2mPath(URI);
if (!coapExchange.getRequestOptions().hasContentFormat()) {
coapExchange.respond(ResponseCode.BAD_REQUEST, "Content Format is mandatory");
return;
}
ContentFormat contentFormat = ContentFormat.fromCode(coapExchange.getRequestOptions().getContentFormat());
if (!decoder.isSupported(contentFormat)) {
coapExchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
return;
}
LwM2mNode lwM2mNode;
try {
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
if (identity.isLwm2mBootstrapServer()) {
BootstrapWriteResponse response = nodeEnabler.write(identity, new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
if (response.getCode().isError()) {
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
coapExchange.respond(toCoapResponseCode(response.getCode()));
}
} else {
WriteResponse response = nodeEnabler.write(identity, new WriteRequest(Mode.REPLACE, contentFormat, URI, lwM2mNode));
if (response.getCode().isError()) {
coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
coapExchange.respond(toCoapResponseCode(response.getCode()));
}
}
return;
} catch (CodecException e) {
LOG.warn("Unable to decode payload to write", e);
coapExchange.respond(ResponseCode.BAD_REQUEST);
return;
}
}
}
use of org.eclipse.leshan.core.request.WriteAttributesRequest in project leshan by eclipse.
the class DownlinkRequestSerDesTest method ser_and_des_write_attributes_request.
@Test
public void ser_and_des_write_attributes_request() throws Exception {
ObserveSpec os = new ObserveSpec.Builder().minPeriod(10).maxPeriod(60).build();
ser_and_des_are_equals(new WriteAttributesRequest(3, 0, 1, os));
}
use of org.eclipse.leshan.core.request.WriteAttributesRequest 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.WriteAttributesRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_write_attribute_request.
@Test
public void build_write_attribute_request() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
WriteAttributesRequest request = new WriteAttributesRequest(3, 0, 14, new ObserveSpec.Builder().minPeriod(10).maxPeriod(100).build());
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.PUT, 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/3/0/14?pmin=10&pmax=100", coapRequest.getURI());
}
use of org.eclipse.leshan.core.request.WriteAttributesRequest 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;
}
Aggregations