use of org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet 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());
}
Aggregations