@Override public final String toString() { return String.format("ObserveRequest [path=%s format=%s]", getPath(), format); }
@Override public void visit(ObserveRequest request) { o.add("kind", "observe"); if (request.getContentFormat() != null) o.add("contentFormat", request.getContentFormat().getCode()); }
/** * Create a CoAP observe request context with specific keys needed for internal Leshan working. */ public static Map<String, String> createCoapObserveRequestContext(String endpoint, String registrationId, ObserveRequest request) { Map<String, String> context = new HashMap<>(); context.put(CTX_ENDPOINT, endpoint); context.put(CTX_REGID, registrationId); context.put(CTX_LWM2M_PATH, request.getPath().toString()); for (Entry<String, String> ctx : request.getContext().entrySet()) { context.put(ctx.getKey(), ctx.getValue()); } return context; }
@Override public void visit(ObserveRequest request) { coapRequest = Request.newGet(); if (request.getContentFormat() != null) coapRequest.getOptions().setAccept(request.getContentFormat().getCode()); coapRequest.setObserve(); setTarget(coapRequest, request.getPath()); // add context info to the observe request coapRequest.setUserContext(ObserveUtil.createCoapObserveRequestContext(endpoint, registrationId, request)); }
ObserveRequest request = new ObserveRequest(contentFormat, target); ObserveResponse cResponse = server.send(registration, request, TIMEOUT); processDeviceResponse(req, resp, cResponse);
/** * Creates a request for observing future changes of a particular LWM2M node (object, object instance or resource). * * @param path the path to the LWM2M node to observe * @exception InvalidRequestException if the path is not valid. */ public ObserveRequest(String path) throws InvalidRequestException { this(null, newPath(path), null); }
case "observe": { int format = o.getInt("contentFormat", ContentFormat.TLV.getCode()); return new ObserveRequest(ContentFormat.fromCode(format), path);
/** * Creates a request for observing future changes of a particular LWM2M node (object, object instance or resource). * * @param format the desired format for the response * @param path the path to the LWM2M node to observe * @exception InvalidRequestException if the path is not valid. */ public ObserveRequest(ContentFormat format, String path) throws InvalidRequestException { this(format, newPath(path), null); }
@Override public void visit(ObserveRequest request) { coapRequest = Request.newGet(); coapRequest.setObserve(); setTarget(coapRequest, destination, request.getPath()); }
case "observe": { int format = o.getInt("contentFormat", ContentFormat.TLV.getCode()); return new ObserveRequest(ContentFormat.fromCode(format), path);
@Override public void visit(ObserveRequest request) { o.add("kind", "observe"); if (request.getContentFormat() != null) o.add("contentFormat", request.getContentFormat().getCode()); }
/** * Creates a request for observing future changes of a particular LWM2M node (object, object instance or resource). * * @param format the desired format for the response * @param path the path to the LWM2M node to observe * @param context additional information about the request. This context will be available via the * {@link Observation} once established. * @exception InvalidRequestException if the path is not valid. */ public ObserveRequest(ContentFormat format, String path, Map<String, String> context) throws InvalidRequestException { this(format, newPath(path), context); }
@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()); }
ObserveRequest observeRequest = new ObserveRequest(URI); ObserveResponse response = nodeEnabler.observe(identity, observeRequest); if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
protected ObserveResponse doObserve(ServerIdentity identity, ObserveRequest request) { ReadResponse readResponse = this.read(identity, new ReadRequest(request.getPath().toString())); return new ObserveResponse(readResponse.getCode(), readResponse.getContent(), null, null, readResponse.getErrorMessage()); }
@Override public synchronized ObserveResponse observe(ServerIdentity identity, ObserveRequest request) { LwM2mPath path = request.getPath(); // observe is not supported for bootstrap if (identity.isLwm2mBootstrapServer()) return ObserveResponse.methodNotAllowed(); if (!identity.isSystem()) { // observe or read of the security object is forbidden if (id == LwM2mId.SECURITY) return ObserveResponse.notFound(); // check if the resource is readable. if (path.isResource()) { ResourceModel resourceModel = objectModel.resources.get(path.getResourceId()); if (resourceModel != null && !resourceModel.operations.isReadable()) return ObserveResponse.methodNotAllowed(); } } return doObserve(identity, request); }
@Override public void visit(final ObserveRequest request) { switch (coapResponse.getCode()) { case CHANGED: // ignore changed response (this is probably a NOTIFY) lwM2mresponse = null; break; case CONTENT: lwM2mresponse = buildContentResponse(request.getPath(), coapResponse); if (coapResponse.getOptions().hasObserve()) { // observe request succeed so we can add and observation to registry final CaliforniumObservation observation = new CaliforniumObservation(coapRequest, client, request.getPath()); coapRequest.addMessageObserver(observation); observationRegistry.addObservation(observation); } break; case NOT_FOUND: case METHOD_NOT_ALLOWED: lwM2mresponse = new ValueResponse(fromCoapCode(coapResponse.getCode().value)); break; default: handleUnexpectedResponseCode(client.getEndpoint(), coapRequest, coapResponse); } }
@Override public void visit(ObserveRequest request) { if (coapResponse.isError()) { // handle error response: lwM2mresponse = new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), null, null, null, coapResponse.getPayloadString(), coapResponse); } else if (coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT // This is for backward compatibility, when the spec say notification used CHANGED code || coapResponse.getCode() == org.eclipse.californium.core.coap.CoAP.ResponseCode.CHANGED) { // handle success response: LwM2mNode content = decodeCoapResponse(request.getPath(), coapResponse, request, registration.getEndpoint()); if (coapResponse.getOptions().hasObserve()) { // observe request successful Observation observation = ObserveUtil.createLwM2mObservation(coapRequest); observationService.addObservation(registration, observation); // add the observation to an ObserveResponse instance lwM2mresponse = new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), content, null, observation, null, coapResponse); } else { lwM2mresponse = new ObserveResponse(toLwM2mResponseCode(coapResponse.getCode()), content, null, null, null, coapResponse); } } else { // handle unexpected response: handleUnexpectedResponseCode(registration.getEndpoint(), request, coapResponse); } }