Skip to content

Commit

Permalink
#894 : fix opaque write on single opaque value (server-demo)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 22, 2020
1 parent fc65183 commit 8e4c409
Showing 1 changed file with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.leshan.core.attributes.AttributeSet;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.request.ContentFormat;
Expand Down Expand Up @@ -62,6 +68,7 @@
import org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeSerializer;
import org.eclipse.leshan.server.demo.servlet.json.RegistrationSerializer;
import org.eclipse.leshan.server.demo.servlet.json.ResponseSerializer;
import org.eclipse.leshan.server.demo.utils.MagicLwM2mValueConverter;
import org.eclipse.leshan.server.registration.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -89,6 +96,10 @@ public class ClientServlet extends HttpServlet {

private final Gson gson;

private final LwM2mModel model;

private final MagicLwM2mValueConverter converter;

public ClientServlet(LeshanServer server) {
this.server = server;

Expand All @@ -100,6 +111,8 @@ public ClientServlet(LeshanServer server) {
gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
this.gson = gsonBuilder.create();
this.model = new StaticModel(ObjectLoader.loadDefault());
this.converter = new MagicLwM2mValueConverter();
}

/**
Expand Down Expand Up @@ -256,7 +269,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se
replace = Boolean.valueOf(replaceParam);

// create & process request
LwM2mNode node = extractLwM2mNode(target, req);
LwM2mNode node = extractLwM2mNode(target, req, new LwM2mPath(target));
WriteRequest request = new WriteRequest(replace ? Mode.REPLACE : Mode.UPDATE, contentFormat, target,
node);
WriteResponse cResponse = server.send(registration, request, extractTimeout(req));
Expand Down Expand Up @@ -341,7 +354,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
: null;

// create & process request
LwM2mNode node = extractLwM2mNode(target, req);
LwM2mNode node = extractLwM2mNode(target, req, new LwM2mPath(target));
if (node instanceof LwM2mObjectInstance) {
CreateRequest request;
if (node.getId() == LwM2mObjectInstance.UNDEFINED) {
Expand Down Expand Up @@ -421,13 +434,24 @@ private void processDeviceResponse(HttpServletRequest req, HttpServletResponse r
}
}

private LwM2mNode extractLwM2mNode(String target, HttpServletRequest req) throws IOException {
private LwM2mNode extractLwM2mNode(String target, HttpServletRequest req, LwM2mPath path) 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);
if (node instanceof LwM2mSingleResource) {
// TODO HACK resource type should be extracted from json value but this is not yet available.
LwM2mSingleResource singleResource = (LwM2mSingleResource) node;
ResourceModel resourceModel = model.getResourceModel(path.getObjectId(), singleResource.getId());
if (resourceModel != null) {
Type expectedType = resourceModel.type;
Object expectedValue = converter.convertValue(singleResource.getValue(),
singleResource.getType(), expectedType, path);
node = LwM2mSingleResource.newResource(node.getId(), expectedValue, expectedType);
}
}
} catch (JsonSyntaxException e) {
throw new InvalidRequestException(e, "unable to parse json to tlv:%s", e.getMessage());
}
Expand Down

0 comments on commit 8e4c409

Please sign in to comment.