Search in sources :

Example 1 with InvalidRequestException

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());
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidRequestException(org.eclipse.leshan.core.request.exception.InvalidRequestException) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)1 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)1 InvalidRequestException (org.eclipse.leshan.core.request.exception.InvalidRequestException)1