use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class ClientServlet method doPost.
/**
* {@inheritDoc}
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] path = StringUtils.split(req.getPathInfo(), '/');
String clientEndpoint = path[0];
// /clients/endPoint/LWRequest/observe : do LightWeight M2M observe request on a given client.
if (path.length >= 3 && "observe".equals(path[path.length - 1])) {
try {
String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/observe");
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
// get content format
String contentFormatParam = req.getParameter(FORMAT_PARAM);
ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
// create & process request
ObserveRequest request = new ObserveRequest(contentFormat, target);
ObserveResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
// /clients/endPoint/LWRequest : do LightWeight M2M execute request on a given client.
if (path.length == 4) {
try {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
ExecuteRequest request = new ExecuteRequest(target, IOUtils.toString(req.getInputStream()));
ExecuteResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
// /clients/endPoint/LWRequest : do LightWeight M2M create request on a given client.
if (2 <= path.length && path.length <= 3) {
try {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
// get content format
String contentFormatParam = req.getParameter(FORMAT_PARAM);
ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
// create & process request
LwM2mNode node = extractLwM2mNode(target, req);
if (node instanceof LwM2mObjectInstance) {
CreateRequest request = new CreateRequest(contentFormat, target, (LwM2mObjectInstance) node);
CreateResponse cResponse = server.send(registration, request, TIMEOUT);
processDeviceResponse(req, resp, cResponse);
} else {
throw new IllegalArgumentException("payload must contain an object instance");
}
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
} catch (RuntimeException | InterruptedException e) {
handleException(e, resp);
}
return;
}
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class RedisRegistrationStore method addRegistration.
/* *************** Leshan Registration API **************** */
@Override
public Deregistration addRegistration(Registration registration) {
try (Jedis j = pool.getResource()) {
byte[] lockValue = null;
byte[] lockKey = toLockKey(registration.getEndpoint());
try {
lockValue = RedisLock.acquire(j, lockKey);
// add registration
byte[] k = toEndpointKey(registration.getEndpoint());
byte[] old = j.getSet(k, serializeReg(registration));
// add registration: secondary index
byte[] idx = toRegIdKey(registration.getId());
j.set(idx, registration.getEndpoint().getBytes(UTF_8));
if (old != null) {
Registration oldRegistration = deserializeReg(old);
// remove old secondary index
if (registration.getId() != oldRegistration.getId())
j.del(toRegIdKey(oldRegistration.getId()));
// remove old observation
Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, oldRegistration.getId());
return new Deregistration(oldRegistration, obsRemoved);
}
return null;
} finally {
RedisLock.release(j, lockKey, lockValue);
}
}
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class RedisRegistrationStore method removeRegistration.
private Deregistration removeRegistration(Jedis j, String registrationId, boolean removeOnlyIfNotAlive) {
// fetch the client ep by registration ID index
byte[] ep = j.get(toRegIdKey(registrationId));
if (ep == null) {
return null;
}
byte[] lockValue = null;
byte[] lockKey = toLockKey(ep);
try {
lockValue = RedisLock.acquire(j, lockKey);
// fetch the client
byte[] data = j.get(toEndpointKey(ep));
if (data == null) {
return null;
}
Registration r = deserializeReg(data);
if (!removeOnlyIfNotAlive || !r.isAlive(gracePeriod)) {
long nbRemoved = j.del(toRegIdKey(r.getId()));
if (nbRemoved > 0) {
j.del(toEndpointKey(r.getEndpoint()));
Collection<Observation> obsRemoved = unsafeRemoveAllObservations(j, r.getId());
return new Deregistration(r, obsRemoved);
}
}
return null;
} finally {
RedisLock.release(j, lockKey, lockValue);
}
}
use of org.eclipse.leshan.server.registration.Registration in project leshan by eclipse.
the class RegistrationSerDesTest method ser_and_des_are_equals.
@Test
public void ser_and_des_are_equals() throws Exception {
Link[] objs = new Link[2];
Map<String, Object> att = new HashMap<>();
att.put("ts", 12);
att.put("rt", "test");
att.put("hb", null);
objs[0] = new Link("/0/1024/2", att);
objs[1] = new Link("/0/2");
Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", Identity.unsecure(Inet4Address.getLoopbackAddress(), 1), new InetSocketAddress(212)).objectLinks(objs);
builder.registrationDate(new Date(100L));
builder.lastUpdate(new Date(101L));
Registration r = builder.build();
byte[] ser = RegistrationSerDes.bSerialize(r);
Registration r2 = RegistrationSerDes.deserialize(ser);
assertEquals(r, r2);
}
Aggregations