Search in sources :

Example 1 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class ObjectResource method handlePUT.

@Override
public void handlePUT(CoapExchange coapExchange) {
    ServerIdentity identity = extractServerIdentity(coapExchange, bootstrapHandler);
    String URI = coapExchange.getRequestOptions().getUriPathString();
    // get Observe Spec
    ObserveSpec spec = null;
    if (coapExchange.advanced().getRequest().getOptions().getURIQueryCount() != 0) {
        List<String> uriQueries = coapExchange.advanced().getRequest().getOptions().getUriQuery();
        spec = ObserveSpec.parse(uriQueries);
    }
    // Manage Write Attributes Request
    if (spec != null) {
        WriteAttributesResponse response = nodeEnabler.writeAttributes(identity, new WriteAttributesRequest(URI, spec));
        if (response.getCode().isError()) {
            coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
        } else {
            coapExchange.respond(toCoapResponseCode(response.getCode()));
        }
        return;
    } else // Manage Write and Bootstrap Write Request (replace)
    {
        LwM2mPath path = new LwM2mPath(URI);
        if (!coapExchange.getRequestOptions().hasContentFormat()) {
            coapExchange.respond(ResponseCode.BAD_REQUEST, "Content Format is mandatory");
            return;
        }
        ContentFormat contentFormat = ContentFormat.fromCode(coapExchange.getRequestOptions().getContentFormat());
        if (!decoder.isSupported(contentFormat)) {
            coapExchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
            return;
        }
        LwM2mNode lwM2mNode;
        try {
            LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
            lwM2mNode = decoder.decode(coapExchange.getRequestPayload(), contentFormat, path, model);
            if (identity.isLwm2mBootstrapServer()) {
                BootstrapWriteResponse response = nodeEnabler.write(identity, new BootstrapWriteRequest(path, lwM2mNode, contentFormat));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            } else {
                WriteResponse response = nodeEnabler.write(identity, new WriteRequest(Mode.REPLACE, contentFormat, URI, lwM2mNode));
                if (response.getCode().isError()) {
                    coapExchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                } else {
                    coapExchange.respond(toCoapResponseCode(response.getCode()));
                }
            }
            return;
        } catch (CodecException e) {
            LOG.warn("Unable to decode payload to write", e);
            coapExchange.respond(ResponseCode.BAD_REQUEST);
            return;
        }
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) WriteAttributesResponse(org.eclipse.leshan.core.response.WriteAttributesResponse) ObserveSpec(org.eclipse.leshan.ObserveSpec) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) 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) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Example 2 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse 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 3 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method can_write_replacing_object_instance.

@Test
public void can_write_replacing_object_instance() throws InterruptedException {
    // setup server object
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(1, 0, 3, 60));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // write server object
    LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120);
    LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10);
    LwM2mResource notificationStoring = LwM2mSingleResource.newBooleanResource(6, false);
    LwM2mResource binding = LwM2mSingleResource.newStringResource(7, "U");
    response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(Mode.REPLACE, 1, 0, lifetime, defaultMinPeriod, notificationStoring, binding));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read the values to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(1, 0));
    LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent();
    assertEquals(lifetime, instance.getResource(1));
    assertEquals(defaultMinPeriod, instance.getResource(2));
    assertEquals(notificationStoring, instance.getResource(6));
    assertEquals(binding, instance.getResource(7));
    // removed not contained optional writable resource
    assertNull(instance.getResource(3));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 4 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method write_time_resource.

private void write_time_resource(ContentFormat format) throws InterruptedException {
    // write resource
    // second accuracy
    Date expectedvalue = new Date(946681000l);
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, TIME_RESOURCE_ID, expectedvalue));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read resource to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, TIME_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(expectedvalue, resource.getValue());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Date(java.util.Date) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 5 with WriteResponse

use of org.eclipse.leshan.core.response.WriteResponse in project leshan by eclipse.

the class WriteTest method write_float_resource.

private void write_float_resource(ContentFormat format) throws InterruptedException {
    // write resource
    double expectedvalue = 999.99;
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID, expectedvalue));
    // verify result
    assertEquals(ResponseCode.CHANGED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    // read resource to check the value changed
    ReadResponse readResponse = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(TEST_OBJECT_ID, 0, FLOAT_RESOURCE_ID));
    LwM2mResource resource = (LwM2mResource) readResponse.getContent();
    assertEquals(expectedvalue, resource.getValue());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Aggregations

WriteResponse (org.eclipse.leshan.core.response.WriteResponse)21 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)19 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)14 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)12 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)10 Test (org.junit.Test)8 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)4 ObjectLink (org.eclipse.leshan.core.node.ObjectLink)3 ContentFormat (org.eclipse.leshan.core.request.ContentFormat)3 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)3 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)3 WriteAttributesResponse (org.eclipse.leshan.core.response.WriteAttributesResponse)3 JsonObject (com.eclipsesource.json.JsonObject)2 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)2 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)2 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)2 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)2 LwM2mSingleResource (org.eclipse.leshan.core.node.LwM2mSingleResource)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2