From 1d8fdd7c10710d4aa6f2b2351eeb0343e1b42957 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Mon, 18 Mar 2019 15:27:36 +0100 Subject: [PATCH] Add a way to use a custom bootstrap handler --- .../LeshanBootstrapServerBuilder.java | 20 +++++++++++++++++- .../impl/LeshanBootstrapServer.java | 9 ++++---- .../bootstrap/BootstrapHandlerFactory.java | 21 +++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerFactory.java diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java index e170b7a060..84f5622186 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/LeshanBootstrapServerBuilder.java @@ -35,8 +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.server.bootstrap.BootstrapHandler; +import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory; import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager; import org.eclipse.leshan.server.bootstrap.BootstrapStore; +import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender; import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapServer; import org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer; import org.eclipse.leshan.server.californium.impl.LwM2mBootstrapPskStore; @@ -59,6 +62,8 @@ public class LeshanBootstrapServerBuilder { private BootstrapStore configStore; private BootstrapSecurityStore securityStore; private BootstrapSessionManager sessionManager; + private BootstrapHandlerFactory bootstrapHandlerFactory; + private LwM2mModel model; private NetworkConfig coapConfig; private Builder dtlsConfigBuilder; @@ -192,6 +197,11 @@ public LeshanBootstrapServerBuilder setSessionManager(BootstrapSessionManager se return this; } + public LeshanBootstrapServerBuilder setBootstrapHandlerFactory(BootstrapHandlerFactory bootstrapHandlerFactory) { + this.bootstrapHandlerFactory = bootstrapHandlerFactory; + return this; + } + public LeshanBootstrapServerBuilder setModel(LwM2mModel model) { this.model = model; return this; @@ -254,6 +264,14 @@ public LeshanBootstrapServer build() { if (sessionManager == null) sessionManager = new DefaultBootstrapSessionManager(securityStore); + if (bootstrapHandlerFactory == null) + bootstrapHandlerFactory = new BootstrapHandlerFactory() { + @Override + public BootstrapHandler create(BootstrapStore store, LwM2mBootstrapRequestSender sender, + BootstrapSessionManager sessionManager) { + return new BootstrapHandler(store, sender, sessionManager); + } + }; if (model == null) model = new LwM2mModel(ObjectLoader.loadDefault()); if (coapConfig == null) { @@ -395,6 +413,6 @@ public LeshanBootstrapServer build() { } return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager, - model, coapConfig); + bootstrapHandlerFactory, model, coapConfig); } } diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanBootstrapServer.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanBootstrapServer.java index 95733c8d6a..52e1e00e56 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanBootstrapServer.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/impl/LeshanBootstrapServer.java @@ -21,7 +21,7 @@ import org.eclipse.californium.core.network.CoapEndpoint; import org.eclipse.californium.core.network.config.NetworkConfig; import org.eclipse.leshan.core.model.LwM2mModel; -import org.eclipse.leshan.server.bootstrap.BootstrapHandler; +import org.eclipse.leshan.server.bootstrap.BootstrapHandlerFactory; import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager; import org.eclipse.leshan.server.bootstrap.BootstrapStore; import org.eclipse.leshan.server.bootstrap.LwM2mBootstrapRequestSender; @@ -46,11 +46,12 @@ public class LeshanBootstrapServer implements LwM2mBootstrapServer { private final BootstrapSecurityStore bsSecurityStore; public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint, BootstrapStore bsStore, - BootstrapSecurityStore bsSecurityStore, BootstrapSessionManager bsSessionManager, LwM2mModel model, - NetworkConfig coapConfig) { + BootstrapSecurityStore bsSecurityStore, BootstrapSessionManager bsSessionManager, + BootstrapHandlerFactory bsHandlerFactory, LwM2mModel model, NetworkConfig coapConfig) { Validate.notNull(bsStore, "bootstrap store must not be null"); Validate.notNull(bsSessionManager, "session manager must not be null"); + Validate.notNull(bsHandlerFactory, "BootstrapHandler factory must not be null"); Validate.notNull(model, "model must not be null"); Validate.notNull(coapConfig, "coapConfig must not be null"); @@ -73,7 +74,7 @@ public LeshanBootstrapServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint secure unsecuredEndpoint, model); BootstrapResource bsResource = new BootstrapResource( - new BootstrapHandler(bsStore, requestSender, bsSessionManager)); + bsHandlerFactory.create(bsStore, requestSender, bsSessionManager)); coapServer.add(bsResource); } diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerFactory.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerFactory.java new file mode 100644 index 0000000000..9c480dc844 --- /dev/null +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerFactory.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2019 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.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.server.bootstrap; + +public interface BootstrapHandlerFactory { + BootstrapHandler create(BootstrapStore store, LwM2mBootstrapRequestSender sender, + BootstrapSessionManager sessionManager); +}