use of org.structr.api.util.html.Document in project structr by structr.
the class ConfigServlet method createConfigDocument.
// ----- private methods -----
private Document createConfigDocument(final HttpServletRequest request, final PrintWriter writer) {
final Document doc = new Document(writer);
final Tag body = setupDocument(request, doc);
final Tag form = body.block("form").css("config-form");
final Tag main = form.block("div").id("main");
final Tag tabs = main.block("div").id("configTabs");
final Tag menu = tabs.block("ul").id("configTabsMenu");
// configure form
form.attr(new Attr("action", ConfigUrl), new Attr("method", "post"));
for (final SettingsGroup group : Settings.getGroups()) {
final String key = group.getKey();
final String name = group.getName();
menu.block("li").block("a").id(key + "Menu").attr(new Attr("href", "#" + key)).block("span").text(name);
final Tag container = tabs.block("div").css("tab-content").id(key);
// let settings group render itself
group.render(container);
// stop floating
container.block("div").attr(new Style("clear: both;"));
}
// add services tab
menu.block("li").block("a").id("servicesMenu").attr(new Attr("href", "#services")).block("span").text("Services");
final Services services = Services.getInstance();
final Tag container = tabs.block("div").css("tab-content").id("services");
final Tag table = container.block("table").id("services-table");
final Tag header = table.block("tr");
header.block("th").text("Service Name");
header.block("th").attr(new Attr("colspan", "2"));
for (final String serviceClassName : services.getServices()) {
final Class<Service> serviceClass = services.getServiceClassForName(serviceClassName);
final boolean running = serviceClass != null ? services.isReady(serviceClass) : false;
final Tag row = table.block("tr");
row.block("td").text(serviceClassName);
if (running) {
row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?restart=" + serviceClassName + "';")).text("Restart");
if ("HttpService".equals(serviceClassName)) {
row.block("td");
} else {
row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?stop=" + serviceClassName + "';")).text("Stop");
}
row.block("td");
} else {
row.block("td");
row.block("td");
row.block("td").block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?start=" + serviceClassName + "';")).text("Start");
}
}
// update active section so we can restore it when redirecting
container.empty("input").attr(new Type("hidden"), new Name("active_section")).id("active_section");
// stop floating
container.block("div").attr(new Style("clear: both;"));
// buttons
final Tag buttons = form.block("div").css("buttons");
buttons.block("button").attr(new Type("button")).id("new-entry-button").text("Add entry");
buttons.block("button").attr(new Type("button"), new OnClick("window.location.href='" + ConfigUrl + "?reload';")).text("Reload configuration file");
buttons.empty("input").attr(new Type("submit"), new Value("Save to structr.conf"));
body.block("script").text("$('#new-entry-button').on('click', createNewEntry);");
return doc;
}
use of org.structr.api.util.html.Document in project structr by structr.
the class ConfigServlet method createLoginDocument.
private Document createLoginDocument(final HttpServletRequest request, final PrintWriter writer) {
final Document doc = new Document(writer);
final Tag body = setupDocument(request, doc).css("login");
final Tag loginBox = body.block("div").id("login").css("dialog").attr(new Style("display: block; margin: auto; margin-top: 200px;"));
loginBox.block("i").attr(new Attr("title", "Structr Logo")).css("logo-login sprite sprite-structr_gray_100x27");
loginBox.block("p").text("Welcome to the " + TITLE + ". Please log in with the <b>super- user</b> password which can be found in your structr.conf.");
final Tag form = loginBox.block("form").attr(new Attr("action", ConfigUrl), new Attr("method", "post"));
final Tag table = form.block("table");
final Tag row1 = table.block("tr");
row1.block("td").block("label").attr(new Attr("for", "passwordField")).text("Password:");
row1.block("td").empty("input").id("passwordField").attr(new Type("password"), new Name("password"));
final Tag row2 = table.block("tr");
final Tag cell13 = row2.block("td").attr(new Attr("colspan", "2")).css("btn");
final Tag button = cell13.block("button").id("loginButton").attr(new Name("login"));
button.block("i").css("sprite sprite-key");
button.block("span").text(" Login");
cell13.empty("input").attr(new Type("hidden"), new Name("action"), new Value("login"));
return doc;
}
use of org.structr.api.util.html.Document 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();
}
}
}
}
Aggregations