Search in sources :

Example 1 with ConfigurationException

use of org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException 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)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)1 InputStreamReader (java.io.InputStreamReader)1 BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)1 ConfigurationException (org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException)1