Skip to content

Commit

Permalink
GH-1395: Add a way set Configuration with lambda in Endpoints Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 8, 2023
1 parent 3e6b0a5 commit f89cd16
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.network.CoapEndpoint;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.leshan.server.bootstrap.request.BootstrapUplinkRequestReceiver;
import org.eclipse.leshan.server.californium.RootResource;
import org.eclipse.leshan.server.californium.bootstrap.endpoint.coap.CoapBootstrapServerProtocolProvider;
import org.eclipse.leshan.server.californium.endpoint.ServerProtocolProvider;
import org.eclipse.leshan.server.security.ServerSecurityInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -175,7 +177,23 @@ public Builder(BootstrapServerProtocolProvider... protocolProviders) {
}

/**
* create Default CoAP Server Configuration.
* Create Californium {@link Configuration} with all Module Definitions needed for protocols provided by this
* endpointProvider.
* <p>
* Once, you create the configuration you should use {@link #setConfiguration(Configuration)}
*
* <pre>
* // Create Builder
* CaliforniumBootstrapServerEndpointsProvider.Builder builder = new CaliforniumBootstrapServerEndpointsProvider.Builder(
* new CoapBootstrapServerProtocolProvider(), //
* new CoapsBootstrapServerProtocolProvider());
*
* // Set custom Californium Configuration :
* Configuration c = builder.createDefaultConfiguration();
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* builder.setConfiguration(c);
* </pre>
*/
public Configuration createDefaultConfiguration() {
// Get all Californium modules
Expand All @@ -197,13 +215,49 @@ public Configuration createDefaultConfiguration() {
}

/**
* @param serverConfiguration the @{link Configuration} used by the {@link CoapServer}.
* Set {@link Configuration} used by the {@link CoapServer}. It will be shared by all endpoints created by this
* endpoints provider.
* <p>
* {@link Configuration} provided SHOULD be created with {@link #createDefaultConfiguration()}.
* <p>
* It should generally not be used with {@link #setConfiguration(Consumer)}
*/
public Builder setConfiguration(Configuration serverConfiguration) {
this.serverConfiguration = serverConfiguration;
return this;
}

/**
* Create Californium {@link Configuration} with all needed Module Definitions for protocol provided by
* {@link ServerProtocolProvider}s, then apply given consumer to it.
*
* <pre>
* {@code
* endpointsBuilder.setConfiguration(c -> {
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* });
* }
* </pre>
*
* This is like doing :
*
* <pre>
* Configuration c = endpointsBuilder.createDefaultConfiguration();
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* endpointsBuilder.setConfiguration(c);
* </pre>
*/
public Builder setConfiguration(Consumer<Configuration> configurationSetter) {
Configuration cfg = createDefaultConfiguration();
configurationSetter.accept(cfg);

// we set config once all is done without exception.
this.serverConfiguration = cfg;
return this;
}

public Builder addEndpoint(String uri) {
return addEndpoint(EndpointUriUtil.createUri(uri));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.Request;
Expand Down Expand Up @@ -225,7 +226,23 @@ public Builder(ServerProtocolProvider... protocolProviders) {
}

/**
* create Default CoAP Server Configuration.
* Create Californium {@link Configuration} with all Module Definitions needed for protocols provided by this
* endpoints provider.
* <p>
* Once, you create the configuration you should use {@link #setConfiguration(Configuration)}
*
* <pre>
* // Create Builder
* CaliforniumServerEndpointsProvider.Builder builder = new CaliforniumServerEndpointsProvider.Builder(
* new CoapServerProtocolProvider(), //
* new CoapsServerProtocolProvider());
*
* // Set custom Californium Configuration :
* Configuration c = builder.createDefaultConfiguration();
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* builder.setConfiguration(c);
* </pre>
*/
public Configuration createDefaultConfiguration() {
// Get all Californium modules
Expand All @@ -247,13 +264,49 @@ public Configuration createDefaultConfiguration() {
}

/**
* @param serverConfiguration the @{link Configuration} used by the {@link CoapServer}.
* Set {@link Configuration} used by the {@link CoapServer}. It will be shared by all endpoints created by this
* endpoints provider.
* <p>
* {@link Configuration} provided SHOULD be created with {@link #createDefaultConfiguration()}.
* <p>
* It should generally not be used with {@link #setConfiguration(Consumer)}
*/
public Builder setConfiguration(Configuration serverConfiguration) {
this.serverConfiguration = serverConfiguration;
return this;
}

/**
* Create Californium {@link Configuration} with all needed Module Definitions for protocol provided by
* {@link ServerProtocolProvider}s, then apply given consumer to it.
*
* <pre>
* {@code
* endpointsBuilder.setConfiguration(c -> {
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* });
* }
* </pre>
*
* This is like doing :
*
* <pre>
* Configuration c = endpointsBuilder.createDefaultConfiguration();
* c.set(DtlsConfig.DTLS_RECOMMENDED_CIPHER_SUITES_ONLY, true);
* c.set(CoapConfig.ACK_TIMEOUT, 1, TimeUnit.SECONDS);
* endpointsBuilder.setConfiguration(c);
* </pre>
*/
public Builder setConfiguration(Consumer<Configuration> configurationSetter) {
Configuration cfg = createDefaultConfiguration();
configurationSetter.accept(cfg);

// we set config once all is done without exception.
this.serverConfiguration = cfg;
return this;
}

public Builder addEndpoint(String uri) {
return addEndpoint(EndpointUriUtil.createUri(uri));
}
Expand Down

0 comments on commit f89cd16

Please sign in to comment.