public static Class<? extends LwM2mNode> nodeClassFromPath(LwM2mPath path) { if (path.isObject()) { return LwM2mObject.class; } else if (path.isObjectInstance()) { return LwM2mObjectInstance.class; } else if (path.isResource()) { return LwM2mResource.class; } throw new IllegalArgumentException("invalid path level: " + path); }
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding Object {} into JSON", object); // Validate request path if (!requestPath.isObject()) { throw new CodecException("Invalid request path %s for JSON object encoding", requestPath); } // Create resources resourceList = new ArrayList<>(); for (LwM2mObjectInstance instance : object.getInstances().values()) { for (LwM2mResource resource : instance.getResources().values()) { String prefixPath = Integer.toString(instance.getId()) + "/" + Integer.toString(resource.getId()); resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } } }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into JSON", instance); resourceList = new ArrayList<>(); for (LwM2mResource resource : instance.getResources().values()) { // Validate request path & compute resource path String prefixPath; if (requestPath.isObject()) { prefixPath = instance.getId() + "/" + resource.getId(); } else if (requestPath.isObjectInstance()) { prefixPath = Integer.toString(resource.getId()); } else { throw new CodecException("Invalid request path %s for JSON instance encoding", requestPath); } // Create resources resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } }
@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()); }
private CreateRequest(ContentFormat format, LwM2mPath target, Integer instanceId, LwM2mResource[] resources) { super(target); // accept only object and object instance path if (target.isRoot()) throw new InvalidRequestException("Create request cannot target root path"); if (!target.isObject() && !target.isObjectInstance()) throw new InvalidRequestException( "Invalid path %s: Create request must not target an object or object instance", target); // validate instance id if (instanceId != null && instanceId == LwM2mObjectInstance.UNDEFINED) { instanceId = null; } if (target.isObjectInstance()) { if (instanceId == null) { instanceId = target.getObjectInstanceId(); } else { if (!instanceId.equals(target.getObjectInstanceId())) { throw new InvalidRequestException("Conflict between path instance id %s and node instance id %s", target, instanceId); } } } if (instanceId != null && instanceId < 0) throw new InvalidRequestException("Invalid instance id %s for path %s ", instanceId, target); // store attributes this.instanceId = instanceId; this.resources = Collections.unmodifiableList(Arrays.asList(resources)); this.contentFormat = format != null ? format : ContentFormat.TLV; // default to TLV }
@Override protected ReadResponse doRead(ServerIdentity identity, ReadRequest request) { 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, false)); } return ReadResponse.success(new LwM2mObject(getId(), lwM2mObjectInstances)); } // Manage Instance case LwM2mInstanceEnabler instance = instances.get(path.getObjectInstanceId()); if (instance == null) return ReadResponse.notFound(); if (path.getResourceId() == null) { return ReadResponse.success(getLwM2mObjectInstance(path.getObjectInstanceId(), instance, identity, false)); } // Manage Resource case return instance.read(path.getResourceId()); }
@Override public BootstrapDeleteResponse doDelete(BootstrapDeleteRequest request) { if (request.getPath().isRoot() || request.getPath().isObject()) { if (id == LwM2mId.SECURITY) {
node.getClass().getSimpleName()); } else if (getPath().isObject()) { if (!(node instanceof LwM2mObject)) { throw new InvalidRequestException("path '%s' and node type '%s' do not match", target,
protected DiscoverResponse doDiscover(DiscoverRequest request) { LwM2mPath path = request.getPath(); if (path.isObject()) { // Manage discover on object Link[] ObjectLinks = LinkFormatHelper.getObjectDescription(this, null); return DiscoverResponse.success(ObjectLinks); } else if (path.isObjectInstance()) { // Manage discover on instance if (!getAvailableInstanceIds().contains(path.getObjectInstanceId())) return DiscoverResponse.notFound(); Link[] instanceLink = LinkFormatHelper.getInstanceDescription(this, path.getObjectInstanceId(), null); return DiscoverResponse.success(instanceLink); } else if (path.isResource()) { // Manage discover on resource if (!getAvailableInstanceIds().contains(path.getObjectInstanceId())) return DiscoverResponse.notFound(); ResourceModel resourceModel = getObjectModel().resources.get(path.getResourceId()); if (resourceModel == null) return DiscoverResponse.notFound(); if (!getAvailableResourceIds(path.getObjectInstanceId()).contains(path.getResourceId())) return DiscoverResponse.notFound(); Link resourceLink = LinkFormatHelper.getResourceDescription(this, path.getObjectInstanceId(), path.getResourceId(), null); return DiscoverResponse.success(new Link[] { resourceLink }); } return DiscoverResponse.badRequest(null); }
node.getClass().getSimpleName()); } else if (getPath().isObject()) { throw new InvalidRequestException("write request %s cannot target an object", target);
if (path.isObject()) { for (LwM2mObjectInstance instanceNode : ((LwM2mObject) request.getNode()).getInstances().values()) { LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId());