Search in sources :

Example 1 with LeshanBootstrapServer

use of org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer in project leshan by eclipse.

the class LeshanBootstrapServerDemo method createAndStartServer.

public static void createAndStartServer(int webPort, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, String modelsFolderPath, String configFilename) throws Exception {
    // Create Models
    List<ObjectModel> models = ObjectLoader.loadDefault();
    if (modelsFolderPath != null) {
        models.addAll(ObjectLoader.loadObjectsFromDir(new File(modelsFolderPath)));
    }
    // Prepare and start bootstrap server
    LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
    BootstrapStoreImpl bsStore = new BootstrapStoreImpl(configFilename);
    builder.setConfigStore(bsStore);
    builder.setSecurityStore(new BootstrapSecurityStoreImpl(bsStore));
    builder.setLocalAddress(localAddress, localPort);
    builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
    builder.setModel(new LwM2mModel(models));
    // Create CoAP Config
    NetworkConfig coapConfig;
    File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
    if (configFile.isFile()) {
        coapConfig = new NetworkConfig();
        coapConfig.load(configFile);
    } else {
        coapConfig = LeshanServerBuilder.createDefaultNetworkConfig();
        coapConfig.store(configFile);
    }
    builder.setCoapConfig(coapConfig);
    LeshanBootstrapServer bsServer = builder.build();
    bsServer.start();
    // Now prepare and start jetty
    Server server = new Server(webPort);
    WebAppContext root = new WebAppContext();
    root.setContextPath("/");
    root.setResourceBase(LeshanBootstrapServerDemo.class.getClassLoader().getResource("webapp").toExternalForm());
    root.setParentLoaderPriority(true);
    ServletHolder bsServletHolder = new ServletHolder(new BootstrapServlet(bsStore));
    root.addServlet(bsServletHolder, "/api/bootstrap/*");
    ServletHolder serverServletHolder = new ServletHolder(new ServerServlet(bsServer));
    root.addServlet(serverServletHolder, "/api/server/*");
    server.setHandler(root);
    server.start();
    LOG.info("Web server started at {}.", server.getURI());
}
Also used : LeshanBootstrapServerBuilder(org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServerServlet(org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BootstrapServlet(org.eclipse.leshan.server.bootstrap.demo.servlet.BootstrapServlet) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) File(java.io.File)

Example 2 with LeshanBootstrapServer

use of org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer in project leshan by eclipse.

the class LeshanBootstrapServerBuilder method build.

public LeshanBootstrapServer build() {
    if (localAddress == null)
        localAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_PORT);
    // TODO we should have default implementation for BootstrapStore in leshan.server project.
    if (configStore == null)
        throw new IllegalStateException("BootstrapStore is mandatory");
    if (sessionManager == null)
        sessionManager = new DefaultBootstrapSessionManager(securityStore);
    if (model == null)
        model = new LwM2mModel(ObjectLoader.loadDefault());
    if (coapConfig == null) {
        coapConfig = createDefaultNetworkConfig();
    }
    // handle dtlsConfig
    DtlsConnectorConfig dtlsConfig = null;
    if (!noSecuredEndpoint) {
        if (dtlsConfigBuilder == null) {
            dtlsConfigBuilder = new DtlsConnectorConfig.Builder();
        }
        DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig();
        // Handle PSK Store
        if (incompleteConfig.getPskStore() == null && securityStore != null) {
            dtlsConfigBuilder.setPskStore(new LwM2mBootstrapPskStore(securityStore));
        } else {
            LOG.warn("PskStore should be automatically set by Leshan. Using a custom implementation is not advised.");
        }
        // Handle secure address
        if (incompleteConfig.getAddress() == null) {
            if (localAddressSecure == null) {
                localAddressSecure = new InetSocketAddress(0);
            }
            dtlsConfigBuilder.setAddress(localAddressSecure);
        } else if (localAddressSecure != null && !localAddressSecure.equals(incompleteConfig.getAddress())) {
            throw new IllegalStateException(String.format("Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for secure address: %s != %s", localAddressSecure, incompleteConfig.getAddress()));
        }
        // Handle active peers
        if (incompleteConfig.getMaxConnections() == null)
            dtlsConfigBuilder.setMaxConnections(coapConfig.getInt(Keys.MAX_ACTIVE_PEERS));
        if (incompleteConfig.getStaleConnectionThreshold() == null)
            dtlsConfigBuilder.setStaleConnectionThreshold(coapConfig.getLong(Keys.MAX_PEER_INACTIVITY_PERIOD));
        // we try to build the dtlsConfig, if it fail we will just not create the secured endpoint
        try {
            dtlsConfig = dtlsConfigBuilder.build();
        } catch (IllegalStateException e) {
        }
    }
    CoapEndpoint unsecuredEndpoint = null;
    if (!noUnsecuredEndpoint) {
        if (endpointFactory != null) {
            unsecuredEndpoint = endpointFactory.createUnsecuredEndpoint(localAddress, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setInetSocketAddress(localAddress);
            builder.setNetworkConfig(coapConfig);
            unsecuredEndpoint = builder.build();
        }
    }
    CoapEndpoint securedEndpoint = null;
    if (!noSecuredEndpoint && dtlsConfig != null) {
        if (endpointFactory != null) {
            securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, null);
        } else {
            CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
            builder.setConnector(new DTLSConnector(dtlsConfig));
            builder.setNetworkConfig(coapConfig);
            builder.setEndpointContextMatcher(new Lwm2mEndpointContextMatcher());
            securedEndpoint = builder.build();
        }
    }
    if (securedEndpoint == null && unsecuredEndpoint == null) {
        throw new IllegalStateException("All CoAP enpoints are deactivated, at least one endpoint should be activated");
    }
    return new LeshanBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager, model, coapConfig);
}
Also used : LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) InetSocketAddress(java.net.InetSocketAddress) LwM2mBootstrapPskStore(org.eclipse.leshan.server.californium.impl.LwM2mBootstrapPskStore) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) DefaultBootstrapSessionManager(org.eclipse.leshan.server.impl.DefaultBootstrapSessionManager) Lwm2mEndpointContextMatcher(org.eclipse.leshan.core.californium.Lwm2mEndpointContextMatcher) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Aggregations

LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)2 LeshanBootstrapServer (org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer)2 File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)1 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)1 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)1 Builder (org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder)1 Server (org.eclipse.jetty.server.Server)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)1 Lwm2mEndpointContextMatcher (org.eclipse.leshan.core.californium.Lwm2mEndpointContextMatcher)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 BootstrapServlet (org.eclipse.leshan.server.bootstrap.demo.servlet.BootstrapServlet)1 ServerServlet (org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet)1 LeshanBootstrapServerBuilder (org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder)1 LwM2mBootstrapPskStore (org.eclipse.leshan.server.californium.impl.LwM2mBootstrapPskStore)1 DefaultBootstrapSessionManager (org.eclipse.leshan.server.impl.DefaultBootstrapSessionManager)1