All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.leshan.server.californium.LeshanServerBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.0-M11
Show newest version
/*******************************************************************************
 * Copyright (c) 2013-2015 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.californium;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.leshan.LwM2m;
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.core.observation.Observation;
import org.eclipse.leshan.server.LwM2mServer;
import org.eclipse.leshan.server.californium.impl.InMemoryRegistrationStore;
import org.eclipse.leshan.server.californium.impl.LeshanServer;
import org.eclipse.leshan.server.impl.InMemorySecurityStore;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.model.StandardModelProvider;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.RegistrationStore;
import org.eclipse.leshan.server.security.Authorizer;
import org.eclipse.leshan.server.security.DefaultAuthorizer;
import org.eclipse.leshan.server.security.SecurityInfo;
import org.eclipse.leshan.server.security.SecurityStore;

/**
 * Class helping you to build and configure a Californium based Leshan Lightweight M2M server. Usage: create it, call
 * the different setters for changing the configuration and then call the {@link #build()} method for creating the
 * {@link LwM2mServer} ready to operate.
 */
public class LeshanServerBuilder {

    private CaliforniumRegistrationStore registrationStore;
    private SecurityStore securityStore;
    private LwM2mModelProvider modelProvider;
    private Authorizer authorizer;

    private InetSocketAddress localAddress;
    private InetSocketAddress localSecureAddress;

    private LwM2mNodeEncoder encoder;
    private LwM2mNodeDecoder decoder;

    private PublicKey publicKey;
    private PrivateKey privateKey;
    private X509Certificate[] certificateChain;
    private Certificate[] trustedCertificates;

    private NetworkConfig networkConfig;

    /**
     * 

* Set the address/port for unsecured CoAP Server. *

* * By default a wildcard address and the default CoAP port(5683) is used * * @param hostname The address to bind. If null wildcard address is used. * @param port A valid port value is between 0 and 65535. A port number of zero will let the system pick up an * ephemeral port in a bind operation. */ public LeshanServerBuilder setLocalAddress(String hostname, int port) { if (hostname == null) { this.localAddress = new InetSocketAddress(port); } else { this.localAddress = new InetSocketAddress(hostname, port); } return this; } /** *

* Set the address for unsecured CoAP Server. *

* * By default a wildcard address and the default CoAP port(5683) is used. */ public LeshanServerBuilder setLocalAddress(InetSocketAddress localAddress) { this.localAddress = localAddress; return this; } /** *

* Set the address/port for secured CoAP Server (Using DTLS). *

* * By default a wildcard address and the default CoAPs port(5684) is used. * * @param hostname The address to bind. If null wildcard address is used. * @param port A valid port value is between 0 and 65535. A port number of zero will let the system pick up an * ephemeral port in a bind operation. */ public LeshanServerBuilder setLocalSecureAddress(String hostname, int port) { if (hostname == null) { this.localSecureAddress = new InetSocketAddress(port); } else { this.localSecureAddress = new InetSocketAddress(hostname, port); } return this; } /** *

* Set the address for secured CoAP Server (Using DTLS). *

* * By default a wildcard address and the default CoAP port(5684) is used. */ public LeshanServerBuilder setLocalSecureAddress(InetSocketAddress localSecureAddress) { this.localSecureAddress = localSecureAddress; return this; } /** *

* Set your {@link RegistrationStore} implementation which stores {@link Registration} and {@link Observation}. *

* By default the {@link InMemoryRegistrationStore} implementation is used. * */ public LeshanServerBuilder setRegistrationStore(CaliforniumRegistrationStore registrationStore) { this.registrationStore = registrationStore; return this; } /** *

* Set your {@link SecurityStore} implementation which stores {@link SecurityInfo}. *

* By default no security store is set and the secure CoAP Server is not launched. An {@link InMemorySecurityStore} * is provided to start using secure connection. * */ public LeshanServerBuilder setSecurityStore(SecurityStore securityStore) { this.securityStore = securityStore; return this; } /** *

* Set your {@link Authorizer} implementation to define if a device if authorize to register to this server. *

* By default the {@link DefaultAuthorizer} implementation is used, if a security store is set. * */ public LeshanServerBuilder setAuthorizer(Authorizer authorizer) { this.authorizer = authorizer; return this; } /** *

* Set your {@link LwM2mModelProvider} implementation. *

* By default the {@link StandardModelProvider} implementation is used which support all core objects for all * devices. * */ public LeshanServerBuilder setObjectModelProvider(LwM2mModelProvider objectModelProvider) { this.modelProvider = objectModelProvider; return this; } /** *

* Set the {@link PublicKey} of the server which will be used for RawPublicKey DTLS authentication. *

* This should be used for RPK support only. If you support RPK and X509, * {@link LeshanServerBuilder#setCertificateChain(X509Certificate[])} should be used. */ public LeshanServerBuilder setPublicKey(PublicKey publicKey) { this.publicKey = publicKey; return this; } /** * Set the {@link PrivateKey} of the server which will be used for RawPublicKey(RPK) and X509 DTLS authentication. */ public LeshanServerBuilder setPrivateKey(PrivateKey privateKey) { this.privateKey = privateKey; return this; } /** *

* Set the CertificateChain of the server which will be used for X509 DTLS authentication. *

* For RPK the public key will be extract from the first X509 certificate of the certificate chain. If you only need * RPK support, use {@link LeshanServerBuilder#setPublicKey(PublicKey)} instead. */ public LeshanServerBuilder setCertificateChain(X509Certificate[] certificateChain) { this.certificateChain = certificateChain; return this; } /** * The list of trusted certificates used to authenticate devices. */ public LeshanServerBuilder setTrustedCertificates(Certificate[] trustedCertificates) { this.trustedCertificates = trustedCertificates; return this; } /** *

* Set the {@link LwM2mNodeEncoder} which will encode {@link LwM2mNode} with supported content format. *

* By default the {@link DefaultLwM2mNodeEncoder} is used. It supports Text, Opaque, TLV and JSON format. */ public LeshanServerBuilder setEncoder(LwM2mNodeEncoder encoder) { this.encoder = encoder; return this; } /** *

* Set the {@link LwM2mNodeDecoder} which will decode data in supported content format to create {@link LwM2mNode}. *

* By default the {@link DefaultLwM2mNodeDecoder} is used. It supports Text, Opaque, TLV and JSON format. */ public LeshanServerBuilder setDecoder(LwM2mNodeDecoder decoder) { this.decoder = decoder; return this; } /** * Set the Californium/CoAP {@link NetworkConfig}. */ public LeshanServerBuilder setNetworkConfig(NetworkConfig config) { this.networkConfig = config; return this; } public LeshanServer build() { if (localAddress == null) localAddress = new InetSocketAddress((InetAddress) null, LwM2m.DEFAULT_COAP_PORT); if (localSecureAddress == null) localSecureAddress = new InetSocketAddress((InetAddress) null, LwM2m.DEFAULT_COAP_SECURE_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 (networkConfig == null) { networkConfig = new NetworkConfig(); } return new LeshanServer(localAddress, localSecureAddress, registrationStore, securityStore, authorizer, modelProvider, encoder, decoder, publicKey, privateKey, certificateChain, trustedCertificates, networkConfig); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy