Skip to content
Permalink
Browse files

Add a way to use custom encoder/decoder at bootstrap server side

  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent 13cc434 commit 5e344d3034e9fba888a8274fc4e94083a8614e3f
@@ -36,6 +36,11 @@
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfigStore;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
@@ -72,6 +77,9 @@
private NetworkConfig coapConfig;
private Builder dtlsConfigBuilder;

private LwM2mNodeEncoder encoder;
private LwM2mNodeDecoder decoder;

private PublicKey publicKey;
private PrivateKey privateKey;
private X509Certificate[] certificateChain;
@@ -292,6 +300,28 @@ public LeshanBootstrapServerBuilder setModel(LwM2mModel model) {
return this;
}

/**
* <p>
* Set the {@link LwM2mNodeEncoder} which will encode {@link LwM2mNode} with supported content format.
* </p>
* By default the {@link DefaultLwM2mNodeEncoder} is used. It supports Text, Opaque, TLV and JSON format.
*/
public LeshanBootstrapServerBuilder setEncoder(LwM2mNodeEncoder encoder) {
this.encoder = encoder;
return this;
}

/**
* <p>
* Set the {@link LwM2mNodeDecoder} which will decode data in supported content format to create {@link LwM2mNode}.
* </p>
* By default the {@link DefaultLwM2mNodeDecoder} is used. It supports Text, Opaque, TLV and JSON format.
*/
public LeshanBootstrapServerBuilder setDecoder(LwM2mNodeDecoder decoder) {
this.decoder = decoder;
return this;
}

/**
* Set the CoAP/Californium {@link NetworkConfig}.
* <p>
@@ -398,6 +428,10 @@ public BootstrapHandler create(BootstrapConfigStore store, LwM2mBootstrapRequest
if (endpointFactory == null) {
endpointFactory = new DefaultEndpointFactory();
}
if (encoder == null)
encoder = new DefaultLwM2mNodeEncoder();
if (decoder == null)
decoder = new DefaultLwM2mNodeDecoder();

// handle dtlsConfig
DtlsConnectorConfig dtlsConfig = null;
@@ -525,7 +559,7 @@ public BootstrapHandler create(BootstrapConfigStore store, LwM2mBootstrapRequest
}

return createBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager,
bootstrapHandlerFactory, model, coapConfig);
bootstrapHandlerFactory, model, coapConfig, encoder, decoder);
}

/**
@@ -550,13 +584,15 @@ protected boolean shouldTryToCreateSecureEndpoint() {
* @param bsHandlerFactory the factory used to create {@link BootstrapHandler}.
* @param model the LWM2M model used mainly used for data encoding.
* @param coapConfig the CoAP configuration.
* @param decoder decoder used to decode response payload.
* @param encoder encode used to encode request payload.
* @return the LWM2M Bootstrap server.
*/
protected LeshanBootstrapServer createBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
BootstrapConfigStore bsStore, BootstrapSecurityStore bsSecurityStore,
BootstrapSessionManager bsSessionManager, BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model,
NetworkConfig coapConfig) {
NetworkConfig coapConfig, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, bsStore, bsSecurityStore, bsSessionManager,
bsHandlerFactory, model, coapConfig);
bsHandlerFactory, model, coapConfig, encoder, decoder);
}
}
@@ -22,8 +22,6 @@
import org.eclipse.leshan.core.californium.AsyncRequestObserver;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.core.request.DownlinkRequest;
@@ -42,14 +40,16 @@
private final Endpoint nonSecureEndpoint;
private final Endpoint secureEndpoint;
private final LwM2mModel model;
private final LwM2mNodeDecoder decoder = new DefaultLwM2mNodeDecoder();
private final LwM2mNodeEncoder encoder = new DefaultLwM2mNodeEncoder();
private final LwM2mNodeDecoder decoder;
private final LwM2mNodeEncoder encoder;

public CaliforniumLwM2mBootstrapRequestSender(Endpoint secureEndpoint, Endpoint nonSecureEndpoint,
LwM2mModel model) {
public CaliforniumLwM2mBootstrapRequestSender(Endpoint secureEndpoint, Endpoint nonSecureEndpoint, LwM2mModel model,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
this.secureEndpoint = secureEndpoint;
this.nonSecureEndpoint = nonSecureEndpoint;
this.model = model;
this.encoder = encoder;
this.decoder = decoder;
}

@Override
@@ -23,6 +23,8 @@
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.node.codec.LwM2mNodeDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mNodeEncoder;
import org.eclipse.leshan.server.bootstrap.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
@@ -53,7 +55,8 @@

public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint, BootstrapConfigStore bsStore,
BootstrapSecurityStore bsSecurityStore, BootstrapSessionManager bsSessionManager,
BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig) {
BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {

Validate.notNull(bsStore, "bootstrap store must not be null");
Validate.notNull(bsSessionManager, "session manager must not be null");
@@ -77,7 +80,8 @@ public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint secure
coapServer.addEndpoint(securedEndpoint);

// create request sender
LwM2mBootstrapRequestSender requestSender = createRequestSender(securedEndpoint, unsecuredEndpoint, model);
LwM2mBootstrapRequestSender requestSender = createRequestSender(securedEndpoint, unsecuredEndpoint, model,
encoder, decoder);

// create bootstrap resource
CoapResource bsResource = createBootstrapResource(
@@ -90,8 +94,8 @@ protected CoapServer createCoapServer(NetworkConfig coapConfig) {
}

protected LwM2mBootstrapRequestSender createRequestSender(Endpoint securedEndpoint, Endpoint unsecuredEndpoint,
LwM2mModel model) {
return new CaliforniumLwM2mBootstrapRequestSender(securedEndpoint, unsecuredEndpoint, model);
LwM2mModel model, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
return new CaliforniumLwM2mBootstrapRequestSender(securedEndpoint, unsecuredEndpoint, model, encoder, decoder);
}

protected CoapResource createBootstrapResource(BootstrapHandler handler) {

0 comments on commit 5e344d3

Please sign in to comment.
You can’t perform that action at this time.