diff --git a/leshan-standalone/src/main/java/leshan/server/servlet/ClientServlet.java b/leshan-standalone/src/main/java/leshan/server/servlet/ClientServlet.java index 367df4ec..f6213d03 100644 --- a/leshan-standalone/src/main/java/leshan/server/servlet/ClientServlet.java +++ b/leshan-standalone/src/main/java/leshan/server/servlet/ClientServlet.java @@ -54,6 +54,7 @@ import leshan.server.lwm2m.tlv.Tlv; import leshan.server.servlet.json.ClientSerializer; import leshan.server.servlet.json.ResponseSerializer; +import leshan.server.servlet.json.TlvDeserializer; import leshan.server.servlet.json.TlvSerializer; import org.apache.commons.io.IOUtils; @@ -65,6 +66,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; /** * Service HTTP REST API calls. @@ -91,6 +93,7 @@ public ClientServlet(RequestHandler requestHandler, ClientRegistry clientRegistr GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Tlv.class, new TlvSerializer()); + gsonBuilder.registerTypeAdapter(Tlv.class, new TlvDeserializer()); gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer()); gsonBuilder.registerTypeHierarchyAdapter(ClientResponse.class, new ResponseSerializer()); this.gson = gsonBuilder.create(); @@ -170,7 +173,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws Se resp.getWriter().format("no registered client with id '%s'", requestInfo.endpoint).flush(); } } catch (IllegalArgumentException e) { - // content encoding other than text/plain is not supported (yet) + // content type not supported resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); resp.getWriter().append(e.getMessage()).flush(); } catch (ResourceAccessException e) { @@ -296,6 +299,16 @@ private ClientResponse writeRequest(Client client, RequestInfo requestInfo, Http String content = IOUtils.toString(req.getInputStream(), parameters.get("charset")); return WriteRequest.newReplaceRequest(client, requestInfo.objectId, requestInfo.objectInstanceId, requestInfo.resourceId, content, ContentFormat.TEXT).send(this.requestHandler); + } else if ("application/json".equals(contentType)) { + String content = IOUtils.toString(req.getInputStream(), parameters.get("charset")); + Tlv[] tlvs; + try { + tlvs = gson.fromJson(content, Tlv[].class); + } catch (JsonSyntaxException e) { + throw new IllegalArgumentException("unable to parse json to tlv:" + e.getMessage(), e); + } + return WriteRequest.newReplaceRequest(client, requestInfo.objectId, requestInfo.objectInstanceId, + requestInfo.resourceId, tlvs).send(this.requestHandler); } else { throw new IllegalArgumentException("content type " + req.getContentType() + " not supported for write requests"); diff --git a/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvDeserializer.java b/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvDeserializer.java new file mode 100644 index 00000000..1eb4ab73 --- /dev/null +++ b/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvDeserializer.java @@ -0,0 +1,72 @@ +package leshan.server.servlet.json; + +import java.lang.reflect.Type; + +import leshan.server.lwm2m.tlv.Tlv; +import leshan.server.lwm2m.tlv.TlvType; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; + +public class TlvDeserializer implements JsonDeserializer { + + @Override + public Tlv deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + if (json == null) { + return null; + } + + if (json.isJsonObject()) { + JsonObject object = (JsonObject) json; + + // id + int id; + if (object.has("id")) { + id = object.get("id").getAsInt(); + } else { + throw new JsonParseException("Missing id"); + } + + // type + TlvType tlvtype = null; + if (object.has("type")) { + String type = object.get("type").getAsString(); + tlvtype = TlvType.valueOf(type); + } else { + throw new JsonParseException("Missing type"); + } + + switch (tlvtype) { + case RESOURCE_VALUE: + case RESOURCE_INSTANCE: + if (object.has("value")) { + // TODO manage long and date + JsonPrimitive jsonPrimitive = object.get("value").getAsJsonPrimitive(); + if (jsonPrimitive.isString()) { + return Tlv.newStringValue(tlvtype, jsonPrimitive.getAsString(), id); + } else if (jsonPrimitive.isNumber()) { + return Tlv.newIntegerValue(tlvtype, jsonPrimitive.getAsInt(), id); + + } else if (jsonPrimitive.isBoolean()) { + return Tlv.newBooleanValue(tlvtype, jsonPrimitive.getAsBoolean(), id); + } + } else { + throw new JsonParseException("Missing value"); + } + break; + case OBJECT_INSTANCE: + case MULTIPLE_RESOURCE: + // TODO manage resources field (children) + throw new JsonParseException("OBJECT_INSTANCE and MULTIPLE_RESOURCE not supported"); + } + + } + return null; + } +} diff --git a/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvSerializer.java b/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvSerializer.java index 4ecd60a2..0abb2095 100644 --- a/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvSerializer.java +++ b/leshan-standalone/src/main/java/leshan/server/servlet/json/TlvSerializer.java @@ -53,6 +53,7 @@ public JsonElement serialize(Tlv src, Type typeOfSrc, JsonSerializationContext c try { JsonObject element = new JsonObject(); element.addProperty("id", src.getIdentifier()); + element.addProperty("type", src.getType().toString()); switch (src.getType()) { case RESOURCE_VALUE: case RESOURCE_INSTANCE: