use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.
the class DiscoverTest method cant_discover_resource_of_non_existent_resource.
@Test
public void cant_discover_resource_of_non_existent_resource() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 0, 42));
// verify result
assertEquals(NOT_FOUND, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.
the class DiscoverTest method cant_discover_resource_of_non_existent_instance.
@Test
public void cant_discover_resource_of_non_existent_instance() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 1, 0));
// verify result
assertEquals(NOT_FOUND, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.
the class DiscoverTest method cant_discover_non_existent_instance.
@Test
public void cant_discover_non_existent_instance() throws InterruptedException {
// read ACL object
DiscoverResponse response = helper.server.send(helper.getCurrentRegistration(), new DiscoverRequest(3, 1));
// verify result
assertEquals(NOT_FOUND, response.getCode());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.
the class ClientServlet method doGet.
/**
* {@inheritDoc}
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// all registered clients
if (req.getPathInfo() == null) {
Collection<Registration> registrations = new ArrayList<>();
for (Iterator<Registration> iterator = server.getRegistrationService().getAllRegistrations(); iterator.hasNext(); ) {
registrations.add(iterator.next());
}
String json = this.gson.toJson(registrations.toArray(new Registration[] {}));
resp.setContentType("application/json");
resp.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
resp.setStatus(HttpServletResponse.SC_OK);
return;
}
String[] path = StringUtils.split(req.getPathInfo(), '/');
if (path.length < 1) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
return;
}
String clientEndpoint = path[0];
// /endPoint : get client
if (path.length == 1) {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
resp.setContentType("application/json");
resp.getOutputStream().write(this.gson.toJson(registration).getBytes(StandardCharsets.UTF_8));
resp.setStatus(HttpServletResponse.SC_OK);
} else {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().format("no registered client with id '%s'", clientEndpoint).flush();
}
return;
}
// /clients/endPoint/LWRequest/discover : do LightWeight M2M discover request on a given client.
if (path.length >= 3 && "discover".equals(path[path.length - 1])) {
String target = StringUtils.substringBetween(req.getPathInfo(), clientEndpoint, "/discover");
try {
Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
if (registration != null) {
// create & process request
DiscoverRequest request = new DiscoverRequest(target);
DiscoverResponse 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 read request on a given client.
try {
String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
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
ReadRequest request = new ReadRequest(contentFormat, target);
ReadResponse 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);
}
}
use of org.eclipse.leshan.core.response.DiscoverResponse in project leshan by eclipse.
the class ResponseSerDes method deserialize.
public static LwM2mResponse deserialize(JsonObject o) {
String sCode = o.getString("code", null);
if (sCode == null)
throw new IllegalStateException("Invalid response missing code attribute");
ResponseCode code = ResponseCode.fromName(sCode);
String errorMessage = o.getString("errorMessage", null);
String kind = o.getString("kind", null);
switch(kind) {
case "observe":
{
// TODO ser Observation
LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
return new ObserveResponse(code, content, null, null, errorMessage);
}
case "delete":
return new DeleteResponse(code, errorMessage);
case "discover":
String objectLinks = o.getString("objectLinks", "");
return new DiscoverResponse(code, Link.parse(objectLinks.getBytes()), errorMessage);
case "create":
{
String location = o.getString("location", null);
return new CreateResponse(code, location, errorMessage);
}
case "execute":
return new ExecuteResponse(code, errorMessage);
case "writeAttributes":
{
return new WriteAttributesResponse(code, errorMessage);
}
case "write":
{
return new WriteResponse(code, errorMessage);
}
case "read":
{
LwM2mNode content = LwM2mNodeSerDes.deserialize((JsonObject) o.get("content"));
return new ReadResponse(code, content, errorMessage);
}
default:
throw new IllegalStateException("Invalid response missing kind attribute");
}
}
Aggregations