Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Java URI usage by EndpointUri #1648

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -49,15 +50,16 @@
public class LockStepLwM2mClient extends LockstepEndpoint {

private static final Random r = new Random();

private final InetSocketAddress destination;
private final LwM2mEncoder encoder;
private final LwM2mModel model;
private final LinkSerializer linkSerializer;

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -71,10 +72,11 @@ public LeshanTestClient(String endpoint, List<? extends LwM2mObjectEnabler> obje
BootstrapConsistencyChecker checker, Map<String, String> additionalAttributes,
Map<String, String> 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;
Expand Down Expand Up @@ -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;
}
Expand Down
Loading