Major refactoring of page and navigation

This commit is contained in:
Ziver Koc 2016-07-22 21:37:51 +02:00
parent bb546ce046
commit 3a0f40dab9
25 changed files with 353 additions and 669 deletions

View file

@ -24,6 +24,7 @@ package wa.server.page;
import wa.server.WAContext;
import wa.server.plugin.WAConfigEntry;
import wa.server.plugin.WAPage;
import wa.server.plugin.WAServiceConfig;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
@ -45,7 +46,7 @@ import java.util.logging.Logger;
*
* Created by Ziver on 2015-07-27.
*/
public class ConfigPage implements WAPage{
public class ConfigPage extends WAPage {
private static final Logger log = LogUtil.getLogger();
private static final String TMPL_FILE = "WebContent/page/ConfigPage.tmpl";
@ -75,13 +76,9 @@ public class ConfigPage implements WAPage{
Configurator<WAConfigEntry> target = findObj(confList, index);
switch (request.get("action")){
case "create":
target = new Configurator(config.createConfig());
target = new Configurator(config.createConfig()).applyConfiguration();
case "modify":
for(Configurator.ConfigurationParam param : target.getConfiguration()){
if(request.containsKey(param.getName()))
param.setValue(request.get(param.getName()));
}
target.applyConfiguration();
target.setValues(request).applyConfiguration();
break;
case "delete":
config.deleteConfig(index);

View file

@ -42,7 +42,7 @@ import java.util.logging.LogRecord;
/**
* Created by Ziver on 2015-09-22.
*/
public class LogPage extends Handler implements HttpPage, WAPage {
public class LogPage extends Handler implements HttpPage {
private static final String TMPL_FILE = "WebContent/page/Log.tmpl";
private CircularBuffer<LogRecord> logBuffer;
@ -69,7 +69,7 @@ public class LogPage extends Handler implements HttpPage, WAPage {
}
@Override
//@Override
public Templator htmlResponse(WAContext context, HttpHeader client_info, Map<String, Object> session, Map<String, String> cookie, Map<String, String> request) {
try {
return new Templator(FileUtil.find(TMPL_FILE));
@ -92,10 +92,12 @@ public class LogPage extends Handler implements HttpPage, WAPage {
writer.write(jsonResponse());
writer.close();
}
@Override
//@Override
public DataNode jsonResponse(WAContext context, HttpHeader client_info, Map<String, Object> session, Map<String, String> cookie, Map<String, String> request) {
return jsonResponse();
}
public DataNode jsonResponse() {
DataNode logNode = new DataNode(DataNode.DataType.List);
Iterator<LogRecord> it = logBuffer.iterator();

View file

@ -23,15 +23,15 @@
package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAPage;
import wa.server.plugin.WAService;
import wa.server.plugin.WAServiceConfig;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeader;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import zutil.ui.Navigation;
import java.io.IOException;
import java.util.ArrayList;
@ -42,31 +42,36 @@ import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-06.
*/
public class ServicePage implements WAPage {
public class ServicePage extends WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Services";
private static final String TMPL_FILE = "WebContent/page/ServicePage.tmpl";
private ServiceStatusPage rootStatusPage;
private ArrayList<WAService> services;
private ArrayList<ServiceStatusPage> statusPages;
private ArrayList<LogPage> logPages;
private ArrayList<ConfigPage> configPages;
public ServicePage(PluginManager pluginManager){
public ServicePage(){
super.setPageName("service");
PluginManager pluginManager = new PluginManager();
this.services = pluginManager.toArray(WAService.class);
this.rootStatusPage = new ServiceStatusPage(pluginManager);
this.statusPages = new ArrayList<>();
this.logPages = new ArrayList<>();
this.configPages = new ArrayList<>();
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
Navigation nav = WAContext.getRootNav().createSubNav(this.getPageName(), NAVIGATION_NAME);
nav.setResource(this);
nav.setWeight(100);
for(WAService plugin : services) {
statusPages.add(new ServiceStatusPage(plugin.getStatus()));
logPages.add(new LogPage(plugin.getLog()));
WANavigation serviceNav = nav.createSubNav(plugin.getName());
Navigation serviceNav = nav.createSubNav(plugin.getName());
serviceNav.setResource(plugin);
for(WAServiceConfig conf : plugin.getConfigurations()){
ConfigPage page = new ConfigPage(conf);
@ -115,14 +120,6 @@ public class ServicePage implements WAPage {
return null;
}
public DataNode jsonResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
return null;
}
}

View file

@ -23,7 +23,7 @@
package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAPage;
import wa.server.plugin.WAServiceStatus;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
@ -31,6 +31,7 @@ import zutil.net.http.HttpHeader;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import zutil.ui.Navigation;
import java.io.IOException;
import java.util.ArrayList;
@ -41,7 +42,7 @@ import java.util.logging.Logger;
/**
* Created by Ziver on 2015-04-06.
*/
public class ServiceStatusPage implements WAPage {
public class ServiceStatusPage extends WAPage {
private static final Logger log = LogUtil.getLogger();
public static final String NAVIGATION_NAME = "Service Status";
private static final String TMPL_FILE = "WebContent/page/ServiceStatusPage.tmpl";
@ -56,7 +57,7 @@ public class ServiceStatusPage implements WAPage {
public ServiceStatusPage(PluginManager pluginManager){
this.services = pluginManager.toArray(WAServiceStatus.class);
WANavigation nav = WAContext.getRootNav().createSubNav(ServicePage.NAVIGATION_NAME)
Navigation nav = WAContext.getRootNav().createSubNav(ServicePage.NAVIGATION_NAME)
.createSubNav(NAVIGATION_NAME);
nav.setResource(this);
}

View file

@ -1,91 +0,0 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page;
import wa.server.WAContext;
import wa.server.page.struct.WANavigation;
import wa.server.plugin.WAStatus;
import zutil.net.http.HttpHeader;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import zutil.plugin.PluginManager;
import java.util.ArrayList;
import java.util.Map;
/**
* Created by Ziver on 2015-04-06.
*/
public class StatusPage implements WAPage {
public static final String NAVIGATION_NAME = "Status";
private ArrayList<WAStatus> plugins;
public StatusPage(PluginManager pluginManager){
this.plugins = pluginManager.toArray(WAStatus.class);
WANavigation nav = WAContext.getRootNav().createSubNav(NAVIGATION_NAME);
nav.setResource(this);
for(WAStatus plugin : plugins)
nav.createSubNav(plugin.getName()).setResource(plugin);
}
@Override
public Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) {
WAStatus obj = getPlugin(context);
if(obj != null) {
return new Templator(obj.html());
}
return null;
}
public DataNode jsonResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request){
// Do JSON request in all status plugins
DataNode root = new DataNode(DataNode.DataType.Map);
for(WAStatus obj : plugins) {
if (obj != null)
obj.jsonUpdate(request, root);
}
return root;
}
private WAStatus getPlugin(WAContext context){
if(context.getBreadcrumb().size() >= 2){
int i = plugins.indexOf(context.getBreadcrumb().get(1).getResource());
if(i >= 0)
return plugins.get(i);
}
return null;
}
}

View file

@ -1,49 +0,0 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page;
import wa.server.WAContext;
import zutil.net.http.HttpHeader;
import zutil.parser.DataNode;
import zutil.parser.Templator;
import java.util.Map;
/**
* Created by Ziver on 2015-04-25.
*/
public interface WAPage {
public abstract Templator htmlResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request);
public DataNode jsonResponse(WAContext context,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request);
}

View file

@ -1,230 +0,0 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package wa.server.page.struct;
import java.util.*;
/**
* A class representing a navigation hierarchy/tree for a web application.
*
* Created by Ziver on 2015-04-02.
*/
public class WANavigation implements Iterable{
private static final String NAVIGATION_URL_KEY = "i";
private static int nextId = 0;
private static HashMap<Integer, WANavigation> navMap = new HashMap<Integer, WANavigation>();
private final int id;
private String url;
private String name;
private int weight;
private WANavigation parentNav;
private ArrayList<WANavigation> subNav;
private Object resource;
private WANavigation(String name) {
this.id = nextId++;
this.navMap.put(this.id, this);
this.url = "?"+NAVIGATION_URL_KEY+"="+this.id;
this.name = name;
this.subNav = new ArrayList<>();
}
public List<WANavigation> getSubNavs() {
return subNav;
}
/**
* Will create a new sub-nav if it does not already exist or return a existing one.
*/
public WANavigation createSubNav(String name) {
WANavigation nav = getSubNav(name);
if(nav != null)
return nav;
nav = new WANavigation(name);
nav.setParentNav(this);
subNav.add(nav);
sortSubNavs();
return nav;
}
/**
* Searches for and returns the specified sub-nav or returns null if it was not found.
*/
private WANavigation getSubNav(String name) {
for(WANavigation nav : subNav) {
if(nav.equals(name))
return nav;
}
return null;
}
private void sortSubNavs(){
Collections.sort(subNav, new Comparator<WANavigation>() {
@Override
public int compare(WANavigation o1, WANavigation o2) {
if (o1.weight == o2.weight)
return o1.name.compareToIgnoreCase(o2.name);
return o1.weight - o2.weight;
}
});
}
@Override
public Iterator iterator() {
return subNav.iterator();
}
public String getName(){
return name;
}
public String getUrl(){
return url;
}
private void setParentNav(WANavigation nav){
this.parentNav = nav;
}
/**
* Assign a resource object specific to this navigation object.
* This can be used if target page needs some additional information.
*/
public void setResource(Object obj){
resource = obj;
}
public Object getResource(){
return resource;
}
/**
* Sets the weight of this navigation object. The weight is
* used for deciding the order the parent will sort all sub navigation.
* Lower values will be at the top of sub-nav list.
*/
public void setWeight(int weightOrder){
this.weight = weightOrder;
if(parentNav != null)
parentNav.sortSubNavs();
}
@Override
public boolean equals(Object o){
if(o instanceof String)
return this.name.equals(o);
return this == o ||
(o != null && this.id == (((WANavigation)o).id));
}
/**
* Will create a clone of the navigation tree with some request instance specific information
*/
public NavInstance createNavInstance(Map<String, String> request){
return createNavInstance(getBreadcrumb(request));
}
private NavInstance createNavInstance(List<WANavigation> activeList){
NavInstance instance = new NavInstance(this);
instance.setActive(activeList.contains(this));
for (WANavigation nav : subNav)
instance.addSubNav(nav.createNavInstance(activeList));
return instance;
}
public static WANavigation createRootNav(){
return new WANavigation(null);
}
public static WANavigation getRootNav(Map<String, String> request) {
List<WANavigation> breadcrumb = getBreadcrumb(request);
if (!breadcrumb.isEmpty())
return breadcrumb.get(0);
return null;
}
/**
* @return the specific WANavigation object requested by client
*/
public static WANavigation getNavigation(Map<String, String> request) {
if(request.containsKey(NAVIGATION_URL_KEY))
return navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY)));
return null;
}
/**
* @param request A map of all url parameters sent from client
* @return a List of WANavigation objects depicting the navigation hierarchy for the
* requested page from the client. First entry will be the root navigation object.
*/
public static List<WANavigation> getBreadcrumb(Map<String, String> request) {
LinkedList list = new LinkedList();
WANavigation current = getNavigation(request);
if (current != null){
while(current != null){
list.addFirst(current);
current = current.parentNav;
}
}
return list;
}
public static class NavInstance{
private WANavigation nav;
private boolean active;
private ArrayList<NavInstance> subNavs;
protected NavInstance(WANavigation nav){
this.nav = nav;
this.subNavs = new ArrayList<>();
}
protected void setActive(boolean active){
this.active = active;
}
protected void addSubNav(NavInstance subNav){
subNavs.add(subNav);
}
public boolean isActive(){
return active;
}
public List<NavInstance> getSubNavs() { return subNavs; }
// Mirror getters from WANavigation
public String getName(){ return nav.getName(); }
public String getUrl(){ return nav.getUrl(); }
public Object getResource(){ return nav.getResource(); }
public boolean equals(Object o){
if (o instanceof WANavigation)
return nav.equals(o);
else if (o instanceof NavInstance)
return nav.equals(((NavInstance) o).nav);
return false;
}
}
}