use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.
the class CoapRequestBuilder method setTarget.
private final void setTarget(Request coapRequest, LwM2mPath path) {
EndpointContext context = EndpointContextUtil.extractContext(destination);
coapRequest.setDestinationContext(context);
// root path
if (rootPath != null) {
for (String rootPathPart : rootPath.split("/")) {
if (!StringUtils.isEmpty(rootPathPart)) {
coapRequest.getOptions().addUriPath(rootPathPart);
}
}
}
// objectId
coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectId()));
// objectInstanceId
if (path.getObjectInstanceId() == null) {
if (path.getResourceId() != null) {
// default instanceId
coapRequest.getOptions().addUriPath("0");
}
} else {
coapRequest.getOptions().addUriPath(Integer.toString(path.getObjectInstanceId()));
}
// resourceId
if (path.getResourceId() != null) {
coapRequest.getOptions().addUriPath(Integer.toString(path.getResourceId()));
}
}
use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.
the class ObserveTest method sendNotification.
private void sendNotification(Connector connector, byte[] payload, Response firstCoapResponse, int contentFormat) {
// create observe response
Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT);
response.setType(Type.NON);
response.setPayload(payload);
response.setMID(firstCoapResponse.getMID() + 1);
response.setToken(firstCoapResponse.getToken());
OptionSet options = new OptionSet().setContentFormat(contentFormat).setObserve(firstCoapResponse.getOptions().getObserve() + 1);
response.setOptions(options);
EndpointContext context = new AddressEndpointContext(helper.server.getUnsecuredAddress().getAddress(), helper.server.getUnsecuredAddress().getPort());
response.setDestinationContext(context);
// serialize response
UdpDataSerializer serializer = new UdpDataSerializer();
RawData data = serializer.serializeResponse(response);
// send it
connector.send(data);
}
use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.
the class CoapRequestBuilder method visit.
@Override
public void visit(BootstrapDeleteRequest request) {
coapRequest = Request.newDelete();
coapRequest.setConfirmable(true);
EndpointContext context = EndpointContextUtil.extractContext(destination);
coapRequest.setDestinationContext(context);
}
use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.
the class CoapRequestBuilder method visit.
@Override
public void visit(BootstrapFinishRequest request) {
coapRequest = Request.newPost();
coapRequest.setConfirmable(true);
EndpointContext context = EndpointContextUtil.extractContext(destination);
coapRequest.setDestinationContext(context);
// root path
if (rootPath != null) {
for (String rootPathPart : rootPath.split("/")) {
if (!StringUtils.isEmpty(rootPathPart)) {
coapRequest.getOptions().addUriPath(rootPathPart);
}
}
}
coapRequest.getOptions().addUriPath("bs");
}
use of org.eclipse.californium.elements.EndpointContext in project leshan by eclipse.
the class SecurityTest method dont_sent_request_if_identity_change.
@Test
public void dont_sent_request_if_identity_change() throws NonUniqueSecurityInfoException, InterruptedException, IOException {
// Create PSK server & start it
// default server support PSK
helper.createServer();
helper.server.start();
// Create PSK Client
helper.createPSKClient();
// Add client credentials to the server
helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(helper.getCurrentEndpoint(), GOOD_PSK_ID, GOOD_PSK_KEY));
// Check client is not registered
helper.assertClientNotRegisterered();
// Start it and wait for registration
helper.client.start();
helper.waitForRegistration(1);
// Check client is well registered
helper.assertClientRegisterered();
// Ensure we can send a read request
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1));
// Pause the client
// helper.client.stop(false);
// Add new credential to the server
helper.getSecurityStore().add(SecurityInfo.newPreSharedKeyInfo(GOOD_ENDPOINT, "anotherPSK", GOOD_PSK_KEY));
// Get connector
Endpoint endpoint = helper.client.getCoapServer().getEndpoint(helper.client.getSecuredAddress());
DTLSConnector connector = (DTLSConnector) ((CoapEndpoint) endpoint).getConnector();
// Clear DTLS session to force new handshake
connector.clearConnectionState();
// Change PSK idea
helper.setNewPsk(helper.client, "anotherPSK");
// restart connector
connector.start();
// send and empty message to force a new handshake with new credentials
SimpleMessageCallback callback = new SimpleMessageCallback();
connector.send(RawData.outbound(new byte[0], new AddressEndpointContext(helper.server.getSecuredAddress()), callback, false));
// Wait until new handshake DTLS is done
EndpointContext endpointContext = callback.getEndpointContext(1000);
assertEquals(endpointContext.getPeerIdentity().getName(), "anotherPSK");
// Try to send a read request this should failed with an SendFailedException.
try {
helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3, 0, 1), 1000);
fail("send must failed");
} catch (SendFailedException e) {
assertTrue("must be caused by an EndpointMismatchException", e.getCause() instanceof EndpointMismatchException);
} finally {
connector.stop();
helper.client.destroy(false);
helper.client = null;
}
}
Aggregations