Skip to content

Commit

Permalink
Add API to write object instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 1, 2014
1 parent 80d9340 commit b48a32a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -65,6 +66,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

/**
* Service HTTP REST API calls.
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Tlv> {

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b48a32a

Please sign in to comment.