use of org.eclipse.leshan.core.request.exception.InvalidRequestException in project leshan by eclipse.
the class ClientServlet method extractLwM2mNode.
private LwM2mNode extractLwM2mNode(String target, HttpServletRequest req) throws IOException {
String contentType = StringUtils.substringBefore(req.getContentType(), ";");
if ("application/json".equals(contentType)) {
String content = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding());
LwM2mNode node;
try {
node = gson.fromJson(content, LwM2mNode.class);
} catch (JsonSyntaxException e) {
throw new InvalidRequestException(e, "unable to parse json to tlv:%s", e.getMessage());
}
return node;
} else if ("text/plain".equals(contentType)) {
String content = IOUtils.toString(req.getInputStream(), req.getCharacterEncoding());
int rscId = Integer.valueOf(target.substring(target.lastIndexOf("/") + 1));
return LwM2mSingleResource.newStringResource(rscId, content);
}
throw new InvalidRequestException("content type %s not supported", req.getContentType());
}
Aggregations