LeshanBootstrapServerBuilder { public LeshanBootstrapServer build() { if (localAddress == null) localAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_PORT); if (configStore == null) configStore = new InMemoryBootstrapConfigStore(); if (sessionManager == null) sessionManager = new DefaultBootstrapSessionManager(securityStore); if (bootstrapHandlerFactory == null) bootstrapHandlerFactory = new BootstrapHandlerFactory() { @Override public BootstrapHandler create(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender, BootstrapSessionManager sessionManager) { return new DefaultBootstrapHandler(store, sender, sessionManager); } }; if (model == null) model = new StaticModel(ObjectLoader.loadDefault()); if (coapConfig == null) { coapConfig = createDefaultNetworkConfig(); } if (endpointFactory == null) { endpointFactory = new DefaultEndpointFactory("LWM2M BS Server"); } if (encoder == null) encoder = new DefaultLwM2mNodeEncoder(); if (decoder == null) decoder = new DefaultLwM2mNodeDecoder(); DtlsConnectorConfig dtlsConfig = null; if (!noSecuredEndpoint && shouldTryToCreateSecureEndpoint()) { if (dtlsConfigBuilder == null) { dtlsConfigBuilder = new DtlsConnectorConfig.Builder(); } DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig(); if (incompleteConfig.getAdvancedPskStore() != null) { LOG.warn( "PskStore should be automatically set by Leshan. Using a custom implementation is not advised."); } else if (securityStore != null) { dtlsConfigBuilder.setPskStore(new LwM2mBootstrapPskStore(securityStore)); } if (incompleteConfig.getAddress() == null) { if (localAddressSecure == null) { localAddressSecure = new InetSocketAddress(LwM2m.DEFAULT_COAP_SECURE_PORT); } 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())); } 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)); if (trustedCertificates != null) { if (incompleteConfig.getCertificateVerifier() == null) { if (incompleteConfig.getTrustStore() == null) { dtlsConfigBuilder.setTrustStore(trustedCertificates); } else if (!Arrays.equals(trustedCertificates, incompleteConfig.getTrustStore())) { throw new IllegalStateException(String.format( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for trusted Certificates (trustStore) : \n%s != \n%s", Arrays.toString(trustedCertificates), Arrays.toString(incompleteConfig.getTrustStore()))); } } else if (trustedCertificates != null) { throw new IllegalStateException( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder: if a CertificateVerifier is set, trustedCertificates must not be set."); } } if (privateKey != null) { if (incompleteConfig.getPrivateKey() != null && !incompleteConfig.getPrivateKey().equals(privateKey)) { throw new IllegalStateException(String.format( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for private key: %s != %s", privateKey, incompleteConfig.getPrivateKey())); } if (certificateChain == null && publicKey != null) { if (incompleteConfig.getPublicKey() != null && !incompleteConfig.getPublicKey().equals(publicKey)) { throw new IllegalStateException(String.format( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for public key: %s != %s", publicKey, incompleteConfig.getPublicKey())); } if (incompleteConfig.getRpkTrustStore() == null) { dtlsConfigBuilder.setRpkTrustAll(); } dtlsConfigBuilder.setIdentity(privateKey, publicKey); } if (certificateChain != null && certificateChain.length > 0) { if (incompleteConfig.getCertificateChain() != null && !Arrays.asList(certificateChain).equals(incompleteConfig.getCertificateChain())) { throw new IllegalStateException(String.format( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for certificate chain: %s != %s", Arrays.toString(certificateChain), incompleteConfig.getCertificateChain())); } if (incompleteConfig.getRpkTrustStore() == null) { dtlsConfigBuilder.setRpkTrustAll(); } dtlsConfigBuilder.setIdentity(privateKey, certificateChain, CertificateType.X_509, CertificateType.RAW_PUBLIC_KEY); } } if (incompleteConfig.isServerOnly() == null) { dtlsConfigBuilder.setServerOnly(true); } if (incompleteConfig.isSniEnabled() == null) { dtlsConfigBuilder.setSniEnabled(false); } try { dtlsConfig = dtlsConfigBuilder.build(); } catch (IllegalStateException e) { LOG.warn("Unable to create DTLS config and so secured endpoint.", e); } } CoapEndpoint unsecuredEndpoint = null; if (!noUnsecuredEndpoint) { unsecuredEndpoint = endpointFactory.createUnsecuredEndpoint(localAddress, coapConfig, null); } CoapEndpoint securedEndpoint = null; if (!noSecuredEndpoint && dtlsConfig != null) { securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, null); } if (securedEndpoint == null && unsecuredEndpoint == null) { throw new IllegalStateException( "All CoAP enpoints are deactivated, at least one endpoint should be activated"); } return createBootstrapServer(unsecuredEndpoint, securedEndpoint, configStore, securityStore, sessionManager, bootstrapHandlerFactory, model, coapConfig, encoder, decoder); }  LeshanBootstrapServerBuilder setLocalAddress(String hostname, int port); LeshanBootstrapServerBuilder setLocalAddress(InetSocketAddress localAddress); LeshanBootstrapServerBuilder setLocalSecureAddress(String hostname, int port); LeshanBootstrapServerBuilder setLocalSecureAddress(InetSocketAddress localSecureAddress); LeshanBootstrapServerBuilder setPublicKey(PublicKey publicKey); LeshanBootstrapServerBuilder setCertificateChain(T[] certificateChain); LeshanBootstrapServerBuilder setPrivateKey(PrivateKey privateKey); LeshanBootstrapServerBuilder setTrustedCertificates(T[] trustedCertificates); LeshanBootstrapServerBuilder setConfigStore(BootstrapConfigStore configStore); LeshanBootstrapServerBuilder setSecurityStore(BootstrapSecurityStore securityStore); LeshanBootstrapServerBuilder setSessionManager(BootstrapSessionManager sessionManager); LeshanBootstrapServerBuilder setBootstrapHandlerFactory(BootstrapHandlerFactory bootstrapHandlerFactory); LeshanBootstrapServerBuilder setModel(LwM2mModel model); LeshanBootstrapServerBuilder setEncoder(LwM2mNodeEncoder encoder); LeshanBootstrapServerBuilder setDecoder(LwM2mNodeDecoder decoder); LeshanBootstrapServerBuilder setCoapConfig(NetworkConfig coapConfig); LeshanBootstrapServerBuilder setDtlsConfig(Builder dtlsConfig); LeshanBootstrapServerBuilder setEndpointFactory(EndpointFactory endpointFactory); LeshanBootstrapServerBuilder disableUnsecuredEndpoint(); LeshanBootstrapServerBuilder disableSecuredEndpoint(); NetworkConfig createDefaultNetworkConfig(); LeshanBootstrapServer build();  }
@Test public void create_server_minimal_parameters() { server = builder.build(); assertNull(server.getSecuredAddress()); assertNotNull(server.getUnsecuredAddress()); }