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