Search in sources :

Example 1 with CreateRequest

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;
    }
}
Also used : ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) CodecException(org.eclipse.leshan.core.node.codec.CodecException)

Example 2 with CreateRequest

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());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Registration(org.eclipse.leshan.server.registration.Registration) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 3 with CreateRequest

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());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Tlv(org.eclipse.leshan.tlv.Tlv) Test(org.junit.Test)

Example 4 with CreateRequest

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)));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) Test(org.junit.Test)

Example 5 with CreateRequest

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)));
}
Also used : CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Test(org.junit.Test)

Aggregations

CreateRequest (org.eclipse.leshan.core.request.CreateRequest)17 Test (org.junit.Test)12 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)10 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)10 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)6 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)6 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 WriteAttributesRequest (org.eclipse.leshan.core.request.WriteAttributesRequest)4 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 Registration (org.eclipse.leshan.server.registration.Registration)3 JsonArray (com.eclipsesource.json.JsonArray)2 JsonObject (com.eclipsesource.json.JsonObject)2 Request (org.eclipse.californium.core.coap.Request)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2