Search in sources :

Example 1 with BootstrapConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig in project leshan by eclipse.

the class BootstrapServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (req.getPathInfo() == null) {
        // we need the endpoint in the URL
        sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "endpoint name should be specified in the URL");
        return;
    }
    String[] path = StringUtils.split(req.getPathInfo(), '/');
    // endPoint
    if (path.length != 1) {
        sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "endpoint name should be specified in the URL, nothing more");
        return;
    }
    String endpoint = path[0];
    try {
        BootstrapConfig cfg = gson.fromJson(new InputStreamReader(req.getInputStream()), BootstrapConfig.class);
        if (cfg == null) {
            sendError(resp, HttpServletResponse.SC_BAD_REQUEST, "no content");
        } else {
            bsStore.addConfig(endpoint, cfg);
            resp.setStatus(HttpServletResponse.SC_OK);
        }
    } catch (JsonSyntaxException | ConfigurationException e) {
        sendError(resp, HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) ConfigurationException(org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig)

Example 2 with BootstrapConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig in project leshan by eclipse.

the class BootstrapStoreImpl method loadFromFile.

// /////// File persistence
private void loadFromFile() {
    try {
        File file = new File(filename);
        if (file.exists()) {
            try (InputStreamReader in = new InputStreamReader(new FileInputStream(file))) {
                Map<String, BootstrapConfig> config = gson.fromJson(in, gsonType);
                bootstrapByEndpoint.putAll(config);
            }
        }
    } catch (Exception e) {
        LOG.error("Could not load bootstrap infos from file", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) File(java.io.File) FileInputStream(java.io.FileInputStream) ConfigurationException(org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException)

Example 3 with BootstrapConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig in project leshan by eclipse.

the class LeshanBootstrapServerBuilderTest method start.

@Before
public void start() {
    builder = new LeshanBootstrapServerBuilder();
    builder.setConfigStore(new BootstrapStore() {

        @Override
        public BootstrapConfig getBootstrap(String endpoint) {
            return null;
        }
    });
}
Also used : BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) BootstrapStore(org.eclipse.leshan.server.bootstrap.BootstrapStore) Before(org.junit.Before)

Example 4 with BootstrapConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig in project leshan by eclipse.

the class BootstrapIntegrationTestHelper method createBootstrapServer.

public void createBootstrapServer(BootstrapSecurityStore securityStore) {
    BootstrapStore bsStore = new BootstrapStore() {

        @Override
        public BootstrapConfig getBootstrap(String endpoint) {
            BootstrapConfig bsConfig = new BootstrapConfig();
            // security for BS server
            ServerSecurity bsSecurity = new ServerSecurity();
            bsSecurity.serverId = 1111;
            bsSecurity.bootstrapServer = true;
            bsSecurity.uri = "coap://" + bootstrapServer.getUnsecuredAddress().getHostString() + ":" + bootstrapServer.getUnsecuredAddress().getPort();
            bsSecurity.securityMode = SecurityMode.NO_SEC;
            bsConfig.security.put(0, bsSecurity);
            // security for DM server
            ServerSecurity dmSecurity = new ServerSecurity();
            dmSecurity.uri = "coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort();
            dmSecurity.serverId = 2222;
            dmSecurity.securityMode = SecurityMode.NO_SEC;
            bsConfig.security.put(1, dmSecurity);
            // DM server
            ServerConfig dmConfig = new ServerConfig();
            dmConfig.shortId = 2222;
            bsConfig.servers.put(0, dmConfig);
            return bsConfig;
        }
    };
    if (securityStore == null) {
        securityStore = dummyBsSecurityStore();
    }
    LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
    builder.setConfigStore(bsStore);
    builder.setSecurityStore(securityStore);
    builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    bootstrapServer = builder.build();
}
Also used : ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LeshanBootstrapServerBuilder(org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder) InetSocketAddress(java.net.InetSocketAddress) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity) BootstrapStore(org.eclipse.leshan.server.bootstrap.BootstrapStore)

Example 5 with BootstrapConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig in project leshan by eclipse.

the class BootstrapStoreImpl method deleteConfig.

public boolean deleteConfig(String enpoint) {
    BootstrapConfig res = bootstrapByEndpoint.remove(enpoint);
    saveToFile();
    return res != null;
}
Also used : BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig)

Aggregations

BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)5 InputStreamReader (java.io.InputStreamReader)2 BootstrapStore (org.eclipse.leshan.server.bootstrap.BootstrapStore)2 ConfigurationException (org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig)1 ServerSecurity (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)1 LeshanBootstrapServerBuilder (org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder)1 Before (org.junit.Before)1