diff --git a/src/zutil/net/http/HttpHeader.java b/src/zutil/net/http/HttpHeader.java index 98acaea..cf66718 100755 --- a/src/zutil/net/http/HttpHeader.java +++ b/src/zutil/net/http/HttpHeader.java @@ -86,6 +86,22 @@ public class HttpHeader { public String getRequestURL(){ return url; } + /** + * @return parses out the page name from the request url and returns it. + */ + public String getRequestPage() { + if (url != null){ + int start = 0; + if (url.charAt(0) == '/') + start = 1; + int end = url.indexOf('?'); + if (end < 0) + end = url.length(); + + return url.substring(start, end); + } + return null; + } /** * @return a Iterator with all defined url keys */ @@ -187,4 +203,5 @@ public class HttpHeader { public String toStringAttributes(){ return Converter.toString(urlAttributes); } + } diff --git a/src/zutil/net/http/page/HttpFilePage.java b/src/zutil/net/http/page/HttpFilePage.java index e254b96..fc95072 100755 --- a/src/zutil/net/http/page/HttpFilePage.java +++ b/src/zutil/net/http/page/HttpFilePage.java @@ -142,7 +142,7 @@ public class HttpFilePage implements HttpPage{ out.setHeader("Cache-Control", "max-age=" + MAX_CACHE_AGE_SECONDS); out.setHeader("ETag", "\"" + eTag + "\""); - if (headers.getHeader("If-None-Match") != null && + if (eTag != null && headers.getHeader("If-None-Match") != null && eTag.equals(StringUtil.trimQuotes(headers.getHeader("If-None-Match")))){ // File has not changed out.setStatusCode(304); } else { diff --git a/src/zutil/net/http/page/HttpRedirectPage.java b/src/zutil/net/http/page/HttpRedirectPage.java new file mode 100755 index 0000000..9fc1839 --- /dev/null +++ b/src/zutil/net/http/page/HttpRedirectPage.java @@ -0,0 +1,48 @@ +package zutil.net.http.page; + +import zutil.net.http.HttpHeader; +import zutil.net.http.HttpPage; +import zutil.net.http.HttpPrintStream; + +import java.io.IOException; +import java.util.Map; + +/** + * Created by Ziver on 2016-06-19. + */ +public class HttpRedirectPage implements HttpPage{ + + private String redirectUrl; + + + public HttpRedirectPage(String redirectUrl){ + this.redirectUrl = redirectUrl; + } + + @Override + public void respond(HttpPrintStream out, + HttpHeader headers, + Map session, + Map cookie, + Map request) throws IOException { + + out.setStatusCode(301); + out.setHeader("Location", redirectUrl); + out.print( + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Page Redirection\n" + + " \n" + + " \n" + + " If you are not redirected automatically, follow the link to "+ redirectUrl +"\n" + + " \n" + + "" + ); + } +} diff --git a/src/zutil/ui/Navigation.java b/src/zutil/ui/Navigation.java index 120ec0f..7c95dc9 100755 --- a/src/zutil/ui/Navigation.java +++ b/src/zutil/ui/Navigation.java @@ -22,6 +22,8 @@ package zutil.ui; +import zutil.net.http.HttpHeader; + import java.util.*; /** @@ -45,21 +47,32 @@ public class Navigation implements Iterable{ private Navigation(String id, String name) { - this.id = id; - this.navMap.put(this.id, this); + if (id == null) this.id = ""+navMap.size(); + else this.id = id; + this.name = name; this.subNav = new ArrayList<>(); + + this.navMap.put(this.id, this); } - public List getSubNavs() { - return subNav; + /** + * Will return a existing Navigation or create a new one. + * + * @param name the nice String name of the destination + */ + public Navigation createSubNav(String name) { + return createSubNav(null, name); } /** - * Will create a new sub-nav if it does not already exist or return a existing one. + * Will return a existing Navigation or create a new one. + * + * @param id the unique id or page name + * @param name the nice String name of the destination */ public Navigation createSubNav(String id, String name) { - Navigation nav = getSubNav(name); + Navigation nav = getSubNav(id, name); if(nav != null) return nav; @@ -69,16 +82,6 @@ public class Navigation implements Iterable{ sortSubNavs(); return nav; } - /** - * Searches for and returns the specified sub-nav or returns null if it was not found. - */ - private Navigation getSubNav(String name) { - for(Navigation nav : subNav) { - if(nav.equals(name)) - return nav; - } - return null; - } private void sortSubNavs(){ Collections.sort(subNav, new Comparator() { @Override @@ -89,15 +92,23 @@ public class Navigation implements Iterable{ } }); } + /** + * Searches for and returns the specified sub-nav with the id or name. + * Returns null if no navigation object found. + */ + private Navigation getSubNav(String id, String name) { + for(Navigation nav : subNav) { + if(nav.equals(id) || nav.equals(name)) + return nav; + } + return null; + } @Override public Iterator iterator() { return subNav.iterator(); } - private void setParentNav(Navigation nav){ - this.parentNav = nav; - } public String getName(){ @@ -108,6 +119,10 @@ public class Navigation implements Iterable{ } + + private void setParentNav(Navigation nav){ + this.parentNav = nav; + } /** * Assign a resource object specific to this navigation object. * This can be used if target page needs some additional information. @@ -142,7 +157,7 @@ public class Navigation implements Iterable{ * Will create a clone of the navigation tree with some request instance specific information */ public NavInstance createParameterizedNavInstance(Map request){ - return createParameterizedNavInstance(getBreadcrumb(getNavigation(request))); + return createParameterizedNavInstance(getBreadcrumb(getParameterizedNavigation(request))); } private NavInstance createParameterizedNavInstance(List activeList){ NavInstance instance = new ParameterizedNavInstance(this); @@ -151,9 +166,21 @@ public class Navigation implements Iterable{ instance.addSubNav(nav.createParameterizedNavInstance(activeList)); return instance; } + /** + * @return the specific Navigation object requested by client + */ + public static Navigation getParameterizedNavigation(Map request) { + if(request.containsKey(NAVIGATION_URL_KEY)) + return navMap.get(request.get(NAVIGATION_URL_KEY)); + return null; + } + - public NavInstance createPagedNavInstance(Map request){ - return createPagedNavInstance(getBreadcrumb(getNavigation(request))); + public NavInstance createPagedNavInstance(HttpHeader header){ + Navigation nav = getPagedNavigation(header); + if (nav != null) + return createPagedNavInstance(getBreadcrumb(nav)); + return null; } private NavInstance createPagedNavInstance(List activeList){ NavInstance instance = new PagedNavInstance(this); @@ -162,6 +189,12 @@ public class Navigation implements Iterable{ instance.addSubNav(nav.createPagedNavInstance(activeList)); return instance; } + /** + * @return the specific Navigation object requested by client + */ + public static Navigation getPagedNavigation(HttpHeader header) { + return navMap.get(header.getRequestPage()); + } @@ -169,30 +202,19 @@ public class Navigation implements Iterable{ return new Navigation(null, null); } public static Navigation getRootNav(Map request) { - List breadcrumb = getBreadcrumb(getNavigation(request)); + List breadcrumb = getBreadcrumb(getParameterizedNavigation(request)); if (!breadcrumb.isEmpty()) return breadcrumb.get(0); return null; } + /** - * @return the specific Navigation object requested by client - */ - public static Navigation getNavigation(Map request) { - if(request.containsKey(NAVIGATION_URL_KEY)) - return navMap.get(Integer.parseInt(request.get(NAVIGATION_URL_KEY))); - return null; - } - - /** - * @param pageId the ID of the requested page + * @param nav the * @return a List of Navigation objects depicting the navigation hierarchy for the * requested page from the client. First entry will be the root navigation object. */ - public static List getBreadcrumb(String pageId) { - return getBreadcrumb(navMap.get(pageId)); - } - private static List getBreadcrumb(Navigation nav) { + public static List getBreadcrumb(Navigation nav) { LinkedList list = new LinkedList(); if (nav != null){ while(nav != null){ @@ -209,30 +231,30 @@ public class Navigation implements Iterable{ public abstract static class NavInstance{ protected Navigation nav; protected boolean active; - protected ArrayList subNavs; + protected ArrayList subNavInstance; protected NavInstance(Navigation nav){ this.nav = nav; - this.subNavs = new ArrayList<>(); + this.subNavInstance = new ArrayList<>(); } protected void setActive(boolean active){ this.active = active; } protected void addSubNav(NavInstance subNav){ - subNavs.add(subNav); + subNavInstance.add(subNav); } public boolean isActive(){ return active; } - public List getSubNavs() { return subNavs; } + public List getSubNavs() { return subNavInstance; } // Mirror getters from Navigation public String getName(){ return nav.getName(); } public Object getResource(){ return nav.getResource(); } - public abstract String getUrl(); + public abstract String getURL(); public boolean equals(Object o){ @@ -247,7 +269,7 @@ public class Navigation implements Iterable{ public static class ParameterizedNavInstance extends NavInstance{ protected ParameterizedNavInstance(Navigation nav) { super(nav); } - public String getUrl(){ + public String getURL(){ return "?"+NAVIGATION_URL_KEY+"="+nav.id; } } @@ -255,8 +277,8 @@ public class Navigation implements Iterable{ public static class PagedNavInstance extends NavInstance{ protected PagedNavInstance(Navigation nav) { super(nav); } - public String getUrl(){ - return nav.id; + public String getURL(){ + return "/" + nav.id; } } }