use of org.eclipse.leshan.core.request.CreateRequest 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.request.CreateRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_create_request__with_instance_id.
@Test
public void build_create_request__with_instance_id() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
CreateRequest request = new CreateRequest(12, new LwM2mObjectInstance(26, LwM2mSingleResource.newStringResource(0, "value")));
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.POST, coapRequest.getCode());
assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
assertNotNull(coapRequest.getPayload());
// assert it is encoded as array of instance TLV
Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
assertEquals(TlvType.OBJECT_INSTANCE, tlvs[0].getType());
assertEquals(26, tlvs[0].getIdentifier());
}
use of org.eclipse.leshan.core.request.CreateRequest in project leshan by eclipse.
the class CoapRequestBuilderTest method build_create_request__without_instance_id.
@Test
public void build_create_request__without_instance_id() throws Exception {
Registration reg = newRegistration();
// test
CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
CreateRequest request = new CreateRequest(12, LwM2mSingleResource.newStringResource(0, "value"));
builder.visit(request);
// verify
Request coapRequest = builder.getRequest();
assertEquals(CoAP.Code.POST, coapRequest.getCode());
assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
assertEquals("coap://127.0.0.1:12354/12", coapRequest.getURI());
assertEquals(ContentFormat.TLV.getCode(), coapRequest.getOptions().getContentFormat());
assertNotNull(coapRequest.getPayload());
// assert it is encoded as array of resources TLV
Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(coapRequest.getPayload()));
assertEquals(TlvType.RESOURCE_VALUE, tlvs[0].getType());
}
use of org.eclipse.leshan.core.request.CreateRequest 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.request.CreateRequest 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)));
}
Aggregations