Search in sources :

Example 1 with EndpointContext

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()));
    }
}
Also used : EndpointContext(org.eclipse.californium.elements.EndpointContext)

Example 2 with EndpointContext

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);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) RawData(org.eclipse.californium.elements.RawData) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) OptionSet(org.eclipse.californium.core.coap.OptionSet) UdpDataSerializer(org.eclipse.californium.core.network.serialization.UdpDataSerializer)

Example 3 with EndpointContext

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);
}
Also used : EndpointContext(org.eclipse.californium.elements.EndpointContext)

Example 4 with EndpointContext

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");
}
Also used : EndpointContext(org.eclipse.californium.elements.EndpointContext)

Example 5 with EndpointContext

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;
    }
}
Also used : SendFailedException(org.eclipse.leshan.core.request.exception.SendFailedException) EndpointMismatchException(org.eclipse.californium.elements.EndpointMismatchException) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) SimpleMessageCallback(org.eclipse.californium.elements.util.SimpleMessageCallback) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

EndpointContext (org.eclipse.californium.elements.EndpointContext)7 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)3 JsonValue (com.eclipsesource.json.JsonValue)2 RawData (org.eclipse.californium.elements.RawData)2 JsonObject (com.eclipsesource.json.JsonObject)1 Member (com.eclipsesource.json.JsonObject.Member)1 InetSocketAddress (java.net.InetSocketAddress)1 Principal (java.security.Principal)1 PublicKey (java.security.PublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 HashMap (java.util.HashMap)1 X500Principal (javax.security.auth.x500.X500Principal)1 OptionSet (org.eclipse.californium.core.coap.OptionSet)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 UdpDataSerializer (org.eclipse.californium.core.network.serialization.UdpDataSerializer)1 Observation (org.eclipse.californium.core.observe.Observation)1 EndpointMismatchException (org.eclipse.californium.elements.EndpointMismatchException)1