use of org.eclipse.leshan.core.response.CreateResponse in project leshan by eclipse.
the class ObjectResource method handlePOST.
@Override
public void handlePOST(CoapExchange exchange) {
ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler);
String URI = exchange.getRequestOptions().getUriPathString();
LwM2mPath path = new LwM2mPath(URI);
// Manage Execute Request
if (path.isResource()) {
byte[] payload = exchange.getRequestPayload();
ExecuteResponse response = nodeEnabler.execute(identity, new ExecuteRequest(URI, payload != null ? new String(payload) : null));
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(response.getCode()));
}
return;
}
// handle content format for Write (Update) and Create request
if (!exchange.getRequestOptions().hasContentFormat()) {
exchange.respond(ResponseCode.BAD_REQUEST, "Content Format is mandatory");
return;
}
ContentFormat contentFormat = ContentFormat.fromCode(exchange.getRequestOptions().getContentFormat());
if (!decoder.isSupported(contentFormat)) {
exchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
return;
}
LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
// Manage Update Instance
if (path.isObjectInstance()) {
try {
LwM2mNode lwM2mNode = decoder.decode(exchange.getRequestPayload(), contentFormat, path, model);
WriteResponse response = nodeEnabler.write(identity, new WriteRequest(Mode.UPDATE, contentFormat, URI, lwM2mNode));
if (response.getCode().isError()) {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
} else {
exchange.respond(toCoapResponseCode(response.getCode()));
}
} catch (CodecException e) {
LOG.warn("Unable to decode payload to write", e);
exchange.respond(ResponseCode.BAD_REQUEST);
}
return;
}
// Manage Create Request
try {
// decode the payload as an instance
LwM2mObjectInstance newInstance = decoder.decode(exchange.getRequestPayload(), contentFormat, new LwM2mPath(path.getObjectId()), model, LwM2mObjectInstance.class);
CreateRequest createRequest;
if (newInstance.getId() != LwM2mObjectInstance.UNDEFINED) {
createRequest = new CreateRequest(contentFormat, path.getObjectId(), newInstance);
} else {
// the instance Id was not part of the create request payload.
// will be assigned by the client.
createRequest = new CreateRequest(contentFormat, path.getObjectId(), newInstance.getResources().values());
}
CreateResponse response = nodeEnabler.create(identity, createRequest);
if (response.getCode() == org.eclipse.leshan.ResponseCode.CREATED) {
exchange.setLocationPath(response.getLocation());
exchange.respond(toCoapResponseCode(response.getCode()));
return;
} else {
exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
return;
}
} catch (CodecException e) {
LOG.warn("Unable to decode payload to create", e);
exchange.respond(ResponseCode.BAD_REQUEST);
return;
}
}
use of org.eclipse.leshan.core.response.CreateResponse in project leshan by eclipse.
the class CreateTest method can_create_specific_instance_of_object_with_json.
@Test
public void can_create_specific_instance_of_object_with_json() throws InterruptedException {
// create ACL instance
LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(ContentFormat.JSON, 2, instance));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/12", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.CreateResponse in project leshan by eclipse.
the class CreateTest method can_create_instance_of_object_without_instance_id.
@Test
public void can_create_instance_of_object_without_instance_id() throws InterruptedException {
// create ACL instance
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, new LwM2mResource[] { LwM2mSingleResource.newIntegerResource(0, 123) }));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/0", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
// create a second ACL instance
response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, new LwM2mResource[] { LwM2mSingleResource.newIntegerResource(0, 123) }));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/1", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.CreateResponse in project leshan by eclipse.
the class CreateTest method can_create_specific_instance_of_object.
@Test
public void can_create_specific_instance_of_object() throws InterruptedException {
// create ACL instance
LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, instance));
// verify result
assertEquals(ResponseCode.CREATED, response.getCode());
assertEquals("2/12", response.getLocation());
assertNotNull(response.getCoapResponse());
assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.response.CreateResponse in project leshan by eclipse.
the class ResponseSerDes method jSerialize.
public static JsonObject jSerialize(LwM2mResponse r) {
final JsonObject o = Json.object();
o.add("code", r.getCode().toString());
if (r.isFailure()) {
o.add("errorMessage", r.getErrorMessage());
return o;
}
if (r instanceof ReadResponse) {
o.add("kind", "read");
o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
} else if (r instanceof ObserveResponse) {
o.add("kind", "observe");
o.add("content", LwM2mNodeSerDes.jSerialize(((ReadResponse) r).getContent()));
} else if (r instanceof DiscoverResponse) {
o.add("kind", "discover");
o.add("objectLinks", Link.serialize(((DiscoverResponse) r).getObjectLinks()));
} else if (r instanceof DeleteResponse) {
o.add("kind", "delete");
} else if (r instanceof ExecuteResponse) {
o.add("kind", "execute");
} else if (r instanceof WriteResponse) {
o.add("kind", "write");
} else if (r instanceof WriteAttributesResponse) {
o.add("kind", "writeAttributes");
} else if (r instanceof CreateResponse) {
o.add("kind", "create");
o.add("location", ((CreateResponse) r).getLocation());
}
return o;
}
Aggregations