use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObjectEnabler method doObserve.
@Override
protected ObserveResponse doObserve(final ServerIdentity identity, final ObserveRequest request) {
final LwM2mPath path = request.getPath();
// Manage Object case
if (path.isObject()) {
List<LwM2mObjectInstance> lwM2mObjectInstances = new ArrayList<>();
for (Entry<Integer, LwM2mInstanceEnabler> entry : instances.entrySet()) {
lwM2mObjectInstances.add(getLwM2mObjectInstance(entry.getKey(), entry.getValue(), identity, true));
}
return ObserveResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances));
}
// Manage Instance case
final LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
if (instance == null)
return ObserveResponse.notFound();
if (path.getResourceId() == null) {
return ObserveResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, true));
}
// Manage Resource case
return instance.observe(path.getResourceId());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObjectEnabler method doExecute.
@Override
protected ExecuteResponse doExecute(ExecuteRequest request) {
LwM2mPath path = request.getPath();
LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
if (instance == null) {
return ExecuteResponse.notFound();
}
return instance.execute(path.getResourceId(), request.getParameters());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObjectEnabler method doWrite.
@Override
protected WriteResponse doWrite(ServerIdentity identity, WriteRequest request) {
LwM2mPath path = request.getPath();
// Manage Instance case
LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId());
if (instance == null)
return WriteResponse.notFound();
if (path.isObjectInstance()) {
// instance write
Map<Integer, LwM2mResource> writeResources = ((LwM2mObjectInstance) request.getNode()).getResources();
if (request.isReplaceRequest()) {
// REPLACE
// make them modifiable
writeResources = new HashMap<>(writeResources);
for (ResourceModel resourceModel : getObjectModel().resources.values()) {
if (!identity.isLwm2mServer() || resourceModel.operations.isWritable()) {
LwM2mResource writeResource = writeResources.remove(resourceModel.id);
if (null != writeResource) {
instance.write(resourceModel.id, writeResource);
} else {
instance.reset(resourceModel.id);
}
}
}
}
// UPDATE and resources currently not in the model
for (LwM2mResource resource : writeResources.values()) {
instance.write(resource.getId(), resource);
}
return WriteResponse.success();
}
// Manage Resource case
return instance.write(path.getResourceId(), (LwM2mResource) request.getNode());
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObjectEnabler method doCreate.
@Override
protected CreateResponse doCreate(CreateRequest request) {
Integer instanceId = request.getInstanceId();
if (instanceId == null) {
// the client is in charge to generate the id of the new instance
if (instances.isEmpty()) {
instanceId = 0;
} else {
instanceId = Collections.max(instances.keySet()) + 1;
}
}
LwM2mInstanceEnabler newInstance = instanceFactory.create(getObjectModel());
for (LwM2mResource resource : request.getResources()) {
newInstance.write(resource.getId(), resource);
}
instances.put(instanceId, newInstance);
listenInstance(newInstance, instanceId);
return CreateResponse.success(new LwM2mPath(request.getPath().getObjectId(), instanceId).toString());
}
use of org.eclipse.leshan.core.node.LwM2mPath 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;
}
}
}
Aggregations