Skip to content

Commit

Permalink
Add a way to use custom encoder/decoder at bootstrap server side
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 19, 2019
1 parent dca8d7f commit c795508
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
Expand Up @@ -35,6 +35,11 @@
import org.eclipse.leshan.core.californium.Lwm2mEndpointContextMatcher;
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.BootstrapHandler;
import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory;
Expand Down Expand Up @@ -70,6 +75,9 @@ public class LeshanBootstrapServerBuilder {
private NetworkConfig coapConfig;
private Builder dtlsConfigBuilder;

private LwM2mNodeEncoder encoder;
private LwM2mNodeDecoder decoder;

private PublicKey publicKey;
private PrivateKey privateKey;
private X509Certificate[] certificateChain;
Expand Down Expand Up @@ -290,6 +298,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>
Expand Down Expand Up @@ -393,6 +423,10 @@ public BootstrapHandler create(BootstrapStore store, LwM2mBootstrapRequestSender
if (coapConfig == null) {
coapConfig = createDefaultNetworkConfig();
}
if (encoder == null)
encoder = new DefaultLwM2mNodeEncoder();
if (decoder == null)
decoder = new DefaultLwM2mNodeDecoder();

// handle dtlsConfig
DtlsConnectorConfig dtlsConfig = null;
Expand Down Expand Up @@ -534,7 +568,7 @@ public BootstrapHandler create(BootstrapStore store, LwM2mBootstrapRequestSender
}

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

/**
Expand All @@ -551,12 +585,15 @@ public BootstrapHandler create(BootstrapStore store, LwM2mBootstrapRequestSender
* @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,
BootstrapStore bsStore, BootstrapSecurityStore bsSecurityStore, BootstrapSessionManager bsSessionManager,
BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig) {
BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig,
LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder) {
return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, bsStore, bsSecurityStore, bsSessionManager,
bsHandlerFactory, model, coapConfig);
bsHandlerFactory, model, coapConfig, encoder, decoder);
}
}
Expand Up @@ -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;
Expand All @@ -42,14 +40,16 @@ public class CaliforniumLwM2mBootstrapRequestSender implements LwM2mBootstrapReq
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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +55,8 @@ public class LeshanBootstrapServer implements LwM2mBootstrapServer {

public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint, BootstrapStore 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");
Expand All @@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit c795508

Please sign in to comment.