Search in sources :

Example 1 with CreateResponse

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;
    }
}
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 CreateResponse

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)));
}
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 3 with CreateResponse

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)));
}
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)

Example 4 with CreateResponse

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)));
}
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 CreateResponse

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;
}
Also used : DeleteResponse(org.eclipse.leshan.core.response.DeleteResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) JsonObject(com.eclipsesource.json.JsonObject) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) ExecuteResponse(org.eclipse.leshan.core.response.ExecuteResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Aggregations

CreateResponse (org.eclipse.leshan.core.response.CreateResponse)12 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 Test (org.junit.Test)8 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)5 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)5 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)4 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)4 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)3 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)3 JsonObject (com.eclipsesource.json.JsonObject)2 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)2 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)2 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)2 DeleteResponse (org.eclipse.leshan.core.response.DeleteResponse)2 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)2 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)2 Ignore (org.junit.Ignore)2 ResponseCode (org.eclipse.leshan.ResponseCode)1 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)1