diff --git a/leshan-demo-bsserver/src/main/java/org/eclipse/leshan/demo/bsserver/LeshanBootstrapServerDemo.java b/leshan-demo-bsserver/src/main/java/org/eclipse/leshan/demo/bsserver/LeshanBootstrapServerDemo.java index e19a8e8e70..13bbca7d71 100755 --- a/leshan-demo-bsserver/src/main/java/org/eclipse/leshan/demo/bsserver/LeshanBootstrapServerDemo.java +++ b/leshan-demo-bsserver/src/main/java/org/eclipse/leshan/demo/bsserver/LeshanBootstrapServerDemo.java @@ -36,7 +36,8 @@ import org.eclipse.leshan.bsserver.LeshanBootstrapServerBuilder; import org.eclipse.leshan.bsserver.model.VersionedBootstrapModelProvider; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStoreAdapter; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.ObjectLoader; import org.eclipse.leshan.core.model.ObjectModel; @@ -73,6 +74,8 @@ public class LeshanBootstrapServerDemo { private static final String CF_CONFIGURATION_HEADER = "Leshan Bootstrap Server Demo - " + Configuration.DEFAULT_HEADER; + private static final EndPointUriHandler uriHandler = new DefaultEndPointUriHandler(); + public static void main(String[] args) { // Parse command line @@ -189,7 +192,7 @@ public static LeshanBootstrapServer createBsLeshanServer(LeshanBsServerDemoCLI c endpointsBuilder.addEndpoint(coapAddr, Protocol.COAP); } else { endpointsBuilder.addEndpoint(new CoapOscoreBootstrapServerEndpointFactory( - EndpointUriUtil.createUri(Protocol.COAP.getUriScheme(), coapAddr))); + uriHandler.createUri(Protocol.COAP.getUriScheme(), coapAddr))); } // Create CoAP over DTLS endpoint diff --git a/leshan-demo-server/src/main/java/org/eclipse/leshan/demo/server/LeshanServerDemo.java b/leshan-demo-server/src/main/java/org/eclipse/leshan/demo/server/LeshanServerDemo.java index 6f504f1ddc..8d4355754d 100644 --- a/leshan-demo-server/src/main/java/org/eclipse/leshan/demo/server/LeshanServerDemo.java +++ b/leshan-demo-server/src/main/java/org/eclipse/leshan/demo/server/LeshanServerDemo.java @@ -37,7 +37,8 @@ import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.ObjectLoader; import org.eclipse.leshan.core.model.ObjectModel; @@ -83,6 +84,7 @@ public class LeshanServerDemo { private static final Logger LOG = LoggerFactory.getLogger(LeshanServerDemo.class); private static final String CF_CONFIGURATION_FILENAME = "Californium3.server.properties"; private static final String CF_CONFIGURATION_HEADER = "Leshan Server Demo - " + Configuration.DEFAULT_HEADER; + private static final EndPointUriHandler uriHandler = new DefaultEndPointUriHandler(); public static void main(String[] args) { @@ -242,8 +244,8 @@ public static LeshanServer createLeshanServer(LeshanServerDemoCLI cli) throws Ex if (cli.main.disableOscore) { endpointsBuilder.addEndpoint(coapAddr, Protocol.COAP); } else { - endpointsBuilder.addEndpoint(new CoapOscoreServerEndpointFactory( - EndpointUriUtil.createUri(Protocol.COAP.getUriScheme(), coapAddr))); + endpointsBuilder.addEndpoint( + new CoapOscoreServerEndpointFactory(uriHandler.createUri(Protocol.COAP.getUriScheme(), coapAddr))); } // Create CoAP over DTLS endpoint diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepLwM2mClient.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepLwM2mClient.java index 769c1cbf01..1276e1619d 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepLwM2mClient.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepLwM2mClient.java @@ -16,8 +16,9 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.lockstep; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; + import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import java.util.Random; @@ -30,7 +31,7 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.SystemConfig; import org.eclipse.californium.elements.config.UdpConfig; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.link.DefaultLinkSerializer; import org.eclipse.leshan.core.link.LinkSerializer; import org.eclipse.leshan.core.model.LwM2mModel; @@ -49,6 +50,7 @@ public class LockStepLwM2mClient extends LockstepEndpoint { private static final Random r = new Random(); + private final InetSocketAddress destination; private final LwM2mEncoder encoder; private final LwM2mModel model; @@ -56,8 +58,8 @@ public class LockStepLwM2mClient extends LockstepEndpoint { private final String endpointName; - public LockStepLwM2mClient(final URI destination) { - this(EndpointUriUtil.getSocketAddr(destination)); + public LockStepLwM2mClient(final EndpointUri destination) { + this(uriHandler.getSocketAddr(destination)); } public LockStepLwM2mClient(final InetSocketAddress destination) { diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java index 07e158ddf5..8c40ba5fbf 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java @@ -33,7 +33,6 @@ import java.lang.annotation.RetentionPolicy; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; @@ -56,6 +55,7 @@ import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.elements.AddressEndpointContext; import org.eclipse.californium.elements.config.Configuration; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.LinkParser; import org.eclipse.leshan.core.link.lwm2m.DefaultLwM2mLinkParser; @@ -183,7 +183,7 @@ public void register_with_invalid_request(String givenServerEndpointProvider) th // create a register request without the list of supported object Request invalidRegisterRequest = new Request(Code.POST); - URI destinationURI = server.getEndpoint(Protocol.COAP).getURI(); + EndpointUri destinationURI = server.getEndpoint(Protocol.COAP).getURI(); invalidRegisterRequest .setDestinationContext(new AddressEndpointContext(destinationURI.getHost(), destinationURI.getPort())); invalidRegisterRequest.getOptions().setContentFormat(ContentFormat.LINK.getCode()); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java index 68c8b9b56b..c711ceeb88 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.observe; -import java.net.URI; import java.util.Random; import org.eclipse.californium.core.coap.CoAP.Type; @@ -26,13 +25,14 @@ import org.eclipse.californium.elements.Connector; import org.eclipse.californium.elements.EndpointContext; import org.eclipse.californium.elements.RawData; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.request.ContentFormat; public class TestObserveUtil { public static Random r = new Random(); - public static void sendNotification(Connector connector, URI destination, byte[] payload, byte[] token, int observe, - ContentFormat contentFormat) { + public static void sendNotification(Connector connector, EndpointUri destination, byte[] payload, byte[] token, + int observe, ContentFormat contentFormat) { // create observe response Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/security/ServerOnlySecurityTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/security/ServerOnlySecurityTest.java index 967737f1c2..9268ea3d71 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/security/ServerOnlySecurityTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/security/ServerOnlySecurityTest.java @@ -28,7 +28,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.net.InetSocketAddress; -import java.net.URI; import java.security.cert.Certificate; import java.util.List; import java.util.stream.Stream; @@ -51,6 +50,7 @@ import org.eclipse.californium.scandium.dtls.PskPublicInformation; import org.eclipse.californium.scandium.dtls.pskstore.AdvancedPskStore; import org.eclipse.leshan.client.servers.ServerInfo; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.ReadRequest; import org.eclipse.leshan.core.request.exception.SendFailedException; @@ -210,7 +210,7 @@ public void dont_sent_request_if_identity_change(String givenServerEndpointProvi request.setMID(0); byte[] ping = new UdpDataSerializer().getByteArray(request); // sent it - URI destinationUri = server.getEndpoint(Protocol.COAPS).getURI(); + EndpointUri destinationUri = server.getEndpoint(Protocol.COAPS).getURI(); connector.send(RawData.outbound(ping, new AddressEndpointContext(destinationUri.getHost(), destinationUri.getPort()), callback, false)); // Wait until new handshake DTLS is done diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java index e43512a646..552da82b78 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/server/redis/RedisRegistrationStoreTest.java @@ -16,6 +16,7 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.server.redis; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -35,7 +36,6 @@ import org.eclipse.californium.core.network.serialization.UdpDataParser; import org.eclipse.californium.core.network.serialization.UdpDataSerializer; import org.eclipse.californium.elements.AddressEndpointContext; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.observation.CompositeObservation; @@ -179,7 +179,7 @@ public void remove_observation() { private void givenASimpleRegistration(Long lifetime) { Registration.Builder builder = new Registration.Builder(registrationId, ep, - new IpPeer(new InetSocketAddress(address, port)), EndpointUriUtil.createUri("coap://localhost:5683")); + new IpPeer(new InetSocketAddress(address, port)), uriHandler.createUri("coap://localhost:5683")); registration = builder.lifeTimeInSec(lifetime).smsNumber(sms).bindingMode(binding).objectLinks(objectLinks) .build(); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java index 7915f0f571..8d25d21d87 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanProxyBuilder.java @@ -16,14 +16,14 @@ package org.eclipse.leshan.integration.tests.util; import java.net.InetSocketAddress; -import java.net.URI; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; public class LeshanProxyBuilder { public static ReverseProxy givenReverseProxyFor(LeshanTestServer server, Protocol protocol) { - URI serverEndpointUri = server.getEndpoint(protocol).getURI(); + EndpointUri serverEndpointUri = server.getEndpoint(protocol).getURI(); return new ReverseProxy(new InetSocketAddress("localhost", 0), new InetSocketAddress(serverEndpointUri.getHost(), serverEndpointUri.getPort())); } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServer.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServer.java index a810d31b1a..e11a74dc1d 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServer.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServer.java @@ -33,6 +33,7 @@ import org.eclipse.leshan.bsserver.LeshanBootstrapServer; import org.eclipse.leshan.bsserver.endpoint.LwM2mBootstrapServerEndpointsProvider; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStore; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; import org.eclipse.leshan.core.node.codec.LwM2mEncoder; @@ -50,13 +51,13 @@ public class LeshanTestBootstrapServer extends LeshanBootstrapServer { public LeshanTestBootstrapServer(LwM2mBootstrapServerEndpointsProvider endpointsProvider, BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mEncoder encoder, - LwM2mDecoder decoder, LwM2mLinkParser linkParser, BootstrapSecurityStore securityStore, - ServerSecurityInfo serverSecurityInfo, // + LwM2mDecoder decoder, LwM2mLinkParser linkParser, EndPointUriHandler uriHandler, + BootstrapSecurityStore securityStore, ServerSecurityInfo serverSecurityInfo, // // arguments only needed for LeshanTestBootstrapServer EditableBootstrapConfigStore configStore, EditableSecurityStore editableSecurityStore) { - super(endpointsProvider, bsSessionManager, bsHandlerFactory, encoder, decoder, linkParser, securityStore, - serverSecurityInfo); + super(endpointsProvider, bsSessionManager, bsHandlerFactory, encoder, decoder, linkParser, uriHandler, + securityStore, serverSecurityInfo); // keep store reference for getter. this.configStore = configStore; this.securityStore = editableSecurityStore; diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServerBuilder.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServerBuilder.java index c930d8de70..a32339bcb5 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServerBuilder.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestBootstrapServerBuilder.java @@ -17,7 +17,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.X509Certificate; @@ -34,6 +33,8 @@ import org.eclipse.leshan.bsserver.endpoint.LwM2mBootstrapServerEndpointsProvider; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStore; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStoreAdapter; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder; @@ -82,8 +83,8 @@ public LeshanTestBootstrapServer build() { @Override protected LeshanTestBootstrapServer createBootstrapServer(LwM2mBootstrapServerEndpointsProvider endpointsProvider, BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mEncoder encoder, - LwM2mDecoder decoder, LwM2mLinkParser linkParser, BootstrapSecurityStore securityStore, - ServerSecurityInfo serverSecurityInfo) { + LwM2mDecoder decoder, LwM2mLinkParser linkParser, EndPointUriHandler uriHandler, + BootstrapSecurityStore securityStore, ServerSecurityInfo serverSecurityInfo) { // create endpoint provider. if (endpointsProvider == null) { @@ -108,7 +109,7 @@ protected LeshanTestBootstrapServer createBootstrapServer(LwM2mBootstrapServerEn } return new LeshanTestBootstrapServer(endpointsProvider, bsSessionManager, bsHandlerFactory, encoder, decoder, - linkParser, securityStore, serverSecurityInfo, // + linkParser, uriHandler, securityStore, serverSecurityInfo, // // arguments only needed for LeshanTestBootstrapServer configStore, editableSecurityStore); } @@ -206,8 +207,9 @@ private BootstrapServerProtocolProvider getCaliforniumProtocolProviderSupporting if (protocolToUse.equals(Protocol.COAP)) { return new CoapBootstrapServerProtocolProvider() { @Override - public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(URI uri) { - return new CoapOscoreBootstrapServerEndpointFactory(uri); + public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler uriHandler) { + return new CoapOscoreBootstrapServerEndpointFactory(uri, uriHandler); } }; } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClient.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClient.java index 0af5d5df5a..6bcd531794 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClient.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClient.java @@ -16,6 +16,7 @@ package org.eclipse.leshan.integration.tests.util; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertArg; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.isNotNull; @@ -26,7 +27,6 @@ import static org.mockito.Mockito.timeout; import java.net.InetSocketAddress; -import java.net.URI; import java.security.cert.Certificate; import java.util.List; import java.util.Map; @@ -47,7 +47,8 @@ import org.eclipse.leshan.client.send.DataSender; import org.eclipse.leshan.client.servers.LwM2mServer; import org.eclipse.leshan.client.util.LinkFormatHelper; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.link.LinkSerializer; import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; @@ -71,10 +72,11 @@ public LeshanTestClient(String endpoint, List obje BootstrapConsistencyChecker checker, Map additionalAttributes, Map bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder, ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper, - LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider, ReverseProxy proxy) { + LwM2mAttributeParser attributeParser, EndPointUriHandler uriHandler, + LwM2mClientEndpointsProvider endpointsProvider, ReverseProxy proxy) { super(endpoint, objectEnablers, dataSenders, trustStore, engineFactory, checker, additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor, linkSerializer, linkFormatHelper, - attributeParser, endpointsProvider); + attributeParser, uriHandler, endpointsProvider); // Store some internal attribute this.endpointName = endpoint; @@ -244,10 +246,10 @@ public Exception waitForBootstrapFailure(LeshanBootstrapServer server, long time private boolean isServerIdentifiedByUri(LeshanTestServer server, String expectedUri) { for (LwM2mServerEndpoint endpoint : server.getEndpoints()) { - URI endpointURI = endpoint.getURI(); - InetSocketAddress endpointAddr = EndpointUriUtil.getSocketAddr(endpointURI); + EndpointUri endpointURI = endpoint.getURI(); + InetSocketAddress endpointAddr = uriHandler.getSocketAddr(endpointURI); if (proxy != null && endpointAddr.equals(proxy.getServerAddress())) { - URI proxyUri = EndpointUriUtil.replaceAddress(endpointURI, proxy.getClientSideProxyAddress()); + EndpointUri proxyUri = uriHandler.replaceAddress(endpointURI, proxy.getClientSideProxyAddress()); if (proxyUri.toString().equals(expectedUri)) { return true; } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClientBuilder.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClientBuilder.java index e15827e402..d262ab577f 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClientBuilder.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestClientBuilder.java @@ -16,9 +16,9 @@ package org.eclipse.leshan.integration.tests.util; import static org.eclipse.leshan.core.LwM2mId.OSCORE; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import java.net.InetSocketAddress; -import java.net.URI; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.Certificate; @@ -59,7 +59,8 @@ import org.eclipse.leshan.client.util.LinkFormatHelper; import org.eclipse.leshan.core.CertificateUsage; import org.eclipse.leshan.core.LwM2mId; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.LinkSerializer; import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser; @@ -88,7 +89,6 @@ public class LeshanTestClientBuilder extends LeshanClientBuilder { private static final Random r = new Random(); - private String endpointName; private Protocol protocolToUse; private LeshanServer server; @@ -135,7 +135,7 @@ public LeshanTestClient build() { try { // connect to LWM2M Server if (server != null) { - URI uri = getServerUri(); + EndpointUri uri = getServerUri(); int serverID = 12345; @@ -166,7 +166,7 @@ public LeshanTestClient build() { // connect to LWM2M Bootstrap Server else if (bootstrapServer != null) { LwM2mBootstrapServerEndpoint endpoint = bootstrapServer.getEndpoint(protocolToUse); - URI uri = endpoint.getURI(); + EndpointUri uri = endpoint.getURI(); Security securityEnabler = null; if (pskIdentity != null && pskKey != null) { @@ -230,7 +230,8 @@ protected LeshanTestClient createLeshanClient(String endpoint, List additionalAttributes, Map bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder, ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper, - LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) { + LwM2mAttributeParser attributeParser, EndPointUriHandler uriHandler, + LwM2mClientEndpointsProvider endpointsProvider) { String endpointName; if (this.endpointName != null) { endpointName = this.endpointName; @@ -243,7 +244,7 @@ protected LeshanTestClient createLeshanClient(String endpoint, Listjunit-jupiter test + + org.eclipse.leshan + leshan-lwm2m-core + ${project.version} + test-jar + ch.qos.logback logback-classic diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapHandler.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapHandler.java index 367b94f3bc..c8168e844c 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapHandler.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapHandler.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapRequest; import org.eclipse.leshan.core.response.BootstrapResponse; @@ -31,5 +30,6 @@ public interface BootstrapHandler { - SendableResponse bootstrap(LwM2mPeer sender, BootstrapRequest request, URI serverEndpointUri); + SendableResponse bootstrap(LwM2mPeer sender, BootstrapRequest request, + EndpointUri serverEndpointUri); } diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSession.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSession.java index 9c9b7cd2c9..324ce402b0 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSession.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSession.java @@ -15,10 +15,10 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; -import java.net.URI; import java.util.Map; import org.eclipse.leshan.bsserver.security.BootstrapAuthorizer; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.model.LwM2mModel; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapRequest; @@ -28,7 +28,7 @@ * Represent a single Bootstrapping session. * * Should be created by {@link BootstrapSessionManager} implementations in - * {@link BootstrapSessionManager#begin(BootstrapRequest, LwM2mPeer, URI)}. + * {@link BootstrapSessionManager#begin(BootstrapRequest, LwM2mPeer, EndpointUri)}. */ public interface BootstrapSession { @@ -52,7 +52,10 @@ public interface BootstrapSession { */ LwM2mPeer getClientTransportData(); - URI getEndpointUsed(); + /** + * @return the URI of the endpoint used for this session + */ + EndpointUri getEndpointUsed(); /** * @return true if the LwM2M client is authorized to start a bootstrap session. diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSessionManager.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSessionManager.java index f82d2b991f..64c0980985 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSessionManager.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/BootstrapSessionManager.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapRequest; import org.eclipse.leshan.core.request.DownlinkBootstrapRequest; @@ -102,11 +101,11 @@ public String toString() { * * @return a BootstrapSession, possibly authorized. */ - public BootstrapSession begin(BootstrapRequest request, LwM2mPeer client, URI endpointUsed); + public BootstrapSession begin(BootstrapRequest request, LwM2mPeer client, EndpointUri endpointUsed); /** - * Generally called after {@link #begin(BootstrapRequest, LwM2mPeer, URI)} to know if there is something to do on - * this device. + * Generally called after {@link #begin(BootstrapRequest, LwM2mPeer, EndpointUri)} to know if there is something to + * do on this device. * * @param bsSession the bootstrap session concerned. * @return true if there is a bootstrap requests to send for this client. diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapHandler.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapHandler.java index 4d5bd2c487..a9bed0889f 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapHandler.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapHandler.java @@ -22,11 +22,11 @@ import static org.eclipse.leshan.bsserver.BootstrapFailureCause.REQUEST_FAILED; import static org.eclipse.leshan.bsserver.BootstrapFailureCause.UNAUTHORIZED; -import java.net.URI; import java.util.concurrent.ConcurrentHashMap; import org.eclipse.leshan.bsserver.BootstrapSessionManager.BootstrapPolicy; import org.eclipse.leshan.bsserver.request.BootstrapDownlinkRequestSender; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapFinishRequest; import org.eclipse.leshan.core.request.BootstrapRequest; @@ -82,7 +82,8 @@ public DefaultBootstrapHandler(BootstrapDownlinkRequestSender sender, BootstrapS } @Override - public SendableResponse bootstrap(LwM2mPeer sender, BootstrapRequest request, URI endpointUsed) { + public SendableResponse bootstrap(LwM2mPeer sender, BootstrapRequest request, + EndpointUri endpointUsed) { String endpoint = request.getEndpointName(); // Start session, checking the BS credentials diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSession.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSession.java index 887fc53e83..405ba0cd50 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSession.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSession.java @@ -15,12 +15,12 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; -import java.net.URI; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.model.LwM2mModel; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapRequest; @@ -42,7 +42,7 @@ public class DefaultBootstrapSession implements BootstrapSession { private final ContentFormat contentFormat; private final Map applicationData; private final long creationTime; - private final URI endpointUsed; + private final EndpointUri endpointUsed; private final BootstrapRequest request; private volatile LwM2mModel model; @@ -62,7 +62,7 @@ public class DefaultBootstrapSession implements BootstrapSession { * @param applicationData Data that could be attached to a session. */ public DefaultBootstrapSession(BootstrapRequest request, LwM2mPeer client, boolean authorized, - Map applicationData, URI endpointUsed) { + Map applicationData, EndpointUri endpointUsed) { this(request, client, authorized, null, applicationData, endpointUsed); } @@ -76,7 +76,7 @@ public DefaultBootstrapSession(BootstrapRequest request, LwM2mPeer client, boole * @param applicationData Data that could be attached to a session. */ public DefaultBootstrapSession(BootstrapRequest request, LwM2mPeer client, boolean authorized, - ContentFormat contentFormat, Map applicationData, URI endpointUsed) { + ContentFormat contentFormat, Map applicationData, EndpointUri endpointUsed) { this(request, client, authorized, contentFormat, applicationData, System.currentTimeMillis(), endpointUsed); } @@ -91,7 +91,8 @@ public DefaultBootstrapSession(BootstrapRequest request, LwM2mPeer client, boole * @param creationTime The creation time of this session in ms. */ public DefaultBootstrapSession(BootstrapRequest request, LwM2mPeer client, boolean authorized, - ContentFormat contentFormat, Map applicationData, long creationTime, URI endpointUsed) { + ContentFormat contentFormat, Map applicationData, long creationTime, + EndpointUri endpointUsed) { Validate.notNull(request); this.id = RandomStringUtils.random(10, true, true); this.request = request; @@ -132,7 +133,7 @@ public LwM2mPeer getClientTransportData() { } @Override - public URI getEndpointUsed() { + public EndpointUri getEndpointUsed() { return endpointUsed; } diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSessionManager.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSessionManager.java index 8a09a48eae..26fbb66fa7 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSessionManager.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/DefaultBootstrapSessionManager.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; -import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -24,6 +23,7 @@ import org.eclipse.leshan.bsserver.model.StandardBootstrapModelProvider; import org.eclipse.leshan.bsserver.security.BootstrapAuthorizer; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStore; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapFinishRequest; import org.eclipse.leshan.core.request.BootstrapRequest; @@ -74,7 +74,7 @@ public DefaultBootstrapSessionManager(BootstrapTaskProvider tasksProvider, } @Override - public BootstrapSession begin(BootstrapRequest request, LwM2mPeer client, URI endpointUsed) { + public BootstrapSession begin(BootstrapRequest request, LwM2mPeer client, EndpointUri endpointUsed) { Authorization authorization = authorizer.isAuthorized(request, client); DefaultBootstrapSession session = new DefaultBootstrapSession(request, client, authorization.isApproved(), authorization.getApplicationData(), endpointUsed); diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServer.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServer.java index 4db90db73a..d89b53d6ff 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServer.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServer.java @@ -32,6 +32,7 @@ import org.eclipse.leshan.core.Destroyable; import org.eclipse.leshan.core.Startable; import org.eclipse.leshan.core.Stoppable; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; @@ -67,8 +68,8 @@ public class LeshanBootstrapServer { */ public LeshanBootstrapServer(LwM2mBootstrapServerEndpointsProvider endpointsProvider, BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mEncoder encoder, - LwM2mDecoder decoder, LwM2mLinkParser linkParser, BootstrapSecurityStore securityStore, - ServerSecurityInfo serverSecurityInfo) { + LwM2mDecoder decoder, LwM2mLinkParser linkParser, EndPointUriHandler uriHandler, + BootstrapSecurityStore securityStore, ServerSecurityInfo serverSecurityInfo) { Validate.notNull(endpointsProvider, "endpoints provider must not be null"); Validate.notNull(bsSessionManager, "session manager must not be null"); @@ -80,7 +81,8 @@ public LeshanBootstrapServer(LwM2mBootstrapServerEndpointsProvider endpointsProv requestSender = createRequestSender(endpointsProvider); // create endpoints - BootstrapServerEndpointToolbox toolbox = new BootstrapServerEndpointToolbox(decoder, encoder, linkParser); + BootstrapServerEndpointToolbox toolbox = new BootstrapServerEndpointToolbox(decoder, encoder, linkParser, + uriHandler); BootstrapHandler bootstrapHandler = bsHandlerFactory.create(requestSender, bsSessionManager, dispatcher); BootstrapUplinkRequestReceiver requestReceiver = createRequestReceiver(bootstrapHandler); endpointsProvider.createEndpoints(requestReceiver, toolbox, serverSecurityInfo, this); diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServerBuilder.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServerBuilder.java index 9c53f590fb..58e56f20e6 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServerBuilder.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/LeshanBootstrapServerBuilder.java @@ -31,6 +31,8 @@ import org.eclipse.leshan.bsserver.request.BootstrapDownlinkRequestSender; import org.eclipse.leshan.bsserver.security.BootstrapAuthorizer; import org.eclipse.leshan.bsserver.security.BootstrapSecurityStore; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.lwm2m.DefaultLwM2mLinkParser; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.LwM2mNode; @@ -71,6 +73,7 @@ public class LeshanBootstrapServerBuilder { private Certificate[] trustedCertificates; private LwM2mLinkParser linkParser; + private EndPointUriHandler uriHandler; private LwM2mBootstrapServerEndpointsProvider endpointsProvider; @@ -248,6 +251,16 @@ public LeshanBootstrapServerBuilder setLinkParser(LwM2mLinkParser linkParser) { return this; } + /** + * Set the Uri Handler {@link EndPointUriHandler} + *

+ * By default the {@link DefaultEndPointUriHandler} is used. + */ + public LeshanBootstrapServerBuilder setEndpointUriHandler(EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + return this; + } + /** * Set the Bootstrap authorizer {@link BootstrapAuthorizer} *

@@ -310,6 +323,9 @@ public BootstrapHandler create(BootstrapDownlinkRequestSender sender, decoder = new DefaultLwM2mDecoder(); if (linkParser == null) linkParser = new DefaultLwM2mLinkParser(); + if (uriHandler == null) { + uriHandler = new DefaultEndPointUriHandler(); + } // Handle class depending of Session Manager if (sessionManager == null) { @@ -353,7 +369,7 @@ public BootstrapHandler create(BootstrapDownlinkRequestSender sender, } } return createBootstrapServer(endpointsProvider, sessionManager, bootstrapHandlerFactory, encoder, decoder, - linkParser, securityStore, + linkParser, uriHandler, securityStore, new ServerSecurityInfo(privateKey, publicKey, certificateChain, trustedCertificates)); } @@ -372,9 +388,9 @@ public BootstrapHandler create(BootstrapDownlinkRequestSender sender, */ protected LeshanBootstrapServer createBootstrapServer(LwM2mBootstrapServerEndpointsProvider endpointsProvider, BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mEncoder encoder, - LwM2mDecoder decoder, LwM2mLinkParser linkParser, BootstrapSecurityStore securityStore, - ServerSecurityInfo serverSecurityInfo) { + LwM2mDecoder decoder, LwM2mLinkParser linkParser, EndPointUriHandler uriHandler, + BootstrapSecurityStore securityStore, ServerSecurityInfo serverSecurityInfo) { return new LeshanBootstrapServer(endpointsProvider, bsSessionManager, bsHandlerFactory, encoder, decoder, - linkParser, securityStore, serverSecurityInfo); + linkParser, uriHandler, securityStore, serverSecurityInfo); } } diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/BootstrapServerEndpointToolbox.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/BootstrapServerEndpointToolbox.java index 65c28cd763..6b9efc7677 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/BootstrapServerEndpointToolbox.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/BootstrapServerEndpointToolbox.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.endpoint; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; import org.eclipse.leshan.core.node.codec.LwM2mEncoder; @@ -24,11 +25,14 @@ public class BootstrapServerEndpointToolbox { private final LwM2mDecoder decoder; private final LwM2mEncoder encoder; private final LwM2mLinkParser linkParser; + private final EndPointUriHandler uriHandler; - public BootstrapServerEndpointToolbox(LwM2mDecoder decoder, LwM2mEncoder encoder, LwM2mLinkParser linkParser) { + public BootstrapServerEndpointToolbox(LwM2mDecoder decoder, LwM2mEncoder encoder, LwM2mLinkParser linkParser, + EndPointUriHandler uriHandler) { this.decoder = decoder; this.encoder = encoder; this.linkParser = linkParser; + this.uriHandler = uriHandler; } public LwM2mDecoder getDecoder() { @@ -42,4 +46,8 @@ public LwM2mEncoder getEncoder() { public LwM2mLinkParser getLinkParser() { return linkParser; } + + public EndPointUriHandler getUriHandler() { + return uriHandler; + } } diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/DefaultCompositeBootstrapServerEndpointsProvider.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/DefaultCompositeBootstrapServerEndpointsProvider.java index b6664243b8..cc92d02b8f 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/DefaultCompositeBootstrapServerEndpointsProvider.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/DefaultCompositeBootstrapServerEndpointsProvider.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.endpoint; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -24,6 +23,7 @@ import org.eclipse.leshan.bsserver.LeshanBootstrapServer; import org.eclipse.leshan.bsserver.request.BootstrapUplinkRequestReceiver; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.util.Validate; import org.eclipse.leshan.servers.security.ServerSecurityInfo; @@ -56,7 +56,7 @@ public List getEndpoints() { } @Override - public LwM2mBootstrapServerEndpoint getEndpoint(URI uri) { + public LwM2mBootstrapServerEndpoint getEndpoint(EndpointUri uri) { for (LwM2mBootstrapServerEndpointsProvider provider : providers) { LwM2mBootstrapServerEndpoint endpoint = provider.getEndpoint(uri); if (endpoint != null) { diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpoint.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpoint.java index 511311fa21..862cd9ff35 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpoint.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpoint.java @@ -15,9 +15,8 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.endpoint; -import java.net.URI; - import org.eclipse.leshan.bsserver.BootstrapSession; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.DownlinkBootstrapRequest; import org.eclipse.leshan.core.response.ErrorCallback; @@ -28,7 +27,7 @@ public interface LwM2mBootstrapServerEndpoint { Protocol getProtocol(); - URI getURI(); + EndpointUri getURI(); String getDescription(); diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpointsProvider.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpointsProvider.java index d2887a5cae..7b1feb6850 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpointsProvider.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/endpoint/LwM2mBootstrapServerEndpointsProvider.java @@ -15,18 +15,18 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.endpoint; -import java.net.URI; import java.util.List; import org.eclipse.leshan.bsserver.LeshanBootstrapServer; import org.eclipse.leshan.bsserver.request.BootstrapUplinkRequestReceiver; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.servers.security.ServerSecurityInfo; public interface LwM2mBootstrapServerEndpointsProvider { List getEndpoints(); - LwM2mBootstrapServerEndpoint getEndpoint(URI uri); + LwM2mBootstrapServerEndpoint getEndpoint(EndpointUri uri); void createEndpoints(BootstrapUplinkRequestReceiver requestReceiver, BootstrapServerEndpointToolbox toolbox, ServerSecurityInfo serverSecurityInfo, LeshanBootstrapServer server); diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/BootstrapUplinkRequestReceiver.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/BootstrapUplinkRequestReceiver.java index 11e92c6d6e..1abb388278 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/BootstrapUplinkRequestReceiver.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/BootstrapUplinkRequestReceiver.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.request; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.UplinkBootstrapRequest; import org.eclipse.leshan.core.request.UplinkRequest; @@ -26,8 +25,8 @@ public interface BootstrapUplinkRequestReceiver { SendableResponse requestReceived(LwM2mPeer sender, UplinkBootstrapRequest request, - URI serverEndpointUri); + EndpointUri serverEndpointUri); void onError(LwM2mPeer sender, Exception exception, - Class> requestType, URI serverEndpointUri); + Class> requestType, EndpointUri serverEndpointUri); } diff --git a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/DefaultBootstrapUplinkRequestReceiver.java b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/DefaultBootstrapUplinkRequestReceiver.java index 05681b71fc..a180e5202a 100644 --- a/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/DefaultBootstrapUplinkRequestReceiver.java +++ b/leshan-lwm2m-bsserver/src/main/java/org/eclipse/leshan/bsserver/request/DefaultBootstrapUplinkRequestReceiver.java @@ -15,9 +15,8 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver.request; -import java.net.URI; - import org.eclipse.leshan.bsserver.BootstrapHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.BootstrapRequest; import org.eclipse.leshan.core.request.UplinkBootstrapRequest; @@ -36,12 +35,12 @@ public DefaultBootstrapUplinkRequestReceiver(BootstrapHandler bootstapHandler) { @Override public void onError(LwM2mPeer sender, Exception exception, - Class> requestType, URI serverEndpointUri) { + Class> requestType, EndpointUri serverEndpointUri) { } @Override public SendableResponse requestReceived(LwM2mPeer sender, - UplinkBootstrapRequest request, URI serverEndpointUri) { + UplinkBootstrapRequest request, EndpointUri serverEndpointUri) { RequestHandler requestHandler = new RequestHandler(sender, serverEndpointUri); request.accept(requestHandler); @@ -51,10 +50,10 @@ public SendableResponse requestReceived(LwM2mPeer s public class RequestHandler implements UplinkBootstrapRequestVisitor { private final LwM2mPeer sender; - private final URI serverEndpointUri; + private final EndpointUri serverEndpointUri; private SendableResponse response; - public RequestHandler(LwM2mPeer sender, URI serverEndpointUri) { + public RequestHandler(LwM2mPeer sender, EndpointUri serverEndpointUri) { this.sender = sender; this.serverEndpointUri = serverEndpointUri; } diff --git a/leshan-lwm2m-bsserver/src/test/java/org/eclipse/leshan/bsserver/BootstrapHandlerTest.java b/leshan-lwm2m-bsserver/src/test/java/org/eclipse/leshan/bsserver/BootstrapHandlerTest.java index fb402696b2..338e33497f 100644 --- a/leshan-lwm2m-bsserver/src/test/java/org/eclipse/leshan/bsserver/BootstrapHandlerTest.java +++ b/leshan-lwm2m-bsserver/src/test/java/org/eclipse/leshan/bsserver/BootstrapHandlerTest.java @@ -15,13 +15,13 @@ *******************************************************************************/ package org.eclipse.leshan.bsserver; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.InetSocketAddress; -import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -29,7 +29,7 @@ import org.eclipse.leshan.bsserver.request.BootstrapDownlinkRequestSender; import org.eclipse.leshan.core.ResponseCode; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.peer.PskIdentity; @@ -55,7 +55,7 @@ public enum Mode { ALWAYS_SUCCESS, ALWAYS_FAILURE, NO_RESPONSE }; - private final URI endpointUsed = EndpointUriUtil.createUri("coap://localhost:5683"); + private final EndpointUri endpointUsed = uriHandler.createUri("coap://localhost:5683"); @Test public void error_if_not_authorized() { @@ -242,7 +242,7 @@ public MockBootstrapSessionManager(boolean authorized, BootstrapConfigStore stor } @Override - public BootstrapSession begin(BootstrapRequest request, LwM2mPeer sender, URI endpointUsed) { + public BootstrapSession begin(BootstrapRequest request, LwM2mPeer sender, EndpointUri endpointUsed) { lastSession = new DefaultBootstrapSession(request, sender, authorized, null, endpointUsed); return lastSession; } diff --git a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClient.java b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClient.java index b39cd53a6f..9be3929339 100644 --- a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClient.java +++ b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClient.java @@ -54,6 +54,7 @@ import org.eclipse.leshan.client.send.SendService; import org.eclipse.leshan.client.servers.LwM2mServer; import org.eclipse.leshan.client.util.LinkFormatHelper; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.LinkSerializer; import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser; import org.eclipse.leshan.core.node.LwM2mNode; @@ -94,7 +95,8 @@ public LeshanClient(String endpoint, List objectEn BootstrapConsistencyChecker checker, Map additionalAttributes, Map bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder, ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper, - LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) { + LwM2mAttributeParser attributeParser, EndPointUriHandler uriHandler, + LwM2mClientEndpointsProvider endpointsProvider) { Validate.notNull(endpoint); Validate.notEmpty(objectEnablers); @@ -113,7 +115,7 @@ public LeshanClient(String endpoint, List objectEn bootstrapHandler = createBoostrapHandler(objectTree, checker, linkFormatHelper); ClientEndpointToolbox toolbox = new ClientEndpointToolbox(decoder, encoder, linkSerializer, - objectTree.getModel(), attributeParser); + objectTree.getModel(), attributeParser, uriHandler); endpointsManager = createEndpointsManager(this.endpointsProvider, toolbox, trustStore); requestSender = createRequestSender(this.endpointsProvider); dataSenderManager = createDataSenderManager(dataSenders, rootEnabler, requestSender); diff --git a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClientBuilder.java b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClientBuilder.java index ca124876fd..e3588aec31 100644 --- a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClientBuilder.java +++ b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/LeshanClientBuilder.java @@ -41,6 +41,8 @@ import org.eclipse.leshan.client.util.LinkFormatHelper; import org.eclipse.leshan.core.LwM2m.LwM2mVersion; import org.eclipse.leshan.core.LwM2mId; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.DefaultLinkSerializer; import org.eclipse.leshan.core.link.LinkSerializer; @@ -86,6 +88,8 @@ public class LeshanClientBuilder { private LinkFormatHelper linkFormatHelper; + private EndPointUriHandler uriHandler; + /** * Creates a new instance for setting the configuration options for a {@link LeshanClient} instance. * @@ -193,6 +197,16 @@ public LeshanClientBuilder setAttributeParser(LwM2mAttributeParser attributePars return this; } + /** + * Set the Uri Handler {@link EndPointUriHandler} + *

+ * By default the {@link DefaultEndPointUriHandler} is used. + */ + public LeshanClientBuilder setEndpointUriHandler(EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + return this; + } + /** * Set optional trust store for verifying X.509 server certificates. * @@ -328,10 +342,13 @@ public LeshanClient build() { if (bootstrapConsistencyChecker == null) { bootstrapConsistencyChecker = new DefaultBootstrapConsistencyChecker(); } + if (uriHandler == null) { + uriHandler = new DefaultEndPointUriHandler(); + } return createLeshanClient(endpoint, objectEnablers, dataSenders, this.trustStore, engineFactory, bootstrapConsistencyChecker, additionalAttributes, bsAdditionalAttributes, encoder, decoder, executor, - linkSerializer, linkFormatHelper, attributeParser, endpointsProvider); + linkSerializer, linkFormatHelper, attributeParser, uriHandler, endpointsProvider); } /** @@ -364,9 +381,10 @@ protected LeshanClient createLeshanClient(String endpoint, List additionalAttributes, Map bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder, ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper, - LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) { + LwM2mAttributeParser attributeParser, EndPointUriHandler uriHandler, + LwM2mClientEndpointsProvider endpointsProvider) { return new LeshanClient(endpoint, objectEnablers, dataSenders, trustStore, engineFactory, checker, additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor, linkSerializer, - linkFormatHelper, attributeParser, endpointsProvider); + linkFormatHelper, attributeParser, uriHandler, endpointsProvider); } } diff --git a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/ClientEndpointToolbox.java b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/ClientEndpointToolbox.java index 4c31fd581d..03ac92411e 100644 --- a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/ClientEndpointToolbox.java +++ b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/ClientEndpointToolbox.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.client.endpoint; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.LinkSerializer; import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser; import org.eclipse.leshan.core.model.LwM2mModel; @@ -28,14 +29,16 @@ public class ClientEndpointToolbox { private final LinkSerializer linkSerializer; private final LwM2mModel model; private final LwM2mAttributeParser attributeParser; + private final EndPointUriHandler uriHandler; public ClientEndpointToolbox(LwM2mDecoder decoder, LwM2mEncoder encoder, LinkSerializer linkSerializer, - LwM2mModel model, LwM2mAttributeParser attributeParser) { + LwM2mModel model, LwM2mAttributeParser attributeParser, EndPointUriHandler uriHandler) { this.decoder = decoder; this.encoder = encoder; this.linkSerializer = linkSerializer; this.model = model; this.attributeParser = attributeParser; + this.uriHandler = uriHandler; } public LwM2mDecoder getDecoder() { @@ -57,4 +60,8 @@ public LwM2mModel getModel() { public LwM2mAttributeParser getAttributeParser() { return attributeParser; } + + public EndPointUriHandler getUriHandler() { + return uriHandler; + } } diff --git a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/LwM2mClientEndpoint.java b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/LwM2mClientEndpoint.java index 72649692cd..a6a48a6742 100644 --- a/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/LwM2mClientEndpoint.java +++ b/leshan-lwm2m-client/src/main/java/org/eclipse/leshan/client/endpoint/LwM2mClientEndpoint.java @@ -15,9 +15,8 @@ *******************************************************************************/ package org.eclipse.leshan.client.endpoint; -import java.net.URI; - import org.eclipse.leshan.client.servers.LwM2mServer; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.UplinkRequest; import org.eclipse.leshan.core.response.ErrorCallback; @@ -28,7 +27,7 @@ public interface LwM2mClientEndpoint { Protocol getProtocol(); - URI getURI(); + EndpointUri getURI(); String getDescription(); diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriHandler.java similarity index 54% rename from leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java rename to leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriHandler.java index 791ce50561..1ae63d69dd 100644 --- a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUriUtil.java +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2022 Sierra Wireless and others. + * Copyright (c) 2024 Sierra Wireless and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -19,63 +19,67 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URI; -import java.net.URISyntaxException; import org.eclipse.leshan.core.util.Validate; -public class EndpointUriUtil { +public class DefaultEndPointUriHandler implements EndPointUriHandler { - public static URI createUri(String scheme, String host, int port) { - try { - return new URI(scheme, null, host, port, null, null, null); - } catch (URISyntaxException e) { - throw new IllegalStateException(e); - } + private final EndPointUriParser parser; + + public DefaultEndPointUriHandler() { + this(new DefaultEndPointUriParser()); } - public static URI createUri(String scheme, InetSocketAddress addr) { - try { - return new URI(scheme, null, toUriHostName(addr), addr.getPort(), null, null, null); - } catch (URISyntaxException e) { - throw new IllegalStateException(e); - } + public DefaultEndPointUriHandler(EndPointUriParser parser) { + this.parser = parser; + } + + @Override + public EndPointUriParser getParser() { + return parser; } - public static URI createUri(String uri) { + @Override + public EndpointUri createUri(String scheme, InetSocketAddress addr) { + return new EndpointUri(scheme, toUriHostName(addr), addr.getPort()); + } + + @Override + public EndpointUri createUri(String uri) { + return parser.parse(uri); + } + + @Override + public EndpointUri createUri(URI uri) { try { - return new URI(uri); - } catch (URISyntaxException e) { + return new EndpointUri(uri.getScheme(), uri.getHost(), uri.getPort()); + } catch (InvalidEndpointUriException e) { throw new IllegalStateException(e); } } - public static URI replaceAddress(URI originalUri, InetSocketAddress newAddress) { + @Override + public EndpointUri replaceAddress(EndpointUri originalUri, InetSocketAddress newAddress) { try { - return new URI(originalUri.getScheme(), null, toUriHostName(newAddress), newAddress.getPort(), null, null, - null); - } catch (URISyntaxException e) { + return new EndpointUri(originalUri.getScheme(), toUriHostName(newAddress), newAddress.getPort()); + } catch (InvalidEndpointUriException e) { throw new IllegalStateException(e); } } - public static InetSocketAddress getSocketAddr(URI uri) { + @Override + public InetSocketAddress getSocketAddr(EndpointUri uri) { return new InetSocketAddress(uri.getHost(), uri.getPort()); } - public static void validateURI(URI uri) throws IllegalArgumentException { - Validate.notNull(uri); - - if (uri.getScheme() == null) { - throw new IllegalArgumentException(String.format("Invalid URI[%s]: Scheme MUST NOT be null", uri)); - } - - if (uri.getHost() == null) { - throw new IllegalArgumentException(String.format("Invalid URI[%s]: Host MUST NOT be null", uri)); - } - - if (uri.getPort() == -1) { - throw new IllegalArgumentException(String.format("Invalid URI[%s]: Post MUST NOT be undefined", uri)); + @Override + public void validateURI(EndpointUri uri) throws InvalidEndpointUriException { + if (uri == null) { + throw new InvalidEndpointUriException("uri must not be null"); } + parser.validateScheme(uri.getScheme()); + parser.validateHost(uri.getHost()); + parser.validatePort(uri.getPort()); } /** @@ -84,7 +88,7 @@ public static void validateURI(URI uri) throws IllegalArgumentException { * Following https://www.rfc-editor.org/rfc/rfc6874#section-2, zone id (also called scope id) in URI should be * prefixed by %25 */ - private static String toUriHostName(InetSocketAddress socketAddr) { + protected static String toUriHostName(InetSocketAddress socketAddr) { if (socketAddr == null) { Validate.notNull(socketAddr); } @@ -101,6 +105,7 @@ private static String toUriHostName(InetSocketAddress socketAddr) { host = hostAddress + separator + scope; } } + host = '[' + host + ']'; } return host; } diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParser.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParser.java new file mode 100644 index 0000000000..d3a89424ba --- /dev/null +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParser.java @@ -0,0 +1,490 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +import org.eclipse.leshan.core.parser.StringParser; + +public class DefaultEndPointUriParser implements EndPointUriParser { + + /** + * This aims to parse {@link EndpointUri} following this simplified grammar derived from RFC3986 : + * + *

+     * URI = scheme "://"  host [ ":" port ]
+     * 
+ * + * @see RFC3986 - Uniform Resource Identifier (URI): + * Generic Syntax + */ + @Override + public EndpointUri parse(String uri) throws InvalidEndpointUriException { + // manage null/empty case + if (uri == null || uri.length() == 0) { + throw new InvalidEndpointUriException("URI can not be null or empty"); + } + + // create a String Parser + StringParser parser = new StringParser(uri) { + @Override + public void raiseException(String message, Exception cause) throws InvalidEndpointUriException { + throw new InvalidEndpointUriException(cause, "Invalid uri [%s] : %s", getStringToParse(), message); + } + }; + + String scheme = consumeScheme(parser); + parser.consumeChar(':'); + parser.consumeChar('/'); + parser.consumeChar('/'); + String host = consumeHost(parser); + Integer port = null; + if (parser.nextCharIs(':')) { + parser.consumeNextChar(); + port = consumePort(parser); + } + + if (parser.hasMoreChar()) { + parser.raiseException("unexpected characters after [%s]", parser.getAlreadyParsedString()); + } + return new EndpointUri(scheme, host, port); + } + + /** + * This aims to parse SCHEME from URI following RFC3986 + * + *
+     * scheme = ALPHA * (ALPHA / DIGIT / "+" / "-" / ".")
+     * 
+ * + * @see RFC3986 - 3.1. Scheme + */ + protected String consumeScheme(StringParser parser) { + int start = parser.getPosition(); + + parser.consumeALPHA(); + while (parser.nextCharIsALPHA() || parser.nextCharIsDIGIT() || parser.nextCharIsIn("+-.")) { + parser.consumeNextChar(); + } + + return parser.substring(start, parser.getPosition()); + } + + /** + * This aims to parse HOST from URI following RFC3986 + * + *
+     * host = IPliteral / IPv4address / regname
+     *
+     * IPliteral = "[" ( IPv6address / IPvFuture  ) "]"
+     * 
+ * + * @see RFC3986 - 3.2.2. Host + */ + protected String consumeHost(StringParser parser) { + // if start by [ consume IpLiteral + if (parser.nextCharIs('[')) { + return consumeIPliteral(parser); + } + // The code below is not needed as RegName include ipv4 grammar. + +// // else try to consume Ipv4 +// String host = tryToConsumeIPv4address(parser); +// if (host != null) { +// return host; +// } + // else consume regname + return consumeRegName(parser); + } + + /** + * This aims to parse IPv6address from URI following RFC3986 + * + *
+     *  IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture  ) "]"
+     *
+     *  IPv6addrz = IPv6address "%25" ZoneID
+     * 
+ * + * @see RFC3986 - 3.2.2. Host + */ + protected String consumeIPliteral(StringParser parser) { + int start = parser.getPosition(); + parser.consumeChar('['); + consumeIPv6address(parser); + if (parser.nextCharIs('%')) { + parser.consumeNextChar(); + parser.consumeChar('2'); + parser.consumeChar('5'); + consumeZoneID(parser); + } + parser.consumeChar(']'); + return parser.substring(start, parser.getPosition()); + } + + /** + * This aims to parse IPv6address from URI following RFC3986 + * + *
+     *  IPv6address =                           6( h16 ":" ) ls32
+     *             /                       "::" 5( h16 ":" ) ls32
+     *             / [               h16 ] "::" 4( h16 ":" ) ls32
+     *             / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
+     *             / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
+     *             / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32
+     *             / [ *4( h16 ":" ) h16 ] "::"              ls32
+     *             / [ *5( h16 ":" ) h16 ] "::"              h16
+     *             / [ *6( h16 ":" ) h16 ] "::"
+     *
+     *   ls32        = ( h16 ":" h16 ) / IPv4address
+     *               ; least-significant 32 bits of address
+     *
+     *   h16         = 1*4HEXDIG
+     *               ; 16 bits of address represented in hexadecimal
+     * 
+ * + * @see RFC3986 - 3.2.2. Host + */ + protected String consumeIPv6address(StringParser parser) { + int start = parser.getPosition(); + + // A 128-bit IPv6 address is divided into eight 16-bit pieces. + int nb16bitsParsed = 0; + // "::" can be used only 1 time to represent 1 or many 16-bits pieces with 0 value. + boolean elipsedConsumed = false; + + // consume first 16-bit pieces. + if (parser.nextCharIsUPLOHEXDIG()) { + // parse 16-bit pieces (h16= 1*4HEXDIG) + int nbHexDig = 0; + do { + parser.consumeNextChar(); + nbHexDig++; + } while (parser.nextCharIsUPLOHEXDIG() && nbHexDig <= 4); + nb16bitsParsed++; + } + while (parser.nextCharIs(':') && (elipsedConsumed ? nb16bitsParsed < 7 : nb16bitsParsed < 8)) { + parser.consumeNextChar(); + // consume next 16-bit pieces. + if (parser.nextCharIsUPLOHEXDIG()) { + // parse 16-bit pieces (h16= 1*4HEXDIG) + int beforeHex = parser.getPosition(); + int nbHexDig = 0; + do { + parser.consumeNextChar(); + nbHexDig++; + } while (parser.nextCharIsUPLOHEXDIG() && nbHexDig <= 4); + + if (parser.nextCharIs('.')) { + // oops we start to parse ipv4 address so rollback. + parser.backtrackTo(beforeHex); + consumeIPv4address(parser); + nb16bitsParsed++; + nb16bitsParsed++; + } else { + nb16bitsParsed++; + } + } else if (!elipsedConsumed && parser.nextCharIs(':')) { + // parse eclipse ('::') + // only 1 elipse allowed + elipsedConsumed = true; + } + } + String ipv6Addr = parser.substring(start, parser.getPosition()); + if (elipsedConsumed ? nb16bitsParsed > 7 : nb16bitsParsed > 8) { + parser.raiseException("Invalid IPv6 address : %s is too long", ipv6Addr); + } + return ipv6Addr; + } + + /** + * This aims to parse zoneID from URI following RFC6874 + * + *
+     * ZoneID = 1 * (unreserved / pctencoded)
+     * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
+     * pct-encoded   = "%" HEXDIG HEXDIG
+     * 
+ * + * @see RFC6874 - 2. Specification + * @see RFC3986 - 2.3. Unreserved Characters + * @see RFC3986 - 2.1. Percent-Encoding + */ + private void consumeZoneID(StringParser parser) { + while (true) { + if (parser.nextCharIs('%')) { + // percent encoding + parser.consumeNextChar(); + parser.consumeHEXDIG(); + parser.consumeHEXDIG(); + } else if (parser.nextCharIsALPHA() || parser.nextCharIsDIGIT() || parser.nextCharIsIn("-._~")) { + // unreserved + parser.consumeNextChar(); + } else { + return; + } + } + } + + /** + * This aims to parse IPv6address from URI following RFC3986 + * + *
+     * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
+     *
+     * dec-octet   = DIGIT                 ; 0-9
+     *             / %x31-39 DIGIT         ; 10-99
+     *             / "1" 2DIGIT            ; 100-199
+     *             / "2" %x30-34 DIGIT     ; 200-249
+     *             / "25" %x30-35          ; 250-255
+     * 
+ * + * @see RFC3986 - 3.2.2. Host + */ + protected String tryToConsumeIPv4address(StringParser parser) { + int start = parser.getPosition(); + try { + return consumeIPv4address(parser); + } catch (InvalidEndpointUriException e) { + parser.backtrackTo(start); + return null; + } + } + + /** + * This aims to parse IPv6address from URI following RFC3986 + * + *
+     * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
+     *
+     * 
+ * + * @see RFC3986 - 3.2.2. Host + */ + protected String consumeIPv4address(StringParser parser) { + int start = parser.getPosition(); + + parser.consumeDecOctet(); + parser.consumeChar('.'); + parser.consumeDecOctet(); + parser.consumeChar('.'); + parser.consumeDecOctet(); + parser.consumeChar('.'); + parser.consumeDecOctet(); + + return parser.substring(start, parser.getPosition()); + } + + /** + * This aims to parse regName from URI following RFC3986 but for now we limit parsing to HostName from DNS. + * + *
+     * domain ::= subdomain
+     * subdomain ::= label | subdomain "." label
+     * 
+ * + * @see RFC1034 - 3.2.2. Host + * @see RFC1034 - 3.5. Preferred name syntax + * @see RFC1123 - 2. GENERAL ISSUES + * @see Host Naming Rules and RFC Documents + */ + protected String consumeRegName(StringParser parser) { + int start = parser.getPosition(); + + // consume domain + consumeLabel(parser); + while (parser.nextCharIs('.')) { + parser.consumeNextChar(); + consumeLabel(parser); + } + + // return domain value as regname + int end = parser.getPosition(); + if (end > start) { + return parser.substring(start, end); + } else { + return null; + } + } + + /** + * This aims to consume label from HostName from DNS. + * + *
+     * label ::= let-dig [ [ ldh-str ] let-dig ]
+     * ldh-str ::= let-dig-hyp | let-dig-hyp ldh-str
+     * let-dig-hyp ::= let-dig | "-"
+     * let-dig ::= letter | digit
+     * letter ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
+     * digit ::= any one of the ten digits 0 through 9
+     * 
+ * + * @see RFC1034 - 3.2.2. Host + * @see RFC1034 - 3.5. Preferred name syntax + * @see RFC1123 - 2. GENERAL ISSUES + * @see Host Naming Rules and RFC Documents + */ + protected void consumeLabel(StringParser parser) { + // consume first char (letter or digit) + if (parser.nextCharIsALPHA() || parser.nextCharIsDIGIT()) { + parser.consumeNextChar(); + } else { + return; + } + while (true) { + // hyphen ('-') + if (parser.nextCharIs('-')) { + int beforeHyp = parser.getPosition(); + // consume all '-' + do { + parser.consumeNextChar(); // consume '-' + } while (parser.nextCharIs('-')); + + if (parser.nextCharIsALPHA() || parser.nextCharIsDIGIT()) { + // '-' should be followed by letter or digit + parser.consumeNextChar(); + } else { + // if next char is not letter or digit, label ends before the first '-' + parser.backtrackTo(beforeHyp); + return; + } + } else + // letter or digit + if (parser.nextCharIsALPHA() || parser.nextCharIsDIGIT()) { + parser.consumeNextChar(); + } else { + // we fing the end of label + return; + } + } + } + + /** + * This aims to parse PORT from URI following RFC3986 + * + * Official Grammar is : + * + *
+     * port        = *DIGIT
+     * 
+ * + * But It seems CARDINAL makes more sense : + * + *
+     * port        = cardinal
+     * cardinal       = "0" / ( %x31-39 *DIGIT )
+     * 
+ * + * @see RFC3986 - 3.2.3. Port + */ + protected Integer consumePort(StringParser parser) { + String portStr = parser.consumeCardinal(); + try { + Integer port = new Integer(portStr); + validatePort(port, parser); + return port; + } catch (NumberFormatException e) { + parser.raiseException(e, "port %s is not a Number", portStr); + return null; + } + } + + @Override + public void validateScheme(String scheme) { + // manage null/empty case + if (scheme == null || scheme.length() == 0) { + throw new InvalidEndpointUriException("scheme must not be null"); + } + + // create a String Parser + StringParser parser = new StringParser(scheme) { + @Override + public void raiseException(String message, Exception cause) throws InvalidEndpointUriException { + throw new InvalidEndpointUriException(cause, "Invalid scheme [%s] : %s", getStringToParse(), message); + } + }; + + consumeScheme(parser); + if (parser.hasMoreChar()) { + parser.raiseException("unexpected characters after [%s]", parser.getAlreadyParsedString()); + } + } + + @Override + public void validateHost(String host) { + // manage null/empty case + if (host == null || host.length() == 0) { + throw new InvalidEndpointUriException("host must not be null"); + } + + // create a String Parser + StringParser parser = new StringParser(host) { + @Override + public void raiseException(String message, Exception cause) throws InvalidEndpointUriException { + throw new InvalidEndpointUriException(cause, "Invalid host [%s] : %s", getStringToParse(), message); + } + }; + + consumeHost(parser); + if (parser.hasMoreChar()) { + parser.raiseException("unexpected characters after [%s]", parser.getAlreadyParsedString()); + } + } + + @Override + public void validatePort(String port) { + // manage null/empty case + if (port == null || port.length() == 0) { + throw new InvalidEndpointUriException("scheme must not be null"); + } + + // create a String Parser + StringParser parser = new StringParser(port) { + @Override + public void raiseException(String message, Exception cause) throws InvalidEndpointUriException { + throw new InvalidEndpointUriException(cause, "Invalid port [%s] : %s", getStringToParse(), message); + } + }; + + consumePort(parser); + if (parser.hasMoreChar()) { + parser.raiseException("unexpected characters after [%s]", parser.getAlreadyParsedString()); + } + } + + protected void validatePort(Integer port, StringParser parser) { + // manage null/empty case + if (port == null) { + if (parser != null) { + parser.raiseException("port must not be null"); + } else { + throw new InvalidEndpointUriException("port must not be null"); + } + } + + if (port > 65535 || port < 0) { + if (parser != null) { + parser.raiseException("Invalid port [%d] : port must be an integer between 0 and 65535", port); + } else { + throw new InvalidEndpointUriException("Invalid port [%d] : port must be an integer between 0 and 65535", + port); + } + } + } + + @Override + public void validatePort(Integer port) { + validatePort(port, null); + } +} diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriHandler.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriHandler.java new file mode 100644 index 0000000000..da81915178 --- /dev/null +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriHandler.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +import java.net.InetSocketAddress; +import java.net.URI; + +/** + * Class responsible to handle {@link EndpointUri} + */ +public interface EndPointUriHandler { + + EndPointUriParser getParser(); + + EndpointUri createUri(String scheme, InetSocketAddress addr); + + EndpointUri createUri(String uri); + + EndpointUri createUri(URI uri); + + EndpointUri replaceAddress(EndpointUri originalUri, InetSocketAddress newAddress); + + InetSocketAddress getSocketAddr(EndpointUri uri); + + void validateURI(EndpointUri uri) throws InvalidEndpointUriException; +} diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriParser.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriParser.java new file mode 100644 index 0000000000..dc72b8b4bd --- /dev/null +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndPointUriParser.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +public interface EndPointUriParser { + + EndpointUri parse(String Uri) throws InvalidEndpointUriException; + + void validateScheme(String scheme) throws InvalidEndpointUriException; + + void validateHost(String host) throws InvalidEndpointUriException; + + void validatePort(String port) throws InvalidEndpointUriException; + + void validatePort(Integer port) throws InvalidEndpointUriException; +} diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUri.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUri.java new file mode 100644 index 0000000000..d76e011521 --- /dev/null +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/EndpointUri.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +import java.util.Objects; + +public class EndpointUri { + + private final String scheme; + private final String host; + private final Integer port; + + public EndpointUri(String scheme, String host, Integer port) { + this.scheme = scheme; + this.host = host; + this.port = port; + } + + public String getScheme() { + return scheme; + } + + public String getHost() { + return host; + } + + public Integer getPort() { + return port; + } + + @Override + public final boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof EndpointUri)) + return false; + EndpointUri other = (EndpointUri) obj; + return Objects.equals(host, other.host) && Objects.equals(port, other.port) + && Objects.equals(scheme, other.scheme); + } + + @Override + public final int hashCode() { + return Objects.hash(host, port, scheme); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append(scheme); + builder.append("://"); + builder.append(host); + if (port != null) { + builder.append(":"); + builder.append(port); + } + return builder.toString(); + } +} diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/InvalidEndpointUriException.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/InvalidEndpointUriException.java new file mode 100644 index 0000000000..9484cb5ecd --- /dev/null +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/endpoint/InvalidEndpointUriException.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +public class InvalidEndpointUriException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public InvalidEndpointUriException() { + } + + public InvalidEndpointUriException(String m) { + super(m); + } + + public InvalidEndpointUriException(String m, Object... args) { + super(String.format(m, args)); + } + + public InvalidEndpointUriException(Throwable e) { + super(e); + } + + public InvalidEndpointUriException(String m, Throwable e) { + super(m, e); + } + + public InvalidEndpointUriException(Throwable e, String m, Object... args) { + super(String.format(m, args), e); + } +} diff --git a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/parser/StringParser.java b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/parser/StringParser.java index 108a0fc40d..e7d5b7598a 100644 --- a/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/parser/StringParser.java +++ b/leshan-lwm2m-core/src/main/java/org/eclipse/leshan/core/parser/StringParser.java @@ -120,6 +120,21 @@ public boolean nextCharIsHEXDIG() { return nextCharIsDIGIT() || nextCharIsBetween('A', 'F'); } + /** + * HEXDIG as defined at https://datatracker.ietf.org/doc/html/rfc2234#section-6.1 but case insensitive + * + *
+     * HEXDIG = DIGIT / "a" / "b" / "c" / "d" / "e" / "f" / "A" / "B" / "C" / "D" / "E" / "F"
+     * 
+ * + * + * + * @return true if there is a next char and it is an HEXDIG + */ + public boolean nextCharIsUPLOHEXDIG() { + return nextCharIsDIGIT() || nextCharIsBetween('A', 'F') || nextCharIsBetween('a', 'f'); + } + /** * consume next char. User must check before if there is more char available with {@link #hasMoreChar()} */ @@ -211,6 +226,23 @@ public void consumeLOALPHA() throws T { consumeNextChar(); } + /** + * Consume next char if it is ALPHA argument if not raise an exception. + * + * @see #nextCharIsALPHA() + */ + public void consumeALPHA() throws T { + if (!hasMoreChar()) { + raiseException("Unable to parse [%s] : unexpected EOF, expected 'LOALPHA' character after %s", strToParse, + getAlreadyParsedString()); + } + if (!nextCharIsALPHA()) { + raiseException("Unable to parse [%s] : unexpected character '%s', expected 'ALPHA' after %s", strToParse, + getNextChar(), getAlreadyParsedString()); + } + consumeNextChar(); + } + /** * Consume cardinal as described at https://datatracker.ietf.org/doc/html/rfc6690#section-2 * @@ -236,6 +268,70 @@ public String consumeCardinal() throws T { } } + /** + * Consume cardinal as described at https://datatracker.ietf.org/doc/html/rfc6690#section-2 + * + *
+     *  dec-octet   = DIGIT                 ; 0-9
+     *              / %x31-39 DIGIT         ; 10-99
+     *              / "1" 2DIGIT            ; 100-199
+     *              / "2" %x30-34 DIGIT     ; 200-249
+     *              / "25" %x30-35          ; 250-255
+     * 
+ */ + public String consumeDecOctet() throws T { + int start = getPosition(); + if (nextCharIs('0')) { + // 0 + consumeNextChar(); + } else if (nextCharIsBetween('3', '9')) { + // 3-9 + // 30-99 + consumeNextChar(); + if (nextCharIsDIGIT()) { + // 30-99 + consumeNextChar(); + } + } else if (nextCharIs('1')) { + // 1 + // 10-19 + // 100-199 + consumeNextChar(); + if (nextCharIsDIGIT()) { + // 10-19 + consumeNextChar(); + if (nextCharIsDIGIT()) { + // 100-199 + consumeNextChar(); + } + } + } else if (nextCharIs('2')) { + // case 2?? + consumeNextChar(); + if (nextCharIsBetween('0', '4')) { + // 20-24 + // 200-249 + consumeNextChar(); + if (nextCharIsDIGIT()) { + // 200-249 + consumeNextChar(); + } + } else if (nextCharIs('5')) { + // 25 + // 250-255 + consumeNextChar(); + if (nextCharIsBetween('0', '5')) { + // 250-255 + consumeNextChar(); + } + } else if (nextCharIsBetween('6', '9')) { + // case 26-29 + consumeNextChar(); + } + } + return substring(start, getPosition()); + } + /** * Cancel consumption of last char. */ diff --git a/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParserTest.java b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParserTest.java new file mode 100644 index 0000000000..a3443526eb --- /dev/null +++ b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/DefaultEndPointUriParserTest.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrowsExactly; + +import java.util.stream.Stream; + +import org.eclipse.leshan.core.link.attributes.InvalidAttributeException; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +public class DefaultEndPointUriParserTest { + + private final EndPointUriParser parser = new DefaultEndPointUriParser(); + + private static Stream validAttributes() throws InvalidAttributeException { + return Stream.of(// + Arguments.of("coap", "localhost", null), // + Arguments.of("coaps", "localhost", null), // + Arguments.of("coap+tcp", "localhost", null), // + Arguments.of("coaps+tcp", "localhost", null), // + Arguments.of("coap", "localhost", 3), // + Arguments.of("coap", "domain.org", 80), // + Arguments.of("coap", "domain.org", null), // + Arguments.of("coap", "sub.domain.org", 888), // + Arguments.of("coap", "sub.domain.org", null), // + Arguments.of("coap", "0.0.0.0", 8888), // + Arguments.of("coap", "0.0.0.0", null), // + Arguments.of("coap", "sub-sub-domain.sub----domain.org", null), // + Arguments.of("coap", "15.200.199.255", 8888), // + Arguments.of("coap", "sub.domain.org", null), // + Arguments.of("http", "[2001:db8::192.168.1.1]", 8080), // + Arguments.of("https", "[2001:db8:85a3::8a2e:370:192.168.0.1]", 443), // + Arguments.of("ftp", "[2001:0db8:0000:0042::172.16.0.1]", 21), // + Arguments.of("ws", "[2607:f0d0:1002:51::192.168.0.100]", 9090), // + Arguments.of("wss", "[2001:db8::10.1.1.1]", 8443), // + Arguments.of("smtp", "[2001:4860:4860::8888]", 25), // + Arguments.of("ssh", "[::1]", 22), // + Arguments.of("telnet", "[2001:db8::ff00:42:8329]", 23), // + Arguments.of("telnet", "[2001:db8::ff00:42:8329%25zoneid]", 23), // + Arguments.of("telnet", "[2001:db8::ff00:42:8329%25%AA%FFzoneid]", 23) // + ); + } + + @ParameterizedTest(name = "Tests {index} : {0}://{1}:{2}") + @MethodSource("validAttributes") + public void check_one_valid_uri(String scheme, String host, Integer port) throws InvalidEndpointUriException { + EndpointUri uri = parser.parse(scheme + "://" + host + (port != null ? ':' + port.toString() : "")); + + assertEquals(scheme, uri.getScheme()); + assertEquals(host, uri.getHost()); + assertEquals(port, uri.getPort()); + } + + @ParameterizedTest + @ValueSource(strings = { // + "coap", // + "coap:localhost", // + "coap:/localhost", // + "coap://localhost:dazd", // + "coap://domain.org:da80", // + "coap://domain----.org:80", // + "coap://----domain.org:80", // + "coap://[dzqd]:80", // + "coap://[aa:aa:aa:aa:aa:aa:aa:aa:aa:aa]:80", // + "coap://[aa:aa:aa:aa::aa:aa:aa:aa]:80", // + "coap://[aa:aa:aa:aa:aa:aa:aa:255.255.255.255]:80", // + "coap://[aa:aa:aa:aa:aa:aa::255.255.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa:255.255.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa:260.255.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa:255.256.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa:2555.256.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa:255.255.255.255.255]:80", // + "coap://[aa:aa:aa::aa:aa:aa%zoneid]:80", // + "coap://[aa:aa:aa::aa:aa:aa%25%zoneid]:80", // + }) + public void parse_invalid_formats(String uri) { + assertThrowsExactly(InvalidEndpointUriException.class, () -> { + parser.parse(uri); + }); + } +} diff --git a/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/EndpointUriTest.java b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/EndpointUriTest.java new file mode 100644 index 0000000000..33b2b4eaf3 --- /dev/null +++ b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/endpoint/EndpointUriTest.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.endpoint; + +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +public class EndpointUriTest { + + @Test + public void assertEqualsHashcode() { + EqualsVerifier.forClass(EndpointUri.class).verify(); + } +} diff --git a/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/util/TestToolBox.java b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/util/TestToolBox.java new file mode 100644 index 0000000000..2f1035c4be --- /dev/null +++ b/leshan-lwm2m-core/src/test/java/org/eclipse/leshan/core/util/TestToolBox.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2024 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.core.util; + +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; + +public class TestToolBox { + + public static EndPointUriHandler uriHandler = new DefaultEndPointUriHandler(); + +} diff --git a/leshan-lwm2m-server-redis/src/main/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDes.java b/leshan-lwm2m-server-redis/src/main/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDes.java index 9cef37dded..815acc0cc3 100644 --- a/leshan-lwm2m-server-redis/src/main/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDes.java +++ b/leshan-lwm2m-server-redis/src/main/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDes.java @@ -16,8 +16,6 @@ *******************************************************************************/ package org.eclipse.leshan.server.redis.serialization; -import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -31,6 +29,9 @@ import org.eclipse.leshan.core.LwM2m.LwM2mVersion; import org.eclipse.leshan.core.LwM2m.Version; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.attributes.Attribute; import org.eclipse.leshan.core.link.attributes.AttributeModel; @@ -60,6 +61,7 @@ public class RegistrationSerDes { private final AttributeParser attributeParser; private final LwM2mPeerSerDes peerSerDes; + private final EndPointUriHandler uriHandler; public RegistrationSerDes(LwM2mPeerSerDes peerSerDes) { // Define all supported Attributes @@ -70,16 +72,22 @@ public RegistrationSerDes(LwM2mPeerSerDes peerSerDes) { // Create default link Parser this.attributeParser = new DefaultAttributeParser(suppportedAttributes); this.peerSerDes = peerSerDes; + this.uriHandler = new DefaultEndPointUriHandler(); } public RegistrationSerDes() { - this.attributeParser = new DefaultAttributeParser(); - this.peerSerDes = new LwM2mPeerSerDes(); + this(new DefaultAttributeParser(), new LwM2mPeerSerDes()); } public RegistrationSerDes(AttributeParser attributeParser, LwM2mPeerSerDes peerSerDes) { + this(attributeParser, peerSerDes, new DefaultEndPointUriHandler()); + } + + public RegistrationSerDes(AttributeParser attributeParser, LwM2mPeerSerDes peerSerDes, + EndPointUriHandler uriHandler) { this.attributeParser = attributeParser; this.peerSerDes = peerSerDes; + this.uriHandler = uriHandler; } public JsonNode jSerialize(Registration r) { @@ -165,10 +173,10 @@ public byte[] bSerialize(Registration r) { } public Registration deserialize(JsonNode jObj) { - URI lastEndpointUsed; + EndpointUri lastEndpointUsed; try { - lastEndpointUsed = new URI(jObj.get("epUri").asText()); - } catch (URISyntaxException e1) { + lastEndpointUsed = uriHandler.createUri(jObj.get("epUri").asText()); + } catch (IllegalStateException e1) { throw new IllegalStateException( String.format("Unable to deserialize last endpoint used URI %s of registration %s/%s", jObj.get("epUri").asText(), jObj.get("regId").asText(), jObj.get("ep").asText())); diff --git a/leshan-lwm2m-server-redis/src/test/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDesTest.java b/leshan-lwm2m-server-redis/src/test/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDesTest.java index 34a73625ce..2c5f3a6330 100644 --- a/leshan-lwm2m-server-redis/src/test/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDesTest.java +++ b/leshan-lwm2m-server-redis/src/test/java/org/eclipse/leshan/server/redis/serialization/RegistrationSerDesTest.java @@ -16,6 +16,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.redis.serialization; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import java.net.Inet4Address; @@ -25,7 +26,6 @@ import java.util.Map; import org.eclipse.leshan.core.LwM2m.LwM2mVersion; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.attributes.AttributeSet; import org.eclipse.leshan.core.link.attributes.ContentFormatAttribute; @@ -58,7 +58,7 @@ public void ser_and_des_are_equals() { Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1)), - EndpointUriUtil.createUri("coap://localhost:5683")).objectLinks(objs).rootPath("/") + uriHandler.createUri("coap://localhost:5683")).objectLinks(objs).rootPath("/") .supportedContentFormats(ContentFormat.TLV, ContentFormat.TEXT); builder.registrationDate(new Date(100L)); builder.lastUpdate(new Date(101L)); @@ -96,7 +96,7 @@ public void ser_and_des_are_equals_with_app_data() { Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1)), - EndpointUriUtil.createUri("coap://localhost:5683")).objectLinks(objs).rootPath("/") + uriHandler.createUri("coap://localhost:5683")).objectLinks(objs).rootPath("/") .supportedContentFormats(ContentFormat.TLV, ContentFormat.TEXT).applicationData(appData); builder.registrationDate(new Date(100L)); diff --git a/leshan-lwm2m-server/pom.xml b/leshan-lwm2m-server/pom.xml index 5cc333d1ce..2f4c5aff30 100644 --- a/leshan-lwm2m-server/pom.xml +++ b/leshan-lwm2m-server/pom.xml @@ -46,6 +46,12 @@ Contributors: junit-jupiter test
+ + org.eclipse.leshan + leshan-lwm2m-core + ${project.version} + test-jar + ch.qos.logback logback-classic diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServer.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServer.java index 02625eb810..d5492b80f4 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServer.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServer.java @@ -24,6 +24,7 @@ import org.eclipse.leshan.core.Destroyable; import org.eclipse.leshan.core.Startable; import org.eclipse.leshan.core.Stoppable; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.CodecException; @@ -135,7 +136,7 @@ public LeshanServer(LwM2mServerEndpointsProvider endpointsProvider, Registration LwM2mDecoder decoder, boolean noQueueMode, ClientAwakeTimeProvider awakeTimeProvider, RegistrationIdProvider registrationIdProvider, RegistrationDataExtractor registrationDataExtractor, boolean updateRegistrationOnNotification, boolean updateRegistrationOnSend, LwM2mLinkParser linkParser, - ServerSecurityInfo serverSecurityInfo) { + EndPointUriHandler uriHandler, ServerSecurityInfo serverSecurityInfo) { Validate.notNull(endpointsProvider, "endpointsProvider cannot be null"); Validate.notNull(registrationStore, "registration store cannot be null"); @@ -163,7 +164,7 @@ public LeshanServer(LwM2mServerEndpointsProvider endpointsProvider, Registration // create endpoints ServerEndpointToolbox toolbox = new ServerEndpointToolbox(decoder, encoder, linkParser, - new DefaultClientProfileProvider(registrationStore, modelProvider)); + new DefaultClientProfileProvider(registrationStore, modelProvider), uriHandler); RegistrationHandler registrationHandler = new RegistrationHandler(registrationService, authorizer, registrationIdProvider, registrationDataExtractor); UplinkDeviceManagementRequestReceiver requestReceiver = createRequestReceiver(registrationHandler, sendService); diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServerBuilder.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServerBuilder.java index 46d30e8195..f4dbb3a385 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServerBuilder.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/LeshanServerBuilder.java @@ -28,6 +28,8 @@ import javax.security.auth.login.Configuration; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.lwm2m.DefaultLwM2mLinkParser; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.LwM2mNode; @@ -86,6 +88,7 @@ public class LeshanServerBuilder { private boolean updateRegistrationOnSend = false; private LwM2mServerEndpointsProvider endpointsProvider; + private EndPointUriHandler uriHandler; /** *

@@ -301,6 +304,16 @@ public LeshanServerBuilder setUpdateRegistrationOnSend(boolean updateRegistratio return this; } + /** + * Set the Uri Handler {@link EndPointUriHandler} + *

+ * By default the {@link DefaultEndPointUriHandler} is used. + */ + public LeshanServerBuilder setEndpointUriHandler(EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + return this; + } + /** * By default LeshanServer doesn't support any protocol. Users need to provide 1 or several * {@link LwM2mServerEndpointsProvider} implementation. @@ -357,13 +370,16 @@ public LeshanServer build() { if (registrationDataExtractor == null) { registrationDataExtractor = new DefaultRegistrationDataExtractor(); } + if (uriHandler == null) { + uriHandler = new DefaultEndPointUriHandler(); + } ServerSecurityInfo serverSecurityInfo = new ServerSecurityInfo(privateKey, publicKey, certificateChain, trustedCertificates); return createServer(endpointsProvider, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor, linkParser, - serverSecurityInfo, updateRegistrationOnNotification, updateRegistrationOnSend); + uriHandler, serverSecurityInfo, updateRegistrationOnNotification, updateRegistrationOnSend); } /** @@ -374,17 +390,17 @@ public LeshanServer build() { * * @see LeshanServer#LeshanServer(LwM2mServerEndpointsProvider, RegistrationStore, SecurityStore, Authorizer, * LwM2mModelProvider, LwM2mEncoder, LwM2mDecoder, boolean, ClientAwakeTimeProvider, RegistrationIdProvider, - * RegistrationDataExtractor, boolean, boolean, LwM2mLinkParser, ServerSecurityInfo) + * RegistrationDataExtractor, boolean, boolean, LwM2mLinkParser, EndPointUriHandler, ServerSecurityInfo) */ protected LeshanServer createServer(LwM2mServerEndpointsProvider endpointsProvider, RegistrationStore registrationStore, SecurityStore securityStore, Authorizer authorizer, LwM2mModelProvider modelProvider, LwM2mEncoder encoder, LwM2mDecoder decoder, boolean noQueueMode, ClientAwakeTimeProvider awakeTimeProvider, RegistrationIdProvider registrationIdProvider, RegistrationDataExtractor registrationDataExtractor, LwM2mLinkParser linkParser, - ServerSecurityInfo serverSecurityInfo, boolean updateRegistrationOnNotification, - boolean updateRegistrationOnSend) { + EndPointUriHandler uriHandler, ServerSecurityInfo serverSecurityInfo, + boolean updateRegistrationOnNotification, boolean updateRegistrationOnSend) { return new LeshanServer(endpointsProvider, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder, noQueueMode, awakeTimeProvider, registrationIdProvider, registrationDataExtractor, - updateRegistrationOnNotification, updateRegistrationOnSend, linkParser, serverSecurityInfo); + updateRegistrationOnNotification, updateRegistrationOnSend, linkParser, uriHandler, serverSecurityInfo); } } diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/DefaultCompositeServerEndpointsProvider.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/DefaultCompositeServerEndpointsProvider.java index 797599b673..adbea7cb17 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/DefaultCompositeServerEndpointsProvider.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/DefaultCompositeServerEndpointsProvider.java @@ -15,13 +15,13 @@ *******************************************************************************/ package org.eclipse.leshan.server.endpoint; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.util.Validate; import org.eclipse.leshan.server.LeshanServer; import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver; @@ -56,7 +56,7 @@ public List getEndpoints() { } @Override - public LwM2mServerEndpoint getEndpoint(URI uri) { + public LwM2mServerEndpoint getEndpoint(EndpointUri uri) { for (LwM2mServerEndpointsProvider provider : providers) { LwM2mServerEndpoint endpoint = provider.getEndpoint(uri); if (endpoint != null) { diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpoint.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpoint.java index 2ffddb4aaf..19b01b8f76 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpoint.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpoint.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.endpoint; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.DownlinkDeviceManagementRequest; @@ -30,7 +29,7 @@ public interface LwM2mServerEndpoint { Protocol getProtocol(); - URI getURI(); + EndpointUri getURI(); String getDescription(); diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpointsProvider.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpointsProvider.java index 4726becf32..75727b0fb2 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpointsProvider.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/LwM2mServerEndpointsProvider.java @@ -15,9 +15,9 @@ *******************************************************************************/ package org.eclipse.leshan.server.endpoint; -import java.net.URI; import java.util.List; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.server.LeshanServer; import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver; import org.eclipse.leshan.server.request.UplinkDeviceManagementRequestReceiver; @@ -27,7 +27,7 @@ public interface LwM2mServerEndpointsProvider { List getEndpoints(); - LwM2mServerEndpoint getEndpoint(URI uri); + LwM2mServerEndpoint getEndpoint(EndpointUri uri); void createEndpoints(UplinkDeviceManagementRequestReceiver requestReceiver, LwM2mNotificationReceiver observationService, ServerEndpointToolbox toolbox, diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/ServerEndpointToolbox.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/ServerEndpointToolbox.java index 6fb3f45838..8a84a253e7 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/ServerEndpointToolbox.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/endpoint/ServerEndpointToolbox.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.endpoint; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.lwm2m.LwM2mLinkParser; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; import org.eclipse.leshan.core.node.codec.LwM2mEncoder; @@ -26,13 +27,15 @@ public class ServerEndpointToolbox { private final LwM2mEncoder encoder; private final LwM2mLinkParser linkParser; private final ClientProfileProvider profileProvider; + private final EndPointUriHandler uriHandler; public ServerEndpointToolbox(LwM2mDecoder decoder, LwM2mEncoder encoder, LwM2mLinkParser linkParser, - ClientProfileProvider profileProvider) { + ClientProfileProvider profileProvider, EndPointUriHandler uriHandler) { this.decoder = decoder; this.encoder = encoder; this.linkParser = linkParser; this.profileProvider = profileProvider; + this.uriHandler = uriHandler; } public LwM2mDecoder getDecoder() { @@ -50,4 +53,8 @@ public LwM2mLinkParser getLinkParser() { public ClientProfileProvider getProfileProvider() { return profileProvider; } + + public EndPointUriHandler getUriHandler() { + return uriHandler; + } } diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/Registration.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/Registration.java index 61766a6e3a..3373af78ae 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/Registration.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/Registration.java @@ -19,7 +19,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -34,7 +33,7 @@ import org.eclipse.leshan.core.LwM2m.LwM2mVersion; import org.eclipse.leshan.core.LwM2m.Version; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.peer.IpPeer; @@ -91,7 +90,7 @@ public class Registration { private final Map applicationData; - private final URI lastEndpointUsed; + private final EndpointUri lastEndpointUsed; protected Registration(Builder builder) { @@ -359,7 +358,7 @@ public Map getApplicationData() { *

* This can be changed in next milestones : https://github.com/eclipse/leshan/issues/1415 */ - public URI getLastEndpointUsed() { + public EndpointUri getLastEndpointUsed() { return lastEndpointUsed; } @@ -406,7 +405,7 @@ public static class Builder { private final String registrationId; private final String endpoint; private final LwM2mPeer clientTransportData; - private final URI lastEndpointUsed; + private final EndpointUri lastEndpointUsed; private Date registrationDate; private Date lastUpdate; @@ -451,12 +450,13 @@ public Builder(Registration registration) { applicationData = registration.applicationData; } - public Builder(String registrationId, String endpoint, LwM2mPeer clientTransportData, URI lastEndpointUsed) { + public Builder(String registrationId, String endpoint, LwM2mPeer clientTransportData, + EndpointUri lastEndpointUsed) { Validate.notNull(registrationId); Validate.notEmpty(endpoint); Validate.notNull(clientTransportData); - EndpointUriUtil.validateURI(lastEndpointUsed); + Validate.notNull(lastEndpointUsed); this.registrationId = registrationId; this.endpoint = endpoint; diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java index 7c29f229a6..a75d346d37 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/registration/RegistrationHandler.java @@ -17,10 +17,10 @@ *******************************************************************************/ package org.eclipse.leshan.server.registration; -import java.net.URI; import java.util.Date; import org.eclipse.leshan.core.LwM2m.LwM2mVersion; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.DeregisterRequest; import org.eclipse.leshan.core.request.RegisterRequest; @@ -57,7 +57,7 @@ public RegistrationHandler(RegistrationServiceImpl registrationService, Authoriz } public SendableResponse register(LwM2mPeer sender, RegisterRequest registerRequest, - URI endpointUsed) { + EndpointUri endpointUsed) { // Extract data from object link LwM2mVersion lwM2mVersion = LwM2mVersion.get(registerRequest.getLwVersion()); diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/DefaultUplinkRequestReceiver.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/DefaultUplinkRequestReceiver.java index 1e7a56b911..a95ea939b2 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/DefaultUplinkRequestReceiver.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/DefaultUplinkRequestReceiver.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.request; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.DeregisterRequest; import org.eclipse.leshan.core.request.RegisterRequest; @@ -43,7 +42,7 @@ public DefaultUplinkRequestReceiver(RegistrationHandler registrationHandler, Sen @Override public void onError(LwM2mPeer sender, ClientProfile senderProfile, Exception exception, Class> requestType, - URI serverEndpointUri) { + EndpointUri serverEndpointUri) { if (requestType.equals(SendRequest.class)) { sendHandler.onError(senderProfile.getRegistration(), exception.getMessage() != null ? exception.getMessage() : null, exception); @@ -52,7 +51,7 @@ public void onError(LwM2mPeer sender, ClientProfile senderProfile, Exception exc @Override public SendableResponse requestReceived(LwM2mPeer sender, ClientProfile senderProfile, - UplinkDeviceManagementRequest request, URI serverEndpointUri) { + UplinkDeviceManagementRequest request, EndpointUri serverEndpointUri) { RequestHandler requestHandler = new RequestHandler(sender, senderProfile, serverEndpointUri); request.accept(requestHandler); @@ -63,10 +62,10 @@ public class RequestHandler implements UplinkDeviceMana private final LwM2mPeer sender; private final ClientProfile senderProfile; - private final URI endpoint; + private final EndpointUri endpoint; private SendableResponse response; - public RequestHandler(LwM2mPeer sender, ClientProfile clientProfile, URI serverEndpointUri) { + public RequestHandler(LwM2mPeer sender, ClientProfile clientProfile, EndpointUri serverEndpointUri) { this.sender = sender; this.senderProfile = clientProfile; this.endpoint = serverEndpointUri; diff --git a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/UplinkDeviceManagementRequestReceiver.java b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/UplinkDeviceManagementRequestReceiver.java index 43a940b0b2..ecfe985127 100644 --- a/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/UplinkDeviceManagementRequestReceiver.java +++ b/leshan-lwm2m-server/src/main/java/org/eclipse/leshan/server/request/UplinkDeviceManagementRequestReceiver.java @@ -15,8 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.request; -import java.net.URI; - +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.request.UplinkDeviceManagementRequest; import org.eclipse.leshan.core.response.LwM2mResponse; @@ -26,8 +25,9 @@ public interface UplinkDeviceManagementRequestReceiver { SendableResponse requestReceived(LwM2mPeer sender, ClientProfile senderProfile, - UplinkDeviceManagementRequest request, URI serverEndpointUri); + UplinkDeviceManagementRequest request, EndpointUri serverEndpointUri); void onError(LwM2mPeer sender, ClientProfile senderProfile, Exception exception, - Class> requestType, URI serverEndpointUri); + Class> requestType, + EndpointUri serverEndpointUri); } diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/queue/PresenceServiceTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/queue/PresenceServiceTest.java index f5356653bf..4811d3e425 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/queue/PresenceServiceTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/queue/PresenceServiceTest.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.queue; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -24,7 +25,6 @@ import java.net.UnknownHostException; import java.util.EnumSet; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.server.registration.Registration; @@ -35,6 +35,7 @@ * */ public class PresenceServiceTest { + private final ClientAwakeTimeProvider awakeTimeProvider = new StaticClientAwakeTimeProvider(); private final PresenceServiceImpl presenceService = new PresenceServiceImpl(awakeTimeProvider); @@ -68,7 +69,7 @@ public void testIsOnline() throws Exception { private Registration givenASimpleClient() throws UnknownHostException { Registration.Builder builder = new Registration.Builder("ID", "urn:client", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 12354)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); Registration reg = builder.build(); presenceService.setAwake(reg); @@ -79,7 +80,7 @@ private Registration givenASimpleClientWithQueueMode() throws UnknownHostExcepti Registration.Builder builder = new Registration.Builder("ID", "urn:client", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 12354)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); Registration reg = builder.bindingMode(EnumSet.of(BindingMode.U, BindingMode.Q)).build(); presenceService.setAwake(reg); diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/InMemoryRegistrationStoreTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/InMemoryRegistrationStoreTest.java index c4ceaedc92..9ca60ddc30 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/InMemoryRegistrationStoreTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/InMemoryRegistrationStoreTest.java @@ -17,6 +17,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.registration; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; @@ -27,7 +28,6 @@ import java.net.UnknownHostException; import java.util.EnumSet; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.request.BindingMode; @@ -101,7 +101,7 @@ public void update_registration_to_extend_time_to_live() { private void givenASimpleRegistration(Long lifetime) { Registration.Builder builder = new Registration.Builder(registrationId, ep, - new IpPeer(new InetSocketAddress(address, port)), EndpointUriUtil.createUri("coap://localhost:5683")); + new IpPeer(new InetSocketAddress(address, port)), uriHandler.createUri("coap://localhost:5683")); registration = builder.lifeTimeInSec(lifetime).smsNumber(sms).bindingMode(binding).objectLinks(objectLinks) .build(); diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java index 9ac2fa47aa..b7f2c16bde 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationHandlerTest.java @@ -18,13 +18,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.net.InetSocketAddress; -import java.net.URI; import java.net.UnknownHostException; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.link.DefaultLinkParser; import org.eclipse.leshan.core.link.LinkParseException; import org.eclipse.leshan.core.peer.IpPeer; @@ -115,8 +114,8 @@ private IpPeer givenIdentity() { return new IpPeer(new InetSocketAddress(0)); } - private URI givenServerEndpointUri() { - return EndpointUriUtil.createUri("coap", "localhost", 5683); + private EndpointUri givenServerEndpointUri() { + return new EndpointUri("coap", "localhost", 5683); } private RegisterRequest givenRegisterRequestWithEndpoint(String endpoint) { diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationSortObjectLinksTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationSortObjectLinksTest.java index 967da1df07..58a1882888 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationSortObjectLinksTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationSortObjectLinksTest.java @@ -17,6 +17,7 @@ package org.eclipse.leshan.server.registration; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -24,7 +25,6 @@ import java.net.InetSocketAddress; import java.net.UnknownHostException; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.peer.IpPeer; import org.junit.jupiter.api.Test; @@ -40,7 +40,7 @@ public void sort_link_object_on_get() throws UnknownHostException { Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLocalHost(), 1)), - EndpointUriUtil.createUri("coap://localhost:5683")).objectLinks(objs); + uriHandler.createUri("coap://localhost:5683")).objectLinks(objs); Registration r = builder.build(); diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationTest.java index 876a0052bb..7f4dc505c2 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationTest.java @@ -16,6 +16,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.registration; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrowsExactly; @@ -28,7 +29,6 @@ import org.eclipse.leshan.core.LwM2m.LwM2mVersion; import org.eclipse.leshan.core.LwM2m.Version; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.LinkParseException; import org.eclipse.leshan.core.link.LinkParser; @@ -281,7 +281,7 @@ private Registration given_a_registration_with_object_link_like(String objectLin throws LinkParseException { Builder builder = new Registration.Builder("id", "endpoint", new IpPeer(InetSocketAddress.createUnresolved("localhost", 0)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); Link[] links = linkParser.parseCoreLinkFormat(objectLinks.getBytes()); builder.objectLinks(links); diff --git a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationUpdateTest.java b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationUpdateTest.java index 4ca928ea12..469a42c42a 100644 --- a/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationUpdateTest.java +++ b/leshan-lwm2m-server/src/test/java/org/eclipse/leshan/server/registration/RegistrationUpdateTest.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.server.registration; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -23,7 +24,6 @@ import java.util.HashMap; import java.util.Map; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.server.queue.PresenceService; import org.junit.jupiter.api.Test; @@ -40,7 +40,7 @@ public class RegistrationUpdateTest { public void testAdditionalAttributesUpdate() throws Exception { Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLocalHost(), 1)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); Map additionalAttributes = new HashMap(); additionalAttributes.put("x", "1"); @@ -75,7 +75,7 @@ public void testApplicationDataUpdate() throws Exception { Registration.Builder builder = new Registration.Builder("registrationId", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLocalHost(), 1)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); Map appData = new HashMap(); appData.put("x", "1"); appData.put("y", "10"); diff --git a/leshan-tl-cf-bsserver-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapOscoreBootstrapServerEndpointFactory.java b/leshan-tl-cf-bsserver-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapOscoreBootstrapServerEndpointFactory.java index 09244981ae..610f480b9d 100644 --- a/leshan-tl-cf-bsserver-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapOscoreBootstrapServerEndpointFactory.java +++ b/leshan-tl-cf-bsserver-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapOscoreBootstrapServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.security.Principal; import org.eclipse.californium.core.coap.Message; @@ -29,6 +28,8 @@ import org.eclipse.californium.oscore.OSCoreCtxDB; import org.eclipse.californium.oscore.OSCoreEndpointContextInfo; import org.eclipse.leshan.bsserver.LeshanBootstrapServer; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.peer.OscoreIdentity; @@ -45,10 +46,14 @@ public class CoapOscoreBootstrapServerEndpointFactory extends CoapBootstrapServe private static final Logger LOG = LoggerFactory.getLogger(CoapOscoreBootstrapServerEndpointFactory.class); - public CoapOscoreBootstrapServerEndpointFactory(URI uri) { + public CoapOscoreBootstrapServerEndpointFactory(EndpointUri uri) { super(uri); } + public CoapOscoreBootstrapServerEndpointFactory(EndpointUri uri, EndPointUriHandler uriHandler) { + super(uri, null, null, null, uriHandler); + } + @Override public String getEndpointDescription() { return super.getEndpointDescription() + " with very experimental support of OSCORE"; diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/BootstrapResource.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/BootstrapResource.java index 5de34e1d54..26e5410bad 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/BootstrapResource.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/BootstrapResource.java @@ -25,6 +25,7 @@ import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.server.resources.CoapExchange; import org.eclipse.leshan.bsserver.request.BootstrapUplinkRequestReceiver; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.request.BootstrapRequest; import org.eclipse.leshan.core.request.ContentFormat; @@ -45,10 +46,13 @@ public class BootstrapResource extends LwM2mCoapResource { private static final String QUERY_PARAM_PREFERRED_CONTENT_FORMAT = "pct="; private final BootstrapUplinkRequestReceiver receiver; + private final EndPointUriHandler uriHandler; - public BootstrapResource(BootstrapUplinkRequestReceiver receiver, IdentityHandlerProvider identityHandlerProvider) { + public BootstrapResource(BootstrapUplinkRequestReceiver receiver, IdentityHandlerProvider identityHandlerProvider, + EndPointUriHandler uriHandler) { super("bs", identityHandlerProvider); this.receiver = receiver; + this.uriHandler = uriHandler; } @Override @@ -94,7 +98,7 @@ public void handlePOST(CoapExchange exchange) { Request coapRequest = exchange.advanced().getRequest(); SendableResponse sendableResponse = receiver.requestReceived(clientIdentity, new BootstrapRequest(endpoint, preferredContentFomart, additionalParams, coapRequest), - exchange.advanced().getEndpoint().getUri()); + uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); BootstrapResponse response = sendableResponse.getResponse(); if (response.isSuccess()) { exchange.respond(toCoapResponseCode(response.getCode())); diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/AbstractEndpointFactoryBuilder.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/AbstractEndpointFactoryBuilder.java index ca5726de73..5000c027d8 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/AbstractEndpointFactoryBuilder.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/AbstractEndpointFactoryBuilder.java @@ -16,22 +16,29 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import java.util.function.Consumer; import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; +import org.eclipse.leshan.core.util.Validate; public abstract class AbstractEndpointFactoryBuilder, TTarget> { - protected URI uri; + protected EndpointUri uri; protected String loggingTagPrefix; protected Configuration configuration; protected Consumer coapEndpointConfigInitializer; + protected EndPointUriHandler uriHandler; + + public AbstractEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + Validate.notNull(uriHandler); + this.uriHandler = uriHandler; + } @SuppressWarnings("unchecked") private SELF self() { @@ -46,8 +53,12 @@ private SELF self() { public abstract TTarget build(); - public SELF setURI(URI uri) { - EndpointUriUtil.validateURI(uri); + public EndPointUriHandler getUriHandler() { + return uriHandler; + } + + public SELF setURI(EndpointUri uri) { + getUriHandler().validateURI(uri); this.uri = uri; return self(); } @@ -57,13 +68,13 @@ public SELF setURI(int port) { } public SELF setURI(InetSocketAddress addr) { - this.uri = EndpointUriUtil.createUri(getSupportedProtocol().getUriScheme(), addr); + this.uri = getUriHandler().createUri(getSupportedProtocol().getUriScheme(), addr); return self(); } public SELF setURI(String uriAsString) { - URI uri = EndpointUriUtil.createUri(uriAsString); - EndpointUriUtil.validateURI(uri); + EndpointUri uri = getUriHandler().createUri(uriAsString); + getUriHandler().validateURI(uri); if (!getSupportedProtocol().getUriScheme().equals(uri.getScheme())) { throw new IllegalArgumentException(String.format("Invalid URI[%s]: Protocol Scheme MUST NOT be [%s]", uri, diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerCoapMessageTranslator.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerCoapMessageTranslator.java index 5af09d299b..1f7a46d9e1 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerCoapMessageTranslator.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerCoapMessageTranslator.java @@ -53,6 +53,7 @@ public T createLwM2mResponse(BootstrapSession destinat public List createResources(BootstrapUplinkRequestReceiver receiver, BootstrapServerEndpointToolbox toolbox, IdentityHandlerProvider identityHandlerProvider) { - return Arrays.asList((Resource) new BootstrapResource(receiver, identityHandlerProvider)); + return Arrays + .asList((Resource) new BootstrapResource(receiver, identityHandlerProvider, toolbox.getUriHandler())); } } diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerProtocolProvider.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerProtocolProvider.java index 200d09cb61..076b03c222 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerProtocolProvider.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/BootstrapServerProtocolProvider.java @@ -15,11 +15,12 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.bsserver.endpoint; -import java.net.URI; import java.util.List; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; public interface BootstrapServerProtocolProvider { @@ -30,7 +31,8 @@ public interface BootstrapServerProtocolProvider { void applyDefaultValue(Configuration configuration); - CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(URI uri); + CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler defaultUriHandler); - URI getDefaultUri(Configuration configuration); + EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler defaultUriHandler); } diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpoint.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpoint.java index 05b6f07fa5..ac355111e3 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpoint.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpoint.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.bsserver.endpoint; -import java.net.URI; import java.util.SortedMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; @@ -30,7 +29,7 @@ import org.eclipse.leshan.bsserver.BootstrapSession; import org.eclipse.leshan.bsserver.endpoint.BootstrapServerEndpointToolbox; import org.eclipse.leshan.bsserver.endpoint.LwM2mBootstrapServerEndpoint; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.DownlinkBootstrapRequest; import org.eclipse.leshan.core.response.ErrorCallback; @@ -77,8 +76,8 @@ public Protocol getProtocol() { } @Override - public URI getURI() { - return EndpointUriUtil.createUri(protocol.getUriScheme(), endpoint.getAddress()); + public EndpointUri getURI() { + return toolbox.getUriHandler().createUri(protocol.getUriScheme(), endpoint.getAddress()); } @Override diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointFactory.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointFactory.java index f5e68c517f..f8d4b3cf4e 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointFactory.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointFactory.java @@ -15,11 +15,10 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.bsserver.endpoint; -import java.net.URI; - import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.leshan.bsserver.LeshanBootstrapServer; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.servers.security.ServerSecurityInfo; import org.eclipse.leshan.transport.californium.ExceptionTranslator; @@ -29,7 +28,7 @@ public interface CaliforniumBootstrapServerEndpointFactory { Protocol getProtocol(); - URI getUri(); + EndpointUri getUri(); String getEndpointDescription(); diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointsProvider.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointsProvider.java index afb130b230..1a0306a184 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointsProvider.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/CaliforniumBootstrapServerEndpointsProvider.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint; import java.net.InetSocketAddress; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -38,7 +37,9 @@ import org.eclipse.leshan.bsserver.endpoint.LwM2mBootstrapServerEndpoint; import org.eclipse.leshan.bsserver.endpoint.LwM2mBootstrapServerEndpointsProvider; import org.eclipse.leshan.bsserver.request.BootstrapUplinkRequestReceiver; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.util.NamedThreadFactory; import org.eclipse.leshan.servers.security.ServerSecurityInfo; @@ -84,7 +85,7 @@ public List getEndpoints() { } @Override - public LwM2mBootstrapServerEndpoint getEndpoint(URI uri) { + public LwM2mBootstrapServerEndpoint getEndpoint(EndpointUri uri) { for (CaliforniumBootstrapServerEndpoint endpoint : endpoints) { if (endpoint.getURI().equals(uri)) return endpoint; @@ -165,8 +166,13 @@ public static class Builder { private final List protocolProviders; private Configuration serverConfiguration; private final List endpointsFactory; + private EndPointUriHandler uriHandler; public Builder(BootstrapServerProtocolProvider... protocolProviders) { + this(new DefaultEndPointUriHandler(), protocolProviders); + } + + public Builder(EndPointUriHandler uriHandler, BootstrapServerProtocolProvider... protocolProviders) { // TODO TL : handle duplicate ? this.protocolProviders = new ArrayList(); if (protocolProviders.length == 0) { @@ -174,10 +180,14 @@ public Builder(BootstrapServerProtocolProvider... protocolProviders) { } else { this.protocolProviders.addAll(Arrays.asList(protocolProviders)); } - + this.uriHandler = uriHandler; this.endpointsFactory = new ArrayList<>(); } + public void setUriHandler(EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + } + /** * Create Californium {@link Configuration} with all Module Definitions needed for protocols provided by this * endpointProvider. @@ -261,15 +271,15 @@ public Builder setConfiguration(Consumer configurationSetter) { } public Builder addEndpoint(String uri) { - return addEndpoint(EndpointUriUtil.createUri(uri)); + return addEndpoint(uriHandler.createUri(uri)); } - public Builder addEndpoint(URI uri) { + public Builder addEndpoint(EndpointUri uri) { for (BootstrapServerProtocolProvider protocolProvider : protocolProviders) { // TODO TL : validate URI if (protocolProvider.getProtocol().getUriScheme().equals(uri.getScheme())) { // TODO TL: handle duplicate addr - endpointsFactory.add(protocolProvider.createDefaultEndpointFactory(uri)); + endpointsFactory.add(protocolProvider.createDefaultEndpointFactory(uri, uriHandler)); } } // TODO TL: handle missing provider for given protocol @@ -277,7 +287,7 @@ public Builder addEndpoint(URI uri) { } public Builder addEndpoint(InetSocketAddress addr, Protocol protocol) { - return addEndpoint(EndpointUriUtil.createUri(protocol.getUriScheme(), addr)); + return addEndpoint(uriHandler.createUri(protocol.getUriScheme(), addr)); } public Builder addEndpoint(CaliforniumBootstrapServerEndpointFactory endpointFactory) { @@ -294,8 +304,8 @@ protected Builder generateDefaultValue() { if (endpointsFactory.isEmpty()) { for (BootstrapServerProtocolProvider protocolProvider : protocolProviders) { // TODO TL : handle duplicates - endpointsFactory.add(protocolProvider - .createDefaultEndpointFactory(protocolProvider.getDefaultUri(serverConfiguration))); + endpointsFactory.add(protocolProvider.createDefaultEndpointFactory( + protocolProvider.getDefaultUri(serverConfiguration, uriHandler), uriHandler)); } } return this; diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactory.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactory.java index dd7483581f..9caef7f913 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactory.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; @@ -32,7 +31,9 @@ import org.eclipse.californium.elements.config.SystemConfig; import org.eclipse.californium.elements.config.UdpConfig; import org.eclipse.leshan.bsserver.LeshanBootstrapServer; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.servers.security.ServerSecurityInfo; import org.eclipse.leshan.transport.californium.DefaultExceptionTranslator; @@ -55,18 +56,20 @@ public static List getModuleDefinitionsProviders() { return Arrays.asList(SystemConfig.DEFINITIONS, CoapConfig.DEFINITIONS, UdpConfig.DEFINITIONS); } - protected final URI endpointUri; + protected final EndpointUri endpointUri; protected final String loggingTagPrefix; protected final Configuration configuration; protected final Consumer coapEndpointConfigInitializer; + protected final EndPointUriHandler uriHandler; - public CoapBootstrapServerEndpointFactory(URI uri) { - this(uri, null, null, null); + public CoapBootstrapServerEndpointFactory(EndpointUri uri) { + this(uri, null, null, null, new DefaultEndPointUriHandler()); } - public CoapBootstrapServerEndpointFactory(URI uri, String loggingTagPrefix, Configuration configuration, - Consumer coapEndpointConfigInitializer) { - EndpointUriUtil.validateURI(uri); + public CoapBootstrapServerEndpointFactory(EndpointUri uri, String loggingTagPrefix, Configuration configuration, + Consumer coapEndpointConfigInitializer, EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + uriHandler.validateURI(uri); this.endpointUri = uri; this.loggingTagPrefix = loggingTagPrefix == null ? "Bootstrap Server" : loggingTagPrefix; @@ -81,7 +84,7 @@ public Protocol getProtocol() { } @Override - public URI getUri() { + public EndpointUri getUri() { return endpointUri; } @@ -112,7 +115,7 @@ public CoapEndpoint createCoapEndpoint(Configuration defaultConfiguration, Serve configurationToUse = configuration; } - return createEndpointBuilder(EndpointUriUtil.getSocketAddr(endpointUri), configurationToUse, server).build(); + return createEndpointBuilder(uriHandler.getSocketAddr(endpointUri), configurationToUse, server).build(); } /** diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactoryBuilder.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactoryBuilder.java index 4b015f88b1..04d9542757 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactoryBuilder.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerEndpointFactoryBuilder.java @@ -19,12 +19,22 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.bsserver.endpoint.AbstractEndpointFactoryBuilder; public class CoapBootstrapServerEndpointFactoryBuilder extends AbstractEndpointFactoryBuilder { + public CoapBootstrapServerEndpointFactoryBuilder() { + this(new DefaultEndPointUriHandler()); + } + + public CoapBootstrapServerEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + super(uriHandler); + } + @Override protected Protocol getSupportedProtocol() { return CoapBootstrapServerEndpointFactory.getSupportedProtocol(); @@ -44,6 +54,6 @@ public List getModuleDefinitionsProviders() { @Override public CoapBootstrapServerEndpointFactory build() { return new CoapBootstrapServerEndpointFactory(uri, loggingTagPrefix, configuration, - coapEndpointConfigInitializer); + coapEndpointConfigInitializer, getUriHandler()); } } diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerProtocolProvider.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerProtocolProvider.java index 9ec395d414..1ae773e474 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerProtocolProvider.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coap/CoapBootstrapServerProtocolProvider.java @@ -16,13 +16,13 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.bsserver.endpoint.BootstrapServerProtocolProvider; import org.eclipse.leshan.transport.californium.bsserver.endpoint.CaliforniumBootstrapServerEndpointFactory; @@ -45,13 +45,14 @@ public List getModuleDefinitionsProviders() { } @Override - public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(URI uri) { - return new CoapBootstrapServerEndpointFactory(uri); + public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler uriHandler) { + return new CoapBootstrapServerEndpointFactoryBuilder(uriHandler).setURI(uri).build(); } @Override - public URI getDefaultUri(Configuration configuration) { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), + public EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler uriHandler) { + return uriHandler.createUri(getProtocol().getUriScheme(), new InetSocketAddress(configuration.get(CoapConfig.COAP_PORT))); } } diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactory.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactory.java index 7cf61acb86..d60148a6e1 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactory.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint.coaps; import java.net.InetSocketAddress; -import java.net.URI; import java.security.Principal; import java.security.PublicKey; import java.util.Arrays; @@ -56,7 +55,9 @@ import org.eclipse.californium.scandium.dtls.x509.SingleCertificateProvider; import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier; import org.eclipse.leshan.bsserver.LeshanBootstrapServer; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; @@ -99,20 +100,22 @@ public static List getModuleDefinitionsProviders() { DtlsConfig.DEFINITIONS); } - protected final URI endpointUri; + protected final EndpointUri endpointUri; protected final String loggingTagPrefix; protected final Configuration configuration; protected final Consumer dtlsConnectorConfigInitializer; protected final Consumer coapEndpointConfigInitializer; + protected final EndPointUriHandler uriHandler; - public CoapsBootstrapServerEndpointFactory(URI uri) { - this(uri, null, null, null, null); + public CoapsBootstrapServerEndpointFactory(EndpointUri uri) { + this(uri, null, null, null, null, new DefaultEndPointUriHandler()); } - public CoapsBootstrapServerEndpointFactory(URI uri, String loggingTagPrefix, Configuration configuration, + public CoapsBootstrapServerEndpointFactory(EndpointUri uri, String loggingTagPrefix, Configuration configuration, Consumer dtlsConnectorConfigInitializer, - Consumer coapEndpointConfigInitializer) { - EndpointUriUtil.validateURI(uri); + Consumer coapEndpointConfigInitializer, EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + uriHandler.validateURI(uri); this.endpointUri = uri; this.loggingTagPrefix = loggingTagPrefix == null ? "Bootstrap Server" : loggingTagPrefix; @@ -127,7 +130,7 @@ public Protocol getProtocol() { } @Override - public URI getUri() { + public EndpointUri getUri() { return endpointUri; } @@ -159,7 +162,7 @@ public CoapEndpoint createCoapEndpoint(Configuration defaultConfiguration, Serve // create DTLS connector Config DtlsConnectorConfig.Builder dtlsConfigBuilder = createDtlsConnectorConfigBuilder(configurationToUse); - setUpDtlsConfig(dtlsConfigBuilder, EndpointUriUtil.getSocketAddr(endpointUri), serverSecurityInfo, server); + setUpDtlsConfig(dtlsConfigBuilder, uriHandler.getSocketAddr(endpointUri), serverSecurityInfo, server); DtlsConnectorConfig dtlsConfig; try { dtlsConfig = dtlsConfigBuilder.build(); diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactoryBuilder.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactoryBuilder.java index 749007d455..b8d48aec56 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactoryBuilder.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerEndpointFactoryBuilder.java @@ -21,6 +21,8 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; import org.eclipse.californium.scandium.config.DtlsConnectorConfig; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.bsserver.endpoint.AbstractEndpointFactoryBuilder; @@ -29,6 +31,14 @@ public class CoapsBootstrapServerEndpointFactoryBuilder extends protected Consumer dtlsConnectorConfigInitializer; + public CoapsBootstrapServerEndpointFactoryBuilder() { + this(new DefaultEndPointUriHandler()); + } + + public CoapsBootstrapServerEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + super(uriHandler); + } + @Override protected Protocol getSupportedProtocol() { return CoapsBootstrapServerEndpointFactory.getSupportedProtocol(); @@ -37,7 +47,6 @@ protected Protocol getSupportedProtocol() { @Override public void applyDefaultValue(Configuration configuration) { CoapsBootstrapServerEndpointFactory.applyDefaultValue(configuration); - } @Override @@ -54,6 +63,6 @@ public CoapsBootstrapServerEndpointFactoryBuilder setDtlsConnectorConfig( @Override public CoapsBootstrapServerEndpointFactory build() { return new CoapsBootstrapServerEndpointFactory(uri, loggingTagPrefix, configuration, - dtlsConnectorConfigInitializer, coapEndpointConfigInitializer); + dtlsConnectorConfigInitializer, coapEndpointConfigInitializer, getUriHandler()); } } diff --git a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerProtocolProvider.java b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerProtocolProvider.java index fabf31d755..068b9a0563 100644 --- a/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerProtocolProvider.java +++ b/leshan-tl-cf-bsserver-coap/src/main/java/org/eclipse/leshan/transport/californium/bsserver/endpoint/coaps/CoapsBootstrapServerProtocolProvider.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.bsserver.endpoint.coaps; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import java.util.function.Consumer; @@ -24,7 +23,8 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; import org.eclipse.californium.scandium.config.DtlsConnectorConfig; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.bsserver.endpoint.BootstrapServerProtocolProvider; import org.eclipse.leshan.transport.californium.bsserver.endpoint.CaliforniumBootstrapServerEndpointFactory; @@ -56,14 +56,15 @@ public List getModuleDefinitionsProviders() { } @Override - public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(URI uri) { - return new CoapsBootstrapServerEndpointFactoryBuilder().setURI(uri) + public CaliforniumBootstrapServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler uriHandler) { + return new CoapsBootstrapServerEndpointFactoryBuilder(uriHandler).setURI(uri) .setDtlsConnectorConfig(dtlsConnectorConfigInitializer).build(); } @Override - public URI getDefaultUri(Configuration configuration) { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), + public EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler uriHandler) { + return uriHandler.createUri(getProtocol().getUriScheme(), new InetSocketAddress(configuration.get(CoapConfig.COAP_SECURE_PORT))); } } diff --git a/leshan-tl-cf-bsserver-coap/src/test/java/org/eclipse/leshan/transport/californium/bsserver/LeshanBootstrapServerTest.java b/leshan-tl-cf-bsserver-coap/src/test/java/org/eclipse/leshan/transport/californium/bsserver/LeshanBootstrapServerTest.java index 2fa90cec0f..da3c86f1e7 100644 --- a/leshan-tl-cf-bsserver-coap/src/test/java/org/eclipse/leshan/transport/californium/bsserver/LeshanBootstrapServerTest.java +++ b/leshan-tl-cf-bsserver-coap/src/test/java/org/eclipse/leshan/transport/californium/bsserver/LeshanBootstrapServerTest.java @@ -18,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.net.InetSocketAddress; -import java.net.URI; import org.eclipse.leshan.bsserver.BootstrapConfig; import org.eclipse.leshan.bsserver.BootstrapConfigStore; @@ -31,6 +30,7 @@ import org.eclipse.leshan.bsserver.LeshanBootstrapServer; import org.eclipse.leshan.bsserver.LeshanBootstrapServerBuilder; import org.eclipse.leshan.bsserver.request.BootstrapDownlinkRequestSender; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.request.BootstrapRequest; @@ -117,7 +117,7 @@ public void testStartStopDestroy() throws InterruptedException { assertEquals(numberOfThreadbefore, Thread.activeCount(), "All news created threads must be destroyed"); } - private void forceThreadsCreation(URI endpointURI) { + private void forceThreadsCreation(EndpointUri endpointURI) { SendableResponse bootstrap = bsHandler.bootstrap(new IpPeer(new InetSocketAddress(5683)), new BootstrapRequest("test"), endpointURI); bootstrap.sent(); diff --git a/leshan-tl-cf-client-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapOscoreClientEndpointFactory.java b/leshan-tl-cf-client-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapOscoreClientEndpointFactory.java index ae992db849..ed649b226c 100644 --- a/leshan-tl-cf-client-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapOscoreClientEndpointFactory.java +++ b/leshan-tl-cf-client-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapOscoreClientEndpointFactory.java @@ -29,6 +29,7 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.oscore.OSCoreCoapStackFactory; import org.eclipse.californium.oscore.OSCoreEndpointContextInfo; +import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox; import org.eclipse.leshan.client.servers.LwM2mServer; import org.eclipse.leshan.client.servers.ServerInfo; import org.eclipse.leshan.core.oscore.InvalidOscoreSettingException; @@ -66,8 +67,8 @@ public String getEndpointDescription() { */ @Override protected CoapEndpoint.Builder createEndpointBuilder(InetSocketAddress address, ServerInfo serverInfo, - Configuration coapConfig) { - CoapEndpoint.Builder builder = super.createEndpointBuilder(address, serverInfo, coapConfig); + Configuration coapConfig, ClientEndpointToolbox toolbox) { + CoapEndpoint.Builder builder = super.createEndpointBuilder(address, serverInfo, coapConfig, toolbox); // handle oscore if (serverInfo.useOscore) { diff --git a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/CaliforniumClientEndpoint.java b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/CaliforniumClientEndpoint.java index 8f4557da99..456ba3f718 100644 --- a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/CaliforniumClientEndpoint.java +++ b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/CaliforniumClientEndpoint.java @@ -15,8 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.client.endpoint; -import java.net.URI; -import java.net.URISyntaxException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -28,6 +26,7 @@ import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox; import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint; import org.eclipse.leshan.client.servers.LwM2mServer; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.LwM2mModel; import org.eclipse.leshan.core.request.UplinkRequest; @@ -80,14 +79,8 @@ public Protocol getProtocol() { } @Override - public URI getURI() { - try { - return new URI(protocol.getUriScheme(), null, getCoapEndpoint().getAddress().getHostString(), - getCoapEndpoint().getAddress().getPort(), null, null, null); - } catch (URISyntaxException e) { - // TODO TL : handle this properly - throw new IllegalStateException(e); - } + public EndpointUri getURI() { + return toolbox.getUriHandler().createUri(protocol.getUriScheme(), getCoapEndpoint().getAddress()); } @Override diff --git a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapClientEndpointFactory.java b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapClientEndpointFactory.java index 3ca5d79e98..f6bcb1e988 100644 --- a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapClientEndpointFactory.java +++ b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coap/CoapClientEndpointFactory.java @@ -17,7 +17,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.security.cert.Certificate; import java.util.List; @@ -30,7 +29,7 @@ import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox; import org.eclipse.leshan.client.servers.LwM2mServer; import org.eclipse.leshan.client.servers.ServerInfo; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.DefaultExceptionTranslator; import org.eclipse.leshan.transport.californium.ExceptionTranslator; @@ -61,7 +60,7 @@ public String getEndpointDescription() { return "CoAP over UDP endpoint based on Californium library"; } - protected String getLoggingTag(URI uri) { + protected String getLoggingTag(EndpointUri uri) { if (loggingTagPrefix != null) { return String.format("[%s-%s]", loggingTagPrefix, uri); } else { @@ -73,7 +72,8 @@ protected String getLoggingTag(URI uri) { public CoapEndpoint createCoapEndpoint(InetAddress clientAddress, Configuration defaultConfiguration, ServerInfo serverInfo, boolean clientInitiatedOnly, List trustStore, ClientEndpointToolbox toolbox) { - return createEndpointBuilder(new InetSocketAddress(clientAddress, 0), serverInfo, defaultConfiguration).build(); + return createEndpointBuilder(new InetSocketAddress(clientAddress, 0), serverInfo, defaultConfiguration, toolbox) + .build(); } /** @@ -85,11 +85,11 @@ public CoapEndpoint createCoapEndpoint(InetAddress clientAddress, Configuration * @return the {@link Builder} used for unsecured communication. */ protected CoapEndpoint.Builder createEndpointBuilder(InetSocketAddress address, ServerInfo serverInfo, - Configuration coapConfig) { + Configuration coapConfig, ClientEndpointToolbox toolbox) { CoapEndpoint.Builder builder = new CoapEndpoint.Builder(); builder.setConnector(createConnector(address, coapConfig)); builder.setConfiguration(coapConfig); - builder.setLoggingTag(getLoggingTag(EndpointUriUtil.createUri(getProtocol().getUriScheme(), address))); + builder.setLoggingTag(getLoggingTag(toolbox.getUriHandler().createUri(getProtocol().getUriScheme(), address))); return builder; } diff --git a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coaps/CoapsClientEndpointFactory.java b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coaps/CoapsClientEndpointFactory.java index d6d44d973e..ce1a4845fd 100644 --- a/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coaps/CoapsClientEndpointFactory.java +++ b/leshan-tl-cf-client-coap/src/main/java/org/eclipse/leshan/transport/californium/client/endpoint/coaps/CoapsClientEndpointFactory.java @@ -17,7 +17,6 @@ import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.security.Principal; import java.security.PublicKey; import java.security.cert.Certificate; @@ -60,7 +59,7 @@ import org.eclipse.leshan.client.servers.LwM2mServer; import org.eclipse.leshan.client.servers.ServerInfo; import org.eclipse.leshan.core.SecurityMode; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; @@ -107,7 +106,7 @@ public Protocol getProtocol() { } @Override - protected String getLoggingTag(URI uri) { + protected String getLoggingTag(EndpointUri uri) { if (loggingTagPrefix != null) { return String.format("[%s-%s]", loggingTagPrefix, uri); } else { @@ -139,7 +138,7 @@ public CoapEndpoint createCoapEndpoint(InetAddress clientAddress, Configuration } // create CoAP endpoint - CoapEndpoint endpoint = createEndpointBuilder(dtlsConfig, defaultConfiguration).build(); + CoapEndpoint endpoint = createEndpointBuilder(dtlsConfig, defaultConfiguration, toolbox).build(); return endpoint; } @@ -257,13 +256,14 @@ private void filterCipherSuites(DtlsConnectorConfig.Builder dtlsConfigurationBui * @param coapConfig the CoAP config used to create this endpoint. * @return the {@link Builder} used for secured communication. */ - protected CoapEndpoint.Builder createEndpointBuilder(DtlsConnectorConfig dtlsConfig, Configuration coapConfig) { + protected CoapEndpoint.Builder createEndpointBuilder(DtlsConnectorConfig dtlsConfig, Configuration coapConfig, + ClientEndpointToolbox toolbox) { CoapEndpoint.Builder builder = new CoapEndpoint.Builder(); builder.setConnector(createSecuredConnector(dtlsConfig)); builder.setConfiguration(coapConfig); - builder.setLoggingTag( - getLoggingTag(EndpointUriUtil.createUri(getProtocol().getUriScheme(), dtlsConfig.getAddress()))); + builder.setLoggingTag(getLoggingTag( + toolbox.getUriHandler().createUri(getProtocol().getUriScheme(), dtlsConfig.getAddress()))); EndpointContextMatcher securedContextMatcher = createSecuredContextMatcher(); builder.setEndpointContextMatcher(securedContextMatcher); diff --git a/leshan-tl-cf-server-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapOscoreServerEndpointFactory.java b/leshan-tl-cf-server-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapOscoreServerEndpointFactory.java index 3dc62fe202..a80147ffd2 100644 --- a/leshan-tl-cf-server-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapOscoreServerEndpointFactory.java +++ b/leshan-tl-cf-server-coap-oscore/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapOscoreServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.server.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.security.Principal; import org.eclipse.californium.core.coap.Message; @@ -27,6 +26,8 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.oscore.OSCoreCoapStackFactory; import org.eclipse.californium.oscore.OSCoreEndpointContextInfo; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; import org.eclipse.leshan.core.peer.OscoreIdentity; @@ -45,10 +46,14 @@ public class CoapOscoreServerEndpointFactory extends CoapServerEndpointFactory { private static final Logger LOG = LoggerFactory.getLogger(CoapOscoreServerEndpointFactory.class); - public CoapOscoreServerEndpointFactory(URI uri) { + public CoapOscoreServerEndpointFactory(EndpointUri uri) { super(uri); } + public CoapOscoreServerEndpointFactory(EndpointUri uri, EndPointUriHandler uriHandler) { + super(uri, null, null, null, uriHandler); + } + @Override public String getEndpointDescription() { return super.getEndpointDescription() + " with very experimental support of OSCORE"; diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/AbstractEndpointFactoryBuilder.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/AbstractEndpointFactoryBuilder.java index 9abc1b50a4..84288b01d1 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/AbstractEndpointFactoryBuilder.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/AbstractEndpointFactoryBuilder.java @@ -16,22 +16,29 @@ package org.eclipse.leshan.transport.californium.server.endpoint; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import java.util.function.Consumer; import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; +import org.eclipse.leshan.core.util.Validate; public abstract class AbstractEndpointFactoryBuilder, TTarget> { - protected URI uri; + protected EndpointUri uri; protected String loggingTagPrefix; protected Configuration configuration; protected Consumer coapEndpointConfigInitializer; + protected EndPointUriHandler uriHandler; + + public AbstractEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + Validate.notNull(uriHandler); + this.uriHandler = uriHandler; + } @SuppressWarnings("unchecked") private SELF self() { @@ -44,10 +51,14 @@ private SELF self() { protected abstract List getModuleDefinitionsProviders(); + protected EndPointUriHandler getUriHandler() { + return uriHandler; + } + public abstract TTarget build(); - public SELF setURI(URI uri) { - EndpointUriUtil.validateURI(uri); + public SELF setURI(EndpointUri uri) { + uriHandler.validateURI(uri); this.uri = uri; return self(); } @@ -57,13 +68,13 @@ public SELF setURI(int port) { } public SELF setURI(InetSocketAddress addr) { - this.uri = EndpointUriUtil.createUri(getSupportedProtocol().getUriScheme(), addr); + this.uri = uriHandler.createUri(getSupportedProtocol().getUriScheme(), addr); return self(); } public SELF setURI(String uriAsString) { - URI uri = EndpointUriUtil.createUri(uriAsString); - EndpointUriUtil.validateURI(uri); + EndpointUri uri = uriHandler.createUri(uriAsString); + uriHandler.validateURI(uri); if (!getSupportedProtocol().getUriScheme().equals(uri.getScheme())) { throw new IllegalArgumentException(String.format("Invalid URI[%s]: Protocol Scheme MUST NOT be [%s]", uri, diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpoint.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpoint.java index 8b7e8ff676..c59320bb0a 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpoint.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpoint.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.endpoint; -import java.net.URI; import java.util.SortedMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; @@ -28,6 +27,7 @@ import org.eclipse.californium.core.coap.Response; import org.eclipse.californium.core.coap.Token; import org.eclipse.californium.core.network.CoapEndpoint; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.DownlinkDeviceManagementRequest; @@ -84,8 +84,8 @@ public Protocol getProtocol() { } @Override - public URI getURI() { - return endpoint.getUri(); + public EndpointUri getURI() { + return toolbox.getUriHandler().createUri(endpoint.getUri()); } @Override diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointFactory.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointFactory.java index 15f0262bfe..aa31169d79 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointFactory.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointFactory.java @@ -15,10 +15,9 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.endpoint; -import java.net.URI; - import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.elements.config.Configuration; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.server.LeshanServer; import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver; @@ -30,7 +29,7 @@ public interface CaliforniumServerEndpointFactory { Protocol getProtocol(); - URI getUri(); + EndpointUri getUri(); String getEndpointDescription(); diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointsProvider.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointsProvider.java index ea21de41e6..68cbca9dcc 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointsProvider.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/CaliforniumServerEndpointsProvider.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.server.endpoint; import java.net.InetSocketAddress; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -36,7 +35,9 @@ import org.eclipse.californium.core.server.resources.Resource; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.observation.CompositeObservation; import org.eclipse.leshan.core.observation.Observation; @@ -99,7 +100,7 @@ public List getEndpoints() { } @Override - public LwM2mServerEndpoint getEndpoint(URI uri) { + public LwM2mServerEndpoint getEndpoint(EndpointUri uri) { for (CaliforniumServerEndpoint endpoint : endpoints) { if (endpoint.getURI().equals(uri)) return endpoint; @@ -226,8 +227,13 @@ public static class Builder { private final List protocolProviders; private Configuration serverConfiguration; private final List endpointsFactory; + private final EndPointUriHandler uriHandler; public Builder(ServerProtocolProvider... protocolProviders) { + this(new DefaultEndPointUriHandler(), protocolProviders); + } + + public Builder(EndPointUriHandler uriHandler, ServerProtocolProvider... protocolProviders) { // TODO TL : handle duplicate ? this.protocolProviders = new ArrayList(); if (protocolProviders.length == 0) { @@ -235,7 +241,7 @@ public Builder(ServerProtocolProvider... protocolProviders) { } else { this.protocolProviders.addAll(Arrays.asList(protocolProviders)); } - + this.uriHandler = uriHandler; this.endpointsFactory = new ArrayList<>(); } @@ -322,15 +328,15 @@ public Builder setConfiguration(Consumer configurationSetter) { } public Builder addEndpoint(String uri) { - return addEndpoint(EndpointUriUtil.createUri(uri)); + return addEndpoint(uriHandler.createUri(uri)); } - public Builder addEndpoint(URI uri) { + public Builder addEndpoint(EndpointUri uri) { for (ServerProtocolProvider protocolProvider : protocolProviders) { // TODO TL : validate URI if (protocolProvider.getProtocol().getUriScheme().equals(uri.getScheme())) { // TODO TL: handle duplicate addr - endpointsFactory.add(protocolProvider.createDefaultEndpointFactory(uri)); + endpointsFactory.add(protocolProvider.createDefaultEndpointFactory(uri, uriHandler)); } } // TODO TL: handle missing provider for given protocol @@ -338,7 +344,7 @@ public Builder addEndpoint(URI uri) { } public Builder addEndpoint(InetSocketAddress addr, Protocol protocol) { - return addEndpoint(EndpointUriUtil.createUri(protocol.getUriScheme(), addr)); + return addEndpoint(uriHandler.createUri(protocol.getUriScheme(), addr)); } public Builder addEndpoint(CaliforniumServerEndpointFactory endpointFactory) { @@ -355,8 +361,8 @@ protected Builder generateDefaultValue() { if (endpointsFactory.isEmpty()) { for (ServerProtocolProvider protocolProvider : protocolProviders) { // TODO TL : handle duplicates - endpointsFactory.add(protocolProvider - .createDefaultEndpointFactory(protocolProvider.getDefaultUri(serverConfiguration))); + endpointsFactory.add(protocolProvider.createDefaultEndpointFactory( + protocolProvider.getDefaultUri(serverConfiguration, uriHandler), uriHandler)); } } return this; diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerCoapMessageTranslator.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerCoapMessageTranslator.java index 3ef7e0a4ee..ff80591b9b 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerCoapMessageTranslator.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerCoapMessageTranslator.java @@ -79,9 +79,10 @@ public T createLwM2mResponse(ClientProfile clientProfi public List createResources(UplinkDeviceManagementRequestReceiver receiver, ServerEndpointToolbox toolbox, IdentityHandlerProvider identityHandlerProvider) { return Arrays.asList( // - (Resource) new RegisterResource(receiver, toolbox.getLinkParser(), identityHandlerProvider), // + (Resource) new RegisterResource(receiver, toolbox.getLinkParser(), identityHandlerProvider, + toolbox.getUriHandler()), // (Resource) new SendResource(receiver, toolbox.getDecoder(), toolbox.getProfileProvider(), - identityHandlerProvider)); + identityHandlerProvider, toolbox.getUriHandler())); } public AbstractLwM2mResponse createObserveResponse(Observation observation, Response coapResponse, diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerProtocolProvider.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerProtocolProvider.java index b370a1b23a..8504aa41c0 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerProtocolProvider.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/ServerProtocolProvider.java @@ -15,11 +15,12 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.endpoint; -import java.net.URI; import java.util.List; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; public interface ServerProtocolProvider { @@ -30,7 +31,7 @@ public interface ServerProtocolProvider { void applyDefaultValue(Configuration configuration); - CaliforniumServerEndpointFactory createDefaultEndpointFactory(URI uri); + CaliforniumServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, EndPointUriHandler uriHandler); - URI getDefaultUri(Configuration configuration); + EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler uriHandler); } diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactory.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactory.java index 56222a8786..8010f12ab7 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactory.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.server.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; @@ -33,7 +32,9 @@ import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; import org.eclipse.californium.elements.config.SystemConfig; import org.eclipse.californium.elements.config.UdpConfig; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.server.LeshanServer; import org.eclipse.leshan.server.observation.LwM2mNotificationReceiver; @@ -60,18 +61,20 @@ public static List getModuleDefinitionsProviders() { return Arrays.asList(SystemConfig.DEFINITIONS, CoapConfig.DEFINITIONS, UdpConfig.DEFINITIONS); } - protected final URI endpointUri; + protected final EndpointUri endpointUri; protected final String loggingTagPrefix; protected final Configuration configuration; protected final Consumer coapEndpointConfigInitializer; + protected final EndPointUriHandler uriHandler; - public CoapServerEndpointFactory(URI uri) { - this(uri, null, null, null); + public CoapServerEndpointFactory(EndpointUri uri) { + this(uri, null, null, null, new DefaultEndPointUriHandler()); } - public CoapServerEndpointFactory(URI uri, String loggingTagPrefix, Configuration configuration, - Consumer coapEndpointConfigInitializer) { - EndpointUriUtil.validateURI(uri); + public CoapServerEndpointFactory(EndpointUri uri, String loggingTagPrefix, Configuration configuration, + Consumer coapEndpointConfigInitializer, EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + uriHandler.validateURI(uri); this.endpointUri = uri; this.loggingTagPrefix = loggingTagPrefix == null ? "LWM2M Server" : loggingTagPrefix; @@ -90,7 +93,7 @@ public String getEndpointDescription() { } @Override - public URI getUri() { + public EndpointUri getUri() { return endpointUri; } @@ -116,8 +119,8 @@ public CoapEndpoint createCoapEndpoint(Configuration defaultConfiguration, Serve configurationToUse = configuration; } - return createEndpointBuilder(EndpointUriUtil.getSocketAddr(endpointUri), configurationToUse, - notificationReceiver, server).build(); + return createEndpointBuilder(uriHandler.getSocketAddr(endpointUri), configurationToUse, notificationReceiver, + server).build(); } /** diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactoryBuilder.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactoryBuilder.java index bde17c3c7e..598a2ef833 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactoryBuilder.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerEndpointFactoryBuilder.java @@ -19,12 +19,22 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.server.endpoint.AbstractEndpointFactoryBuilder; public class CoapServerEndpointFactoryBuilder extends AbstractEndpointFactoryBuilder { + public CoapServerEndpointFactoryBuilder() { + this(new DefaultEndPointUriHandler()); + } + + public CoapServerEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + super(uriHandler); + } + @Override protected Protocol getSupportedProtocol() { return CoapServerEndpointFactory.getSupportedProtocol(); @@ -43,6 +53,7 @@ public List getModuleDefinitionsProviders() { @Override public CoapServerEndpointFactory build() { - return new CoapServerEndpointFactory(uri, loggingTagPrefix, configuration, coapEndpointConfigInitializer); + return new CoapServerEndpointFactory(uri, loggingTagPrefix, configuration, coapEndpointConfigInitializer, + getUriHandler()); } } diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerProtocolProvider.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerProtocolProvider.java index 1f90e4514c..5a7a3a9365 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerProtocolProvider.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coap/CoapServerProtocolProvider.java @@ -16,13 +16,13 @@ package org.eclipse.leshan.transport.californium.server.endpoint.coap; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import org.eclipse.californium.core.config.CoapConfig; import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.server.endpoint.CaliforniumServerEndpointFactory; import org.eclipse.leshan.transport.californium.server.endpoint.ServerProtocolProvider; @@ -45,13 +45,14 @@ public List getModuleDefinitionsProviders() { } @Override - public CaliforniumServerEndpointFactory createDefaultEndpointFactory(URI uri) { - return new CoapServerEndpointFactory(uri); + public CaliforniumServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler uriHandler) { + return new CoapServerEndpointFactoryBuilder(uriHandler).setURI(uri).build(); } @Override - public URI getDefaultUri(Configuration configuration) { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), + public EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler uriHandler) { + return uriHandler.createUri(getProtocol().getUriScheme(), new InetSocketAddress(configuration.get(CoapConfig.COAP_PORT))); } } diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactory.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactory.java index 66be50eda6..d539f1b1c0 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactory.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactory.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.server.endpoint.coaps; import java.net.InetSocketAddress; -import java.net.URI; import java.security.Principal; import java.security.PublicKey; import java.util.Arrays; @@ -57,7 +56,9 @@ import org.eclipse.californium.scandium.dtls.cipher.CipherSuite; import org.eclipse.californium.scandium.dtls.x509.SingleCertificateProvider; import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.peer.IpPeer; import org.eclipse.leshan.core.peer.LwM2mPeer; @@ -112,20 +113,22 @@ public static List getModuleDefinitionsProviders() { DtlsConfig.DEFINITIONS); } - protected final URI endpointUri; + protected final EndpointUri endpointUri; protected final String loggingTagPrefix; protected final Configuration configuration; protected final Consumer dtlsConnectorConfigInitializer; protected final Consumer coapEndpointConfigInitializer; + protected final EndPointUriHandler uriHandler; - public CoapsServerEndpointFactory(URI uri) { - this(uri, null, null, null, null); + public CoapsServerEndpointFactory(EndpointUri uri) { + this(uri, null, null, null, null, new DefaultEndPointUriHandler()); } - public CoapsServerEndpointFactory(URI uri, String loggingTagPrefix, Configuration configuration, + public CoapsServerEndpointFactory(EndpointUri uri, String loggingTagPrefix, Configuration configuration, Consumer dtlsConnectorConfigInitializer, - Consumer coapEndpointConfigInitializer) { - EndpointUriUtil.validateURI(uri); + Consumer coapEndpointConfigInitializer, EndPointUriHandler uriHandler) { + this.uriHandler = uriHandler; + uriHandler.validateURI(uri); this.endpointUri = uri; this.loggingTagPrefix = loggingTagPrefix == null ? "LWM2M Server" : loggingTagPrefix; @@ -140,7 +143,7 @@ public Protocol getProtocol() { } @Override - public URI getUri() { + public EndpointUri getUri() { return endpointUri; } @@ -173,7 +176,7 @@ public CoapEndpoint createCoapEndpoint(Configuration defaultConfiguration, Serve // create DTLS connector Config DtlsConnectorConfig.Builder dtlsConfigBuilder = createDtlsConnectorConfigBuilder(configurationToUse); - setUpDtlsConfig(dtlsConfigBuilder, EndpointUriUtil.getSocketAddr(endpointUri), serverSecurityInfo, server); + setUpDtlsConfig(dtlsConfigBuilder, uriHandler.getSocketAddr(endpointUri), serverSecurityInfo, server); DtlsConnectorConfig dtlsConfig; try { dtlsConfig = dtlsConfigBuilder.build(); diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactoryBuilder.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactoryBuilder.java index 9e4d07d8ca..77b34a4763 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactoryBuilder.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerEndpointFactoryBuilder.java @@ -21,6 +21,8 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; import org.eclipse.californium.scandium.config.DtlsConnectorConfig; +import org.eclipse.leshan.core.endpoint.DefaultEndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.server.endpoint.AbstractEndpointFactoryBuilder; @@ -29,6 +31,14 @@ public class CoapsServerEndpointFactoryBuilder protected Consumer dtlsConnectorConfigInitializer; + public CoapsServerEndpointFactoryBuilder() { + this(new DefaultEndPointUriHandler()); + } + + public CoapsServerEndpointFactoryBuilder(EndPointUriHandler uriHandler) { + super(uriHandler); + } + @Override protected Protocol getSupportedProtocol() { return CoapsServerEndpointFactory.getSupportedProtocol(); @@ -54,6 +64,6 @@ public CoapsServerEndpointFactoryBuilder setDtlsConnectorConfig( @Override public CoapsServerEndpointFactory build() { return new CoapsServerEndpointFactory(uri, loggingTagPrefix, configuration, dtlsConnectorConfigInitializer, - coapEndpointConfigInitializer); + coapEndpointConfigInitializer, getUriHandler()); } } diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerProtocolProvider.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerProtocolProvider.java index d57c5376c2..c498c07da5 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerProtocolProvider.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/endpoint/coaps/CoapsServerProtocolProvider.java @@ -16,7 +16,6 @@ package org.eclipse.leshan.transport.californium.server.endpoint.coaps; import java.net.InetSocketAddress; -import java.net.URI; import java.util.List; import java.util.function.Consumer; @@ -24,7 +23,8 @@ import org.eclipse.californium.elements.config.Configuration; import org.eclipse.californium.elements.config.Configuration.ModuleDefinitionsProvider; import org.eclipse.californium.scandium.config.DtlsConnectorConfig; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.transport.californium.server.endpoint.CaliforniumServerEndpointFactory; import org.eclipse.leshan.transport.californium.server.endpoint.ServerProtocolProvider; @@ -56,14 +56,15 @@ public List getModuleDefinitionsProviders() { } @Override - public CaliforniumServerEndpointFactory createDefaultEndpointFactory(URI uri) { - return new CoapsServerEndpointFactoryBuilder().setURI(uri) + public CaliforniumServerEndpointFactory createDefaultEndpointFactory(EndpointUri uri, + EndPointUriHandler uriHandler) { + return new CoapsServerEndpointFactoryBuilder(uriHandler).setURI(uri) .setDtlsConnectorConfig(dtlsConnectorConfigInitializer).build(); } @Override - public URI getDefaultUri(Configuration configuration) { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), + public EndpointUri getDefaultUri(Configuration configuration, EndPointUriHandler uriHandler) { + return uriHandler.createUri(getProtocol().getUriScheme(), new InetSocketAddress(configuration.get(CoapConfig.COAP_SECURE_PORT))); } } diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/registration/RegisterResource.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/registration/RegisterResource.java index 3169dacbdb..9c261f6b8d 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/registration/RegisterResource.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/registration/RegisterResource.java @@ -28,6 +28,7 @@ import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.server.resources.CoapExchange; import org.eclipse.californium.core.server.resources.Resource; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.LinkParseException; import org.eclipse.leshan.core.link.LinkParser; @@ -74,13 +75,15 @@ public class RegisterResource extends LwM2mCoapResource { private final UplinkDeviceManagementRequestReceiver receiver; private final LinkParser linkParser; + private final EndPointUriHandler uriHandler; public RegisterResource(UplinkDeviceManagementRequestReceiver receiver, LinkParser linkParser, - IdentityHandlerProvider identityHandlerProvider) { + IdentityHandlerProvider identityHandlerProvider, EndPointUriHandler uriHandler) { super(RESOURCE_NAME, identityHandlerProvider); this.receiver = receiver; this.linkParser = linkParser; + this.uriHandler = uriHandler; getAttributes().addResourceType("core.rd"); } @@ -182,7 +185,7 @@ protected void handleRegister(CoapExchange exchange, Request request) { // Handle request // ------------------------------- final SendableResponse sendableResponse = receiver.requestReceived(sender, null, - registerRequest, exchange.advanced().getEndpoint().getUri()); + registerRequest, uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); RegisterResponse response = sendableResponse.getResponse(); // Create CoAP Response from LwM2m request @@ -235,7 +238,7 @@ protected void handleUpdate(CoapExchange exchange, Request request, String regis // Handle request final SendableResponse sendableResponse = receiver.requestReceived(sender, null, updateRequest, - exchange.advanced().getEndpoint().getUri()); + uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); UpdateResponse updateResponse = sendableResponse.getResponse(); // Create CoAP Response from LwM2m request @@ -257,7 +260,7 @@ protected void handleDeregister(CoapExchange exchange, String registrationId) { // Handle request final SendableResponse sendableResponse = receiver.requestReceived(sender, null, - deregisterRequest, exchange.advanced().getEndpoint().getUri()); + deregisterRequest, uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); DeregisterResponse deregisterResponse = sendableResponse.getResponse(); // Create CoAP Response from LwM2m request diff --git a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/send/SendResource.java b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/send/SendResource.java index 61d3c66206..e6573d9e55 100644 --- a/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/send/SendResource.java +++ b/leshan-tl-cf-server-coap/src/main/java/org/eclipse/leshan/transport/californium/server/send/SendResource.java @@ -20,6 +20,7 @@ import org.eclipse.californium.core.coap.CoAP.ResponseCode; import org.eclipse.californium.core.coap.Request; import org.eclipse.californium.core.server.resources.CoapExchange; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; import org.eclipse.leshan.core.node.TimestampedLwM2mNodes; import org.eclipse.leshan.core.node.codec.CodecException; import org.eclipse.leshan.core.node.codec.LwM2mDecoder; @@ -44,13 +45,16 @@ public class SendResource extends LwM2mCoapResource { private final LwM2mDecoder decoder; private final UplinkDeviceManagementRequestReceiver receiver; private final ClientProfileProvider profileProvider; + private final EndPointUriHandler uriHandler; public SendResource(UplinkDeviceManagementRequestReceiver receiver, LwM2mDecoder decoder, - ClientProfileProvider profileProvider, IdentityHandlerProvider identityHandlerProvider) { + ClientProfileProvider profileProvider, IdentityHandlerProvider identityHandlerProvider, + EndPointUriHandler uriHandler) { super("dp", identityHandlerProvider); this.decoder = decoder; this.receiver = receiver; this.profileProvider = profileProvider; + this.uriHandler = uriHandler; } @Override @@ -74,7 +78,7 @@ public void handlePOST(CoapExchange exchange) { receiver.onError(sender, clientProfile, new InvalidRequestException("Unsupported content format [%s] in [%s] from [%s]", contentFormat, coapRequest, sender), - SendRequest.class, exchange.advanced().getEndpoint().getUri()); + SendRequest.class, uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); return; } @@ -84,7 +88,7 @@ public void handlePOST(CoapExchange exchange) { // Handle "send op request SendRequest sendRequest = new SendRequest(contentFormat, data, coapRequest); SendableResponse sendableResponse = receiver.requestReceived(sender, clientProfile, - sendRequest, exchange.advanced().getEndpoint().getUri()); + sendRequest, uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); SendResponse response = sendableResponse.getResponse(); // send reponse @@ -101,10 +105,11 @@ public void handlePOST(CoapExchange exchange) { exchange.respond(ResponseCode.BAD_REQUEST, "Invalid Payload"); receiver.onError(sender, clientProfile, new InvalidRequestException(e, "Invalid payload in [%s] from [%s]", coapRequest, sender), - SendRequest.class, exchange.advanced().getEndpoint().getUri()); + SendRequest.class, uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); return; } catch (RuntimeException e) { - receiver.onError(sender, clientProfile, e, SendRequest.class, exchange.advanced().getEndpoint().getUri()); + receiver.onError(sender, clientProfile, e, SendRequest.class, + uriHandler.createUri(exchange.advanced().getEndpoint().getUri())); throw e; } } diff --git a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/LwM2mObservationStoreTest.java b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/LwM2mObservationStoreTest.java index 14eb38e297..d3e9085dfe 100644 --- a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/LwM2mObservationStoreTest.java +++ b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/LwM2mObservationStoreTest.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.observation; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -34,7 +35,6 @@ import org.eclipse.californium.core.network.serialization.UdpDataParser; import org.eclipse.californium.core.network.serialization.UdpDataSerializer; import org.eclipse.californium.elements.AddressEndpointContext; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.observation.CompositeObservation; @@ -217,7 +217,7 @@ private org.eclipse.californium.core.observe.Observation prepareCoapCompositeObs private void givenASimpleRegistration(Long lifetime) { Registration.Builder builder = new Registration.Builder(registrationId, ep, - new IpPeer(new InetSocketAddress(address, port)), EndpointUriUtil.createUri("coap://localhost:5683")); + new IpPeer(new InetSocketAddress(address, port)), uriHandler.createUri("coap://localhost:5683")); registration = builder.lifeTimeInSec(lifetime).smsNumber(sms).bindingMode(binding).objectLinks(objectLinks) .build(); diff --git a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/ObservationServiceTest.java b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/ObservationServiceTest.java index 8b1d1957b9..a226db6030 100644 --- a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/ObservationServiceTest.java +++ b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/observation/ObservationServiceTest.java @@ -16,12 +16,12 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.observation; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.URI; import java.net.UnknownHostException; import java.util.Arrays; import java.util.EnumSet; @@ -29,7 +29,7 @@ import java.util.Random; import java.util.Set; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.node.LwM2mPath; @@ -79,7 +79,7 @@ public void setUp() throws Exception { private Registration givenASimpleRegistration() throws UnknownHostException { Registration.Builder builder = new Registration.Builder("4711", "urn:endpoint", new IpPeer(new InetSocketAddress(InetAddress.getLocalHost(), 23452)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); return builder.lifeTimeInSec(10000L).bindingMode(EnumSet.of(BindingMode.U)) .objectLinks(new Link[] { new Link("/3") }).build(); } @@ -190,7 +190,7 @@ private Registration givenASimpleClient(String registrationId) { try { builder = new Registration.Builder(registrationId, registrationId + "_ep", new IpPeer(new InetSocketAddress(InetAddress.getLocalHost(), 10000)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); return builder.build(); } catch (UnknownHostException e) { throw new RuntimeException(e); @@ -213,7 +213,7 @@ public T send(ClientProfile destination, } @Override - public URI getURI() { + public EndpointUri getURI() { return null; } @@ -243,7 +243,7 @@ public List getEndpoints() { } @Override - public LwM2mServerEndpoint getEndpoint(URI uri) { + public LwM2mServerEndpoint getEndpoint(EndpointUri uri) { return dummyEndpoint; } diff --git a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/request/CoapRequestBuilderTest.java b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/request/CoapRequestBuilderTest.java index 8fc8e6c0f2..f76153faa7 100644 --- a/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/request/CoapRequestBuilderTest.java +++ b/leshan-tl-cf-server-coap/src/test/java/org/eclipse/leshan/transport/californium/server/request/CoapRequestBuilderTest.java @@ -16,6 +16,7 @@ *******************************************************************************/ package org.eclipse.leshan.transport.californium.server.request; +import static org.eclipse.leshan.core.util.TestToolBox.uriHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -28,7 +29,6 @@ import org.eclipse.californium.core.coap.MediaTypeRegistry; import org.eclipse.californium.core.coap.Request; import org.eclipse.leshan.core.LwM2m.LwM2mVersion; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.attributes.ResourceTypeAttribute; import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttribute; @@ -87,7 +87,7 @@ private Registration newRegistration() throws UnknownHostException { private Registration newRegistration(String rootpath) throws UnknownHostException { Builder b = new Registration.Builder("regid", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 12354)), - EndpointUriUtil.createUri("coap://localhost:5683")); + uriHandler.createUri("coap://localhost:5683")); if (rootpath != null) { Link[] links = new Link[] { new Link(rootpath, new ResourceTypeAttribute("oma.lwm2m")) }; diff --git a/leshan-tl-jc-client-coap/src/main/java/org/eclipse/leshan/transport/javacoap/client/endpoint/JavaCoapClientEndpoint.java b/leshan-tl-jc-client-coap/src/main/java/org/eclipse/leshan/transport/javacoap/client/endpoint/JavaCoapClientEndpoint.java index c024298332..3206eb5816 100644 --- a/leshan-tl-jc-client-coap/src/main/java/org/eclipse/leshan/transport/javacoap/client/endpoint/JavaCoapClientEndpoint.java +++ b/leshan-tl-jc-client-coap/src/main/java/org/eclipse/leshan/transport/javacoap/client/endpoint/JavaCoapClientEndpoint.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.transport.javacoap.client.endpoint; -import java.net.URI; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -29,7 +28,7 @@ import org.eclipse.leshan.client.endpoint.ClientEndpointToolbox; import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint; import org.eclipse.leshan.client.servers.LwM2mServer; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.LwM2mModel; import org.eclipse.leshan.core.request.UplinkRequest; @@ -80,8 +79,8 @@ public String getDescription() { } @Override - public URI getURI() { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), coapServer.getLocalSocketAddress()); + public EndpointUri getURI() { + return toolbox.getUriHandler().createUri(getProtocol().getUriScheme(), coapServer.getLocalSocketAddress()); } @Override diff --git a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/AbstractJavaCoapServerEndpointsProvider.java b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/AbstractJavaCoapServerEndpointsProvider.java index 20afcef931..47e58946d9 100644 --- a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/AbstractJavaCoapServerEndpointsProvider.java +++ b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/AbstractJavaCoapServerEndpointsProvider.java @@ -17,11 +17,11 @@ import java.io.IOException; import java.net.InetSocketAddress; -import java.net.URI; import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.server.LeshanServer; import org.eclipse.leshan.server.endpoint.LwM2mServerEndpoint; @@ -68,7 +68,7 @@ public void createEndpoints(UplinkDeviceManagementRequestReceiver requestReceive ServerSecurityInfo serverSecurityInfo, LeshanServer server) { // TODO: HACK to be able to get local URI in resource, need to discuss about it with java-coap. - EndpointUriProvider endpointUriProvider = new EndpointUriProvider(supportedProtocol); + EndpointUriProvider endpointUriProvider = new EndpointUriProvider(toolbox.getUriHandler(), supportedProtocol); // Create Resources / Routes RegistrationResource registerResource = new RegistrationResource(requestReceiver, toolbox.getLinkParser(), @@ -116,7 +116,7 @@ public List getEndpoints() { } @Override - public LwM2mServerEndpoint getEndpoint(URI uri) { + public LwM2mServerEndpoint getEndpoint(EndpointUri uri) { if (lwm2mEndpoint != null && lwm2mEndpoint.getURI().equals(uri)) return lwm2mEndpoint; else diff --git a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/EndpointUriProvider.java b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/EndpointUriProvider.java index f2b4c8b091..785c912227 100644 --- a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/EndpointUriProvider.java +++ b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/EndpointUriProvider.java @@ -15,9 +15,8 @@ *******************************************************************************/ package org.eclipse.leshan.transport.javacoap.server.endpoint; -import java.net.URI; - -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndPointUriHandler; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import com.mbed.coap.server.CoapServer; @@ -26,16 +25,18 @@ public class EndpointUriProvider { private CoapServer coapServer; private final Protocol protocol; + private final EndPointUriHandler uriHandler; - public EndpointUriProvider(Protocol protocol) { + public EndpointUriProvider(EndPointUriHandler uriHandler, Protocol protocol) { this.protocol = protocol; + this.uriHandler = uriHandler; } public void setCoapServer(CoapServer coapServer) { this.coapServer = coapServer; } - public URI getEndpointUri() { - return EndpointUriUtil.createUri(protocol.getUriScheme(), coapServer.getLocalSocketAddress()); + public EndpointUri getEndpointUri() { + return uriHandler.createUri(protocol.getUriScheme(), coapServer.getLocalSocketAddress()); } } diff --git a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/JavaCoapServerEndpoint.java b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/JavaCoapServerEndpoint.java index b68ffbfa26..24fb2887f0 100644 --- a/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/JavaCoapServerEndpoint.java +++ b/leshan-tl-jc-server-coap/src/main/java/org/eclipse/leshan/transport/javacoap/server/endpoint/JavaCoapServerEndpoint.java @@ -15,7 +15,6 @@ *******************************************************************************/ package org.eclipse.leshan.transport.javacoap.server.endpoint; -import java.net.URI; import java.util.SortedMap; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; @@ -30,7 +29,7 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicLong; -import org.eclipse.leshan.core.endpoint.EndpointUriUtil; +import org.eclipse.leshan.core.endpoint.EndpointUri; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.observation.Observation; import org.eclipse.leshan.core.request.DownlinkDeviceManagementRequest; @@ -90,8 +89,8 @@ public String getDescription() { } @Override - public URI getURI() { - return EndpointUriUtil.createUri(getProtocol().getUriScheme(), coapServer.getLocalSocketAddress()); + public EndpointUri getURI() { + return toolbox.getUriHandler().createUri(getProtocol().getUriScheme(), coapServer.getLocalSocketAddress()); } @Override