LeshanServerBuilder { public LeshanServer build() { if (localAddress == null) localAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_PORT); if (registrationStore == null) registrationStore = new InMemoryRegistrationStore(); if (authorizer == null) authorizer = new DefaultAuthorizer(securityStore); if (modelProvider == null) modelProvider = new StandardModelProvider(); if (encoder == null) encoder = new DefaultLwM2mNodeEncoder(); if (decoder == null) decoder = new DefaultLwM2mNodeDecoder(); if (coapConfig == null) coapConfig = createDefaultNetworkConfig(); if (awakeTimeProvider == null) { int maxTransmitWait = coapConfig.getInt(Keys.MAX_TRANSMIT_WAIT); if (maxTransmitWait == 0) { LOG.warn( "No value available for MAX_TRANSMIT_WAIT in CoAP NetworkConfig. Fallback with a default 93s value."); awakeTimeProvider = new StaticClientAwakeTimeProvider(); } else { awakeTimeProvider = new StaticClientAwakeTimeProvider(maxTransmitWait); } } if (registrationIdProvider == null) registrationIdProvider = new RandomStringRegistrationIdProvider(); if (endpointFactory == null) { endpointFactory = new DefaultEndpointFactory("LWM2M Server"); } 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 LwM2mPskStore(this.securityStore, registrationStore)); } if (incompleteConfig.getAddress() == null) { if (localSecureAddress == null) { localSecureAddress = new InetSocketAddress(LwM2m.DEFAULT_COAP_SECURE_PORT); } dtlsConfigBuilder.setAddress(localSecureAddress); } else if (localSecureAddress != null && !localSecureAddress.equals(incompleteConfig.getAddress())) { throw new IllegalStateException(String.format( "Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for secure address: %s != %s", localSecureAddress, 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()))); } } } 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.isSniEnabled() == null) { dtlsConfigBuilder.setSniEnabled(false); } Boolean serverOnly = incompleteConfig.isServerOnly(); if (serverOnly == null || !serverOnly) { if (incompleteConfig.getDefaultHandshakeMode() == null) { dtlsConfigBuilder.setDefaultHandshakeMode(DtlsEndpointContext.HANDSHAKE_MODE_NONE); } } 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, registrationStore); } CoapEndpoint securedEndpoint = null; if (!noSecuredEndpoint && dtlsConfig != null) { securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, registrationStore); } if (securedEndpoint == null && unsecuredEndpoint == null) { throw new IllegalStateException( "All CoAP enpoints are deactivated, at least one endpoint should be activated"); } return createServer(unsecuredEndpoint, securedEndpoint, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder, coapConfig, noQueueMode, awakeTimeProvider, registrationIdProvider); }  LeshanServerBuilder setLocalAddress(String hostname, int port); LeshanServerBuilder setLocalAddress(InetSocketAddress localAddress); LeshanServerBuilder setLocalSecureAddress(String hostname, int port); LeshanServerBuilder setLocalSecureAddress(InetSocketAddress localSecureAddress); LeshanServerBuilder setRegistrationStore(CaliforniumRegistrationStore registrationStore); LeshanServerBuilder setSecurityStore(SecurityStore securityStore); LeshanServerBuilder setAuthorizer(Authorizer authorizer); LeshanServerBuilder setObjectModelProvider(LwM2mModelProvider objectModelProvider); LeshanServerBuilder setPublicKey(PublicKey publicKey); LeshanServerBuilder setPrivateKey(PrivateKey privateKey); LeshanServerBuilder setCertificateChain(T[] certificateChain); LeshanServerBuilder setTrustedCertificates(T[] trustedCertificates); LeshanServerBuilder setEncoder(LwM2mNodeEncoder encoder); LeshanServerBuilder setDecoder(LwM2mNodeDecoder decoder); LeshanServerBuilder setCoapConfig(NetworkConfig config); LeshanServerBuilder setDtlsConfig(DtlsConnectorConfig.Builder config); LeshanServerBuilder setEndpointFactory(EndpointFactory endpointFactory); LeshanServerBuilder disableUnsecuredEndpoint(); LeshanServerBuilder disableSecuredEndpoint(); LeshanServerBuilder disableQueueModeSupport(); LeshanServerBuilder setClientAwakeTimeProvider(ClientAwakeTimeProvider awakeTimeProvider); void setRegistrationIdProvider(RegistrationIdProvider registrationIdProvider); LeshanServerBuilder setUpdateRegistrationOnNotification(boolean updateRegistrationOnNotification); static NetworkConfig createDefaultNetworkConfig(); LeshanServer build();  }
@Test public void create_server_without_any_parameter() { server = builder.build(); assertNull(server.getSecuredAddress()); assertNotNull(server.getUnsecuredAddress()); assertNull(server.getSecurityStore()); }