Search in sources :

Example 1 with Setting

use of org.structr.api.config.Setting in project structr by structr.

the class ConfigFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 2)) {
            return null;
        }
        final String configKey = sources[0].toString();
        if (Settings.SuperUserPassword.getKey().equals(configKey)) {
            return Principal.HIDDEN;
        }
        final String defaultValue = sources.length >= 2 ? sources[1].toString() : "";
        final Setting setting = Settings.getSetting(configKey);
        if (setting != null) {
            return setting.getValue();
        } else {
            return defaultValue;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : Setting(org.structr.api.config.Setting)

Example 2 with Setting

use of org.structr.api.config.Setting in project structr by structr.

the class ConfigServlet method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    if (!isAuthenticated(request)) {
        // no trailing semicolon so we dont trip MimeTypes.getContentTypeWithoutCharset
        response.setContentType("text/html; charset=utf-8");
        try (final PrintWriter writer = new PrintWriter(response.getWriter())) {
            final Document doc = createLoginDocument(request, writer);
            doc.render();
            writer.append("\n");
            writer.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
    } else {
        if (request.getParameter("reload") != null) {
            // reload data
            Settings.loadConfiguration(ConfigName);
            // redirect
            response.sendRedirect(ConfigUrl);
        } else if (request.getParameter("reset") != null) {
            final String key = request.getParameter("reset");
            final Setting setting = Settings.getSetting(key);
            if (setting != null) {
                if (setting.isDynamic()) {
                    // remove
                    setting.unregister();
                } else {
                    // reset to default
                    setting.setValue(setting.getDefaultValue());
                }
            }
            // serialize settings
            Settings.storeConfiguration(ConfigName);
            // redirect
            response.sendRedirect(ConfigUrl);
        } else if (request.getParameter("start") != null) {
            final String serviceName = request.getParameter("start");
            if (serviceName != null && isAuthenticated(request)) {
                Services.getInstance().startService(serviceName);
            }
            // redirect
            response.sendRedirect(ConfigUrl + "#services");
        } else if (request.getParameter("stop") != null) {
            final String serviceName = request.getParameter("stop");
            if (serviceName != null && isAuthenticated(request)) {
                Services.getInstance().shutdownService(serviceName);
            }
            // redirect
            response.sendRedirect(ConfigUrl + "#services");
        } else if (request.getParameter("restart") != null) {
            final String serviceName = request.getParameter("restart");
            if (serviceName != null && isAuthenticated(request)) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (Throwable t) {
                        }
                        Services.getInstance().shutdownService(serviceName);
                        Services.getInstance().startService(serviceName);
                    }
                }).start();
            }
            // redirect
            response.sendRedirect(ConfigUrl + "#services");
        } else {
            // no trailing semicolon so we dont trip MimeTypes.getContentTypeWithoutCharset
            response.setContentType("text/html; charset=utf-8");
            try (final PrintWriter writer = new PrintWriter(response.getWriter())) {
                final Document doc = createConfigDocument(request, writer);
                doc.render();
                writer.append("\n");
                writer.flush();
            } catch (IOException ioex) {
                ioex.printStackTrace();
            }
        }
    }
}
Also used : Setting(org.structr.api.config.Setting) IOException(java.io.IOException) Document(org.structr.api.util.html.Document) PrintWriter(java.io.PrintWriter)

Aggregations

Setting (org.structr.api.config.Setting)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Document (org.structr.api.util.html.Document)1