use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.
the class ObjectResource method handleGET.
@Override
public void handleGET(CoapExchange exchange) {
ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler);
String URI = exchange.getRequestOptions().getUriPathString();
// Manage Discover Request
if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) {
DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI));
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()), MediaTypeRegistry.APPLICATION_LINK_FORMAT);
}
} else {
// handle content format for Read and Observe Request
// use TLV as default format
ContentFormat format = ContentFormat.TLV;
if (exchange.getRequestOptions().hasAccept()) {
format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
if (!encoder.isSupported(format)) {
exchange.respond(ResponseCode.NOT_ACCEPTABLE);
return;
}
}
// Manage Observe Request
if (exchange.getRequestOptions().hasObserve()) {
ObserveResponse response = nodeEnabler.observe(identity, new ObserveRequest(URI));
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
return;
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
} else // Manage Read Request
{
ReadResponse response = nodeEnabler.read(identity, new ReadRequest(URI));
if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
return;
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
}
}
}
use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.
the class ObservationServiceImpl method onNotification.
// ********** NotificationListener interface **********//
@Override
public void onNotification(Request coapRequest, Response coapResponse) {
LOG.trace("notification received for request {}: {}", coapRequest, coapResponse);
if (listeners.isEmpty())
return;
// get registration Id
String regid = coapRequest.getUserContext().get(ObserveUtil.CTX_REGID);
// get observation for this request
Observation observation = registrationStore.getObservation(regid, coapResponse.getToken().getBytes());
if (observation == null) {
LOG.error("Unexpected error: Unable to find observation with token {} for registration {}", coapResponse.getToken(), regid);
return;
}
// get registration
Registration registration = registrationStore.getRegistration(observation.getRegistrationId());
if (registration == null) {
LOG.error("Unexpected error: There is no registration with id {} for this observation {}", observation.getRegistrationId(), observation);
return;
}
try {
// get model for this registration
LwM2mModel model = modelProvider.getObjectModel(registration);
// create response
ObserveResponse response = createObserveResponse(observation, model, coapResponse);
// notify all listeners
for (ObservationListener listener : listeners) {
listener.onResponse(observation, registration, response);
}
} catch (InvalidResponseException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Invalid notification for observation [%s]", observation), e);
}
for (ObservationListener listener : listeners) {
listener.onError(observation, registration, e);
}
} catch (RuntimeException e) {
if (LOG.isErrorEnabled()) {
LOG.error(String.format("Unable to handle notification for observation [%s]", observation), e);
}
for (ObservationListener listener : listeners) {
listener.onError(observation, registration, e);
}
}
}
use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.
the class ObserveTest method can_observe_resource.
@Test
public void can_observe_resource() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3/0/15", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"), listener.getResponse().getContent());
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.
the class ObserveTest method can_handle_error_on_notification.
@Test
public void can_handle_error_on_notification() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3/0/15", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// *** HACK send a notification with unsupported content format *** //
byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"), new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
// 666 is not a supported //
sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666);
// contentFormat.
// *** Hack End *** //
// verify result
listener.waitForNotification(2000);
assertTrue(listener.receivedNotify().get());
assertNotNull(listener.getError());
}
use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.
the class ObserveTest method can_observe_object.
@Test
public void can_observe_object() throws InterruptedException {
TestObservationListener listener = new TestObservationListener();
helper.server.getObservationService().addListener(listener);
// observe device timezone
ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
assertNotNull(observeResponse.getCoapResponse());
assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
// an observation response should have been sent
Observation observation = observeResponse.getObservation();
assertEquals("/3", observation.getPath().toString());
assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
assertTrue("We should have only on observation", observations.size() == 1);
assertTrue("New observation is not there", observations.contains(observation));
// write device timezone
LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
// verify result
listener.waitForNotification(2000);
assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
assertTrue(listener.receivedNotify().get());
assertTrue(listener.getResponse().getContent() instanceof LwM2mObject);
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
// try to read the object for comparing
ReadResponse readResp = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
assertEquals(readResp.getContent(), listener.getResponse().getContent());
}
Aggregations