use of org.eclipse.leshan.core.response.CreateResponse 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.core.response.CreateResponse 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