Search in sources :

Example 16 with WriteResponse

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

the class SecureIntegrationTestHelper method setNewPsk.

public void setNewPsk(LwM2mClient client, String identity) {
    LwM2mObjectEnabler securityObject = client.getObjectEnablers().get(0);
    WriteResponse write = securityObject.write(ServerIdentity.SYSTEM, new WriteRequest(LwM2mId.SECURITY, 0, LwM2mId.SEC_PUBKEY_IDENTITY, identity.getBytes()));
    System.out.println(write);
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse)

Example 17 with WriteResponse

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

the class WriteTest method cannot_write_security_resource.

@Test
public void cannot_write_security_resource() throws InterruptedException {
    // try to write unwritable resource like manufacturer on device
    String uri = "new.dest.server";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(0, 0, 0, uri));
    // verify result
    assertEquals(ResponseCode.NOT_FOUND, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Test(org.junit.Test)

Example 18 with WriteResponse

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

the class WriteTest method write_string_resource.

private void write_string_resource(ContentFormat format) throws InterruptedException {
    // write resource
    String expectedvalue = "stringvalue";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(format, TEST_OBJECT_ID, 0, STRING_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, STRING_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)

Example 19 with WriteResponse

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

the class WriteTest method cannot_write_non_writable_resource.

@Test
public void cannot_write_non_writable_resource() throws InterruptedException {
    // try to write unwritable resource like manufacturer on device
    String manufacturer = "new manufacturer";
    WriteResponse response = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 0, manufacturer));
    // verify result
    assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) Test(org.junit.Test)

Example 20 with WriteResponse

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

the class ClientServlet method doPut.

/**
 * {@inheritDoc}
 */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    String clientEndpoint = path[0];
    // at least /endpoint/objectId/instanceId
    if (path.length < 3) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid path");
        return;
    }
    try {
        String target = StringUtils.removeStart(req.getPathInfo(), "/" + clientEndpoint);
        Registration registration = server.getRegistrationService().getByEndpoint(clientEndpoint);
        if (registration != null) {
            // get content format
            String contentFormatParam = req.getParameter(FORMAT_PARAM);
            ContentFormat contentFormat = contentFormatParam != null ? ContentFormat.fromName(contentFormatParam.toUpperCase()) : null;
            // create & process request
            LwM2mNode node = extractLwM2mNode(target, req);
            WriteRequest request = new WriteRequest(Mode.REPLACE, contentFormat, target, node);
            WriteResponse cResponse = server.send(registration, request, TIMEOUT);
            processDeviceResponse(req, resp, cResponse);
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().format("No registered client with id '%s'", clientEndpoint).flush();
        }
    } catch (RuntimeException | InterruptedException e) {
        handleException(e, resp);
    }
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) WriteResponse(org.eclipse.leshan.core.response.WriteResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode)

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