From 2fb862090f5c38169c5cb53db4ff90f572270059 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 20 Jun 2013 03:04:07 +0200 Subject: [PATCH] Clean code. --- src/fr/devinsy/kiss4web/CookieHelper.java | 149 +++--- src/fr/devinsy/kiss4web/Page.java | 12 +- src/fr/devinsy/kiss4web/PageManager.java | 441 +++++++++--------- .../devinsy/kiss4web/ServletDispatcher.java | 71 +-- .../kiss4web/SimpleServletDispatcher.java | 67 +-- src/fr/devinsy/kiss4web/security/Group.java | 37 +- src/fr/devinsy/kiss4web/security/Groups.java | 104 ++--- .../kiss4web/security/GroupsFileReader.java | 36 +- .../kiss4web/security/SecurityAgent.java | 117 +++-- src/fr/devinsy/kiss4web/security/User.java | 30 +- src/fr/devinsy/kiss4web/security/Users.java | 74 ++- .../kiss4web/security/UsersFileReader.java | 41 +- src/fr/devinsy/util/web/FileItemHelper.java | 59 ++- src/fr/devinsy/util/web/Redirector.java | 12 +- .../devinsy/util/web/SimpleSecurityAgent.java | 159 +++---- tests/FooTester.java | 70 ++- 16 files changed, 721 insertions(+), 758 deletions(-) diff --git a/src/fr/devinsy/kiss4web/CookieHelper.java b/src/fr/devinsy/kiss4web/CookieHelper.java index 49328bb..5e8aad5 100644 --- a/src/fr/devinsy/kiss4web/CookieHelper.java +++ b/src/fr/devinsy/kiss4web/CookieHelper.java @@ -4,35 +4,17 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - /** * */ public class CookieHelper { - static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (CookieHelper.class); + static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CookieHelper.class); /** * */ - static public Cookie buildCookie (String name, String value, int duration, boolean isSecure) - { - Cookie result; - - result = new Cookie (name, value); - result.setMaxAge (duration); - result.setPath ("/"); - result.setSecure (isSecure); - - // - return (result); - } - - - /** - * - */ - static public Cookie buildCookie (String name, String value, int duration) + static public Cookie buildCookie(final String name, final String value, final int duration) { Cookie result; @@ -42,11 +24,46 @@ public class CookieHelper return (result); } + /** + * + */ + static public Cookie buildCookie(final String name, final String value, final int duration, final boolean isSecure) + { + Cookie result; + + result = new Cookie(name, value); + result.setMaxAge(duration); + result.setPath("/"); + result.setSecure(isSecure); + + // + return (result); + } /** * */ - static public Cookie getCookie (Cookie[] cookies, String key) + static public boolean exists(final HttpServletRequest request, final String key) + { + boolean result; + + if (getCookieValue(request, key) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } + + /** + * + */ + static public Cookie getCookie(final Cookie[] cookies, final String key) { Cookie result = null; @@ -62,7 +79,7 @@ public class CookieHelper { if (cookieCounter < cookies.length) { - if (key.equals (cookies[cookieCounter].getName ())) + if (key.equals(cookies[cookieCounter].getName())) { ended = true; result = cookies[cookieCounter]; @@ -84,30 +101,28 @@ public class CookieHelper return (result); } + /** + * + */ + static public Cookie getCookie(final HttpServletRequest request, final String key) + { + Cookie result = null; + + result = getCookie(request.getCookies(), key); + + // + return (result); + } /** * */ - static public Cookie getCookie (HttpServletRequest request, String key) - { - Cookie result = null; - - result = getCookie(request.getCookies (), key); - - // - return(result); - } - - - /** - * - */ - static public Object getCookieValue (Cookie[] cookies, String key) + static public Object getCookieValue(final Cookie[] cookies, final String key) { Object result; - + Cookie cookie = getCookie(cookies, key); - + if (cookie == null) { result = null; @@ -116,61 +131,37 @@ public class CookieHelper { result = cookie.getValue(); } - + // - return(result); + return (result); } - /** * */ - static public Object getCookieValue (HttpServletRequest request, String key) + static public Object getCookieValue(final HttpServletRequest request, final String key) { Object result; - - result = getCookieValue (request.getCookies (), key); - + + result = getCookieValue(request.getCookies(), key); + // - return(result); + return (result); } - /** * */ - static public boolean exists(HttpServletRequest request, String key) - { - boolean result; - - if (getCookieValue(request, key) == null) - { - result = false; - } - else - { - result = true; - } - - // - return(result); - } - - - /** - * - */ - static public void set (HttpServletResponse response, String name, String value, int duration) - { - response.addCookie (buildCookie(name, value, duration)); - } - - - /** - * - */ - static public void reset (HttpServletResponse response, String key) + static public void reset(final HttpServletResponse response, final String key) { response.addCookie(buildCookie(key, "", 0)); } + + /** + * + */ + static public void set(final HttpServletResponse response, final String name, final String value, final int duration) + { + response.addCookie(buildCookie(name, value, duration)); + } } \ No newline at end of file diff --git a/src/fr/devinsy/kiss4web/Page.java b/src/fr/devinsy/kiss4web/Page.java index 528b1a8..6098519 100644 --- a/src/fr/devinsy/kiss4web/Page.java +++ b/src/fr/devinsy/kiss4web/Page.java @@ -1,18 +1,18 @@ package fr.devinsy.kiss4web; -import java.io.*; -import javax.servlet.*; -import javax.servlet.http.*; +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * */ public interface Page { - /** + /** * */ - public void doIt (HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException; + public void doIt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; } diff --git a/src/fr/devinsy/kiss4web/PageManager.java b/src/fr/devinsy/kiss4web/PageManager.java index 6824fd4..dc9c973 100644 --- a/src/fr/devinsy/kiss4web/PageManager.java +++ b/src/fr/devinsy/kiss4web/PageManager.java @@ -1,9 +1,16 @@ package fr.devinsy.kiss4web; -import java.io.*; -import javax.servlet.*; -import javax.servlet.http.*; -import fr.devinsy.kiss4web.security.*; +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import fr.devinsy.kiss4web.security.SecurityAgent; +import fr.devinsy.kiss4web.security.User; /** * @@ -14,19 +21,98 @@ public class PageManager extends HttpServlet private static PageManager instance = null; protected SecurityAgent securityAgent; - static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (PageManager.class); + static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(PageManager.class); /** * */ - public void init () throws ServletException + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException + { + logger.info("=================================================="); + logger.info("getContextPath=[" + request.getContextPath() + "]"); + logger.info("getPathInfo=[" + request.getPathInfo() + "]"); + logger.info("getPathTranslated=[" + request.getPathTranslated() + "]"); + logger.info("getQueryString=[" + request.getQueryString() + "]"); + logger.info("getRequestURI=[" + request.getRequestURI() + "]"); + logger.info("getRequestURL=[" + request.getRequestURL() + "]"); + logger.info("getServletPath=[" + request.getServletPath() + "]"); + + String className = buildClassName(request.getPathInfo()); + logger.info("className=" + className); + + Page page = this.instanciatePage("site." + className); + + if (page == null) + { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + + out.println("Unknow page."); + } + else + { + HttpSession session = request.getSession(false); + String login; + if (session == null) + { + login = null; + } + else + { + login = (String) session.getAttribute("login"); + } + + if (this.securityAgent.checkPermission(request.getPathInfo(), login)) + { + page.doIt(request, response); + logger.info("securityAgent say 'permission OK': (" + login + ", " + request.getPathInfo() + ")"); + } + else + { + logger.info("securityAgent say 'permission KO': (" + login + ", " + request.getPathInfo() + ")"); + + if (login == null) + { + response.sendRedirect("/gestion/login.xhtml"); + } + else + { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + + out.println(""); + out.println("Permission denied."); + out.println("
"); + out.println(" "); + out.println("
"); + out.println(""); + } + } + } + } + + /** + * + */ + @Override + public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException + { + doGet(request, response); + } + + /** + * + */ + @Override + public void init() throws ServletException { // instance = this; try { - this.securityAgent = new SecurityAgent (getInitParameter ("securityDataPath")); + this.securityAgent = new SecurityAgent(getInitParameter("securityDataPath")); } catch (Exception exception) { @@ -35,46 +121,44 @@ public class PageManager extends HttpServlet } /** - * */ - public static PageManager instance () + public Page instanciatePage(final String className) { - return instance; - } + Page result; - /** - * - */ - static public String buildClassName (String pathInfo) - { - String result; - result = null; - - if ( pathInfo.equals ("/")) + Class pageClass = null; + try { - result = "Accueil"; + pageClass = (Class) Class.forName(className); + } + catch (java.lang.ClassNotFoundException exception) + { + result = null; + } + + logger.info("class=" + pageClass); + + if (pageClass == null) + { + result = null; + logger.error("Unknow page: (" + className + ")"); } else { - String[] tokens = pathInfo.split ("/"); - StringBuffer name = new StringBuffer (); - - for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++) + try { - name.append (tokens[tokenCounter]); - name.append ('.'); + result = pageClass.newInstance(); } - - if (pathInfo.endsWith ("/")) + catch (java.lang.InstantiationException exception) { - name.append (tokens[tokens.length - 1]); - name.append ('.'); + logger.error("Can't instanciate page (" + className + ")"); + result = null; + } + catch (java.lang.IllegalAccessException exception) + { + logger.error("(2) Can't instanciate page (" + className + ")"); + result = null; } - - logger.info ("==>[" + tokens[tokens.length - 1] + "]"); - name.append (formatClassName (tokens[tokens.length - 1])); - - result = name.toString (); } // @@ -84,20 +168,72 @@ public class PageManager extends HttpServlet /** * */ - static public String buildClassName2 (String pathInfo) + public SecurityAgent securityAgent() + { + SecurityAgent result; + + result = this.securityAgent; + + // + return (result); + } + + /** + * + */ + static public String buildClassName(final String pathInfo) + { + String result; + result = null; + + if (pathInfo.equals("/")) + { + result = "Accueil"; + } + else + { + String[] tokens = pathInfo.split("/"); + StringBuffer name = new StringBuffer(); + + for (int tokenCounter = 1; tokenCounter < tokens.length - 1; tokenCounter++) + { + name.append(tokens[tokenCounter]); + name.append('.'); + } + + if (pathInfo.endsWith("/")) + { + name.append(tokens[tokens.length - 1]); + name.append('.'); + } + + logger.info("==>[" + tokens[tokens.length - 1] + "]"); + name.append(formatClassName(tokens[tokens.length - 1])); + + result = name.toString(); + } + + // + return (result); + } + + /** + * + */ + static public String buildClassName2(final String pathInfo) { String result; - if (pathInfo.endsWith (".xhtml")) + if (pathInfo.endsWith(".xhtml")) { - char[] source = pathInfo.toCharArray (); + char[] source = pathInfo.toCharArray(); - StringBuffer out = new StringBuffer (); + StringBuffer out = new StringBuffer(); for (char c : source) { - out.append ("[" + c + "]"); + out.append("[" + c + "]"); } - logger.debug (out.toString ()); + logger.debug(out.toString()); char[] target = new char[source.length - 7]; int lastStartToken = 0; @@ -112,7 +248,7 @@ public class PageManager extends HttpServlet break; case '.': - target[lastStartToken] = Character.toUpperCase (target[lastStartToken]); + target[lastStartToken] = Character.toUpperCase(target[lastStartToken]); break; default: @@ -120,29 +256,29 @@ public class PageManager extends HttpServlet } } - out = new StringBuffer (); + out = new StringBuffer(); for (char c : target) { - out.append ("[" + c + "]"); + out.append("[" + c + "]"); } - logger.debug (out.toString ()); + logger.debug(out.toString()); - result = new String (target); + result = new String(target); } - else if (pathInfo.equals ("/")) + else if (pathInfo.equals("/")) { result = "Accueil"; } - else if (pathInfo.endsWith ("/")) + else if (pathInfo.endsWith("/")) { - char[] source = pathInfo.toCharArray (); + char[] source = pathInfo.toCharArray(); - StringBuffer out = new StringBuffer (); + StringBuffer out = new StringBuffer(); for (char c : source) { - out.append ("[" + c + "]"); + out.append("[" + c + "]"); } - logger.debug (out.toString ()); + logger.debug(out.toString()); char[] target = new char[source.length - 2]; int lastStartToken = 0; @@ -161,9 +297,9 @@ public class PageManager extends HttpServlet } } - char[] targetPlus = new char [source.length - lastStartToken]; + char[] targetPlus = new char[source.length - lastStartToken]; targetPlus[0] = '.'; - targetPlus[1] = Character.toUpperCase (source[lastStartToken]); + targetPlus[1] = Character.toUpperCase(source[lastStartToken]); int index = 2; for (int nChar = lastStartToken + 1; nChar < source.length - 1; nChar++) { @@ -171,24 +307,24 @@ public class PageManager extends HttpServlet index += 1; } - out = new StringBuffer (); + out = new StringBuffer(); for (char c : target) { - out.append ("[" + c + "]"); + out.append("[" + c + "]"); } - logger.debug (out.toString ()); - out = new StringBuffer (); + logger.debug(out.toString()); + out = new StringBuffer(); for (char c : targetPlus) { - out.append ("[" + c + "]"); + out.append("[" + c + "]"); } - logger.debug (out.toString ()); + logger.debug(out.toString()); - result = new String (target) + new String (targetPlus); + result = new String(target) + new String(targetPlus); } else { - logger.debug ("unknow case"); + logger.debug("unknow case"); result = null; } @@ -196,180 +332,39 @@ public class PageManager extends HttpServlet return (result); } - - /** - */ - public Page instanciatePage (String className) - { - Page result; - - Class pageClass = null; - try - { - pageClass = (Class) Class.forName (className); - } - catch (java.lang.ClassNotFoundException exception) - { - result = null; - } - - logger.info ("class=" + pageClass); - - if (pageClass == null) - { - result = null; - logger.error ("Unknow page: (" + className + ")"); - } - else - { - try - { - result = pageClass.newInstance (); - } - catch (java.lang.InstantiationException exception) - { - logger.error ("Can't instanciate page (" + className + ")"); - result = null; - } - catch (java.lang.IllegalAccessException exception) - { - logger.error ("(2) Can't instanciate page (" + className + ")"); - result = null; - } - } - - // - return (result); - } - /** * */ - public void doGet (HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException - { - logger.info ("=================================================="); - logger.info ("getContextPath=[" + request.getContextPath () + "]"); - logger.info ("getPathInfo=[" + request.getPathInfo () + "]"); - logger.info ("getPathTranslated=[" + request.getPathTranslated () + "]"); - logger.info ("getQueryString=[" + request.getQueryString () + "]"); - logger.info ("getRequestURI=[" + request.getRequestURI () + "]"); - logger.info ("getRequestURL=[" + request.getRequestURL () + "]"); - logger.info ("getServletPath=[" + request.getServletPath () + "]"); - - String className = buildClassName (request.getPathInfo ()); - logger.info ("className=" + className); - - Page page = this.instanciatePage ("site." + className); - - if (page == null) - { - response.setContentType ("text/html"); - PrintWriter out = response.getWriter(); - - out.println ("Unknow page."); - } - else - { - HttpSession session = request.getSession (false); - String login; - if (session == null) - { - login = null; - } - else - { - login = (String) session.getAttribute ("login"); - } - - if (this.securityAgent.checkPermission (request.getPathInfo (), login)) - { - page.doIt (request, response); - logger.info ("securityAgent say 'permission OK': (" + login + ", " + request.getPathInfo () + ")"); - } - else - { - logger.info ("securityAgent say 'permission KO': (" + login + ", " + request.getPathInfo () + ")"); - - if (login == null) - { - response.sendRedirect ("/gestion/login.xhtml"); - } - else - { - response.setContentType ("text/html"); - PrintWriter out = response.getWriter(); - - out.println (""); - out.println ("Permission denied."); - out.println ("
"); - out.println (" "); - out.println ("
"); - out.println (""); - } - } - } - } - - - /** - * - */ - public void doPost (HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException - { - doGet (request, response); - } - - - /** - * - */ - static public String formatClassName (String name) + static public String formatClassName(final String name) { String result; result = null; - String[] splittedLastToken = name.split ("\\."); + String[] splittedLastToken = name.split("\\."); String last = splittedLastToken[0]; - //logger.info ("last=" + last); + // logger.info ("last=" + last); - String[] tokens = last.split ("_"); - StringBuffer all = new StringBuffer (); + String[] tokens = last.split("_"); + StringBuffer all = new StringBuffer(); for (String token : tokens) { - //logger.info ("tok=" + token); + // logger.info ("tok=" + token); - all.append (Character.toUpperCase (token.charAt (0))); - all.append (token.substring (1)); + all.append(Character.toUpperCase(token.charAt(0))); + all.append(token.substring(1)); } - result = all.toString (); + result = all.toString(); // return (result); } - /** * */ - public SecurityAgent securityAgent () - { - SecurityAgent result; - - result = this.securityAgent; - - // - return (result); - } - - - /** - * - */ - static public User getUserFromSession (HttpServletRequest request) + static public User getUserFromSession(final HttpServletRequest request) { User result; @@ -379,7 +374,7 @@ public class PageManager extends HttpServlet } else { - HttpSession session = request.getSession (false); + HttpSession session = request.getSession(false); String login; if (session == null) @@ -388,12 +383,20 @@ public class PageManager extends HttpServlet } else { - login = (String) session.getAttribute ("login"); - result = PageManager.instance ().securityAgent ().users ().getByLogin (login); - } + login = (String) session.getAttribute("login"); + result = PageManager.instance().securityAgent().users().getByLogin(login); + } } // - return (result); + return (result); + } + + /** + * + */ + public static PageManager instance() + { + return instance; } } diff --git a/src/fr/devinsy/kiss4web/ServletDispatcher.java b/src/fr/devinsy/kiss4web/ServletDispatcher.java index 8bb54fa..dd3b943 100755 --- a/src/fr/devinsy/kiss4web/ServletDispatcher.java +++ b/src/fr/devinsy/kiss4web/ServletDispatcher.java @@ -6,11 +6,13 @@ package fr.devinsy.kiss4web; -import java.io.*; -import javax.servlet.*; -import javax.servlet.http.*; - +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @@ -19,47 +21,37 @@ public class ServletDispatcher extends SimpleServletDispatcher { private static final long serialVersionUID = -3471226305721330069L; protected org.apache.log4j.Logger logger; - //protected Servlets servlets; - - + + // protected Servlets servlets; + /** * */ - public void init () throws ServletException + @Override + public void doIt(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { - super.init(); - this.logger = org.apache.log4j.Logger.getLogger (this.getClass()); - //this.servlets = new Servlets(); - } + logger.info("=================================================="); + logger.info("getContextPath=[" + request.getContextPath() + "]"); + logger.info("getPathInfo=[" + request.getPathInfo() + "]"); + logger.info("getPathTranslated=[" + request.getPathTranslated() + "]"); + logger.info("getQueryString=[" + request.getQueryString() + "]"); + logger.info("getRequestURI=[" + request.getRequestURI() + "]"); + logger.info("getRequestURL=[" + request.getRequestURL() + "]"); + logger.info("getServletPath=[" + request.getServletPath() + "]"); - - /** - * - */ - public void doIt (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException - { - logger.info ("=================================================="); - logger.info ("getContextPath=[" + request.getContextPath () + "]"); - logger.info ("getPathInfo=[" + request.getPathInfo () + "]"); - logger.info ("getPathTranslated=[" + request.getPathTranslated () + "]"); - logger.info ("getQueryString=[" + request.getQueryString () + "]"); - logger.info ("getRequestURI=[" + request.getRequestURI () + "]"); - logger.info ("getRequestURL=[" + request.getRequestURL () + "]"); - logger.info ("getServletPath=[" + request.getServletPath () + "]"); + String className = pathInfoToClassName(request.getPathInfo()); + logger.info("className=" + className); - String className = pathInfoToClassName (request.getPathInfo ()); - logger.info ("className=" + className); - - HttpServlet servlet = instanciateServlet (className); + HttpServlet servlet = instanciateServlet(className); if (servlet == null) { - response.setContentType ("text/html"); + response.setContentType("text/html"); PrintWriter out = response.getWriter(); - out.println (""); - out.println ("Unknow page."); - out.println (""); + out.println(""); + out.println("Unknow page."); + out.println(""); out.close(); } @@ -68,4 +60,15 @@ public class ServletDispatcher extends SimpleServletDispatcher servlet.service(request, response); } } + + /** + * + */ + @Override + public void init() throws ServletException + { + super.init(); + this.logger = org.apache.log4j.Logger.getLogger(this.getClass()); + // this.servlets = new Servlets(); + } } diff --git a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java index c32eb48..6916ef1 100755 --- a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java +++ b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java @@ -55,20 +55,16 @@ public class SimpleServletDispatcher extends HttpServlet /* 30 */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '-', '-', '-', '-', '-', /* 40 */'\u0040', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', /* 50 */'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-', '-', '-', '-', '-', - /* 60 */'-', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', - '\u006F', - /* 70 */'\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', - '-', '-', - /* 80 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, - /* 90 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, - /* A0 */'\u00A0', '\u00A1', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', - '\u00AE', '\u00AF', - /* B0 */'-', '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u00BA', '\u00BB', '\u00BC', '\u00BD', - '\u00BE', '\u00BF', - /* C0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', - /* D0 */'\u00D0', '\u00D1', 'o', 'o', 'o', 'o', 'o', 'o', '\u00D8', 'u', 'u', 'u', 'u', 'y', '\u00DE', '\u00DF', - /* E0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', - /* F0 */'o', 'n', 'o', 'o', 'o', 'o', 'o', '\u00F7', '-', 'u', 'u', 'u', 'u', 'y', '-', 'y' }; + /* 60 */'-', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', '\u006F', + /* 70 */'\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '-', '-', + /* 80 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + /* 90 */NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE, + /* A0 */'\u00A0', '\u00A1', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', '\u00AE', '\u00AF', + /* B0 */'-', '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u00BA', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u00BF', + /* C0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', + /* D0 */'\u00D0', '\u00D1', 'o', 'o', 'o', 'o', 'o', 'o', '\u00D8', 'u', 'u', 'u', 'u', 'y', '\u00DE', '\u00DF', + /* E0 */'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', + /* F0 */'o', 'n', 'o', 'o', 'o', 'o', 'o', '\u00F7', '-', 'u', 'u', 'u', 'u', 'y', '-', 'y' }; /** * @@ -250,8 +246,14 @@ public class SimpleServletDispatcher extends HttpServlet } /** - * Extract values from a path. Example: "/article-/123/doors/open.xhtml" => - * "123", "doors" and "open". + * Extract values from a path. + * + * Example: + * + *
+	 * "/article-/123/doors/open.xhtml";
+	 * => "123", "doors" and "open".
+	 * 
*/ static public String[] longRewritedUrlParameters(final String path) { @@ -266,13 +268,18 @@ public class SimpleServletDispatcher extends HttpServlet /** * Convert a path in a class name, using easy conventions. * - * "/" => "Index_xhtml" "/good/" => "good.Good_xhtml" "/good/morning.xhtml" - * => "good.Morning_xhtml" "/good/morning_girl.xhtml" => - * "good.Morning_girl_xhtml" "/good/morning-123.xhtml" => - * "good.Morning_xhtml" ('123' is detected as a parameter, it will be - * decoded in the class called later). "/good/morning-/12/toto.jpg" => - * "good.Morning" ('12' and 'toto.jpg" are detected as a parameter, they + *
+	 * "/" 
+	 * => "Index_xhtml" "/good/"
+	 * => "good.Good_xhtml" "/good/morning.xhtml"
+	 * => "good.Morning_xhtml" "/good/morning_girl.xhtml"
+	 * => "good.Morning_girl_xhtml" "/good/morning-123.xhtml"
+	 * => "good.Morning_xhtml" ('123' is detected as a parameter, it will be
+	 * decoded in the class called later). "/good/morning-/12/toto.jpg"
+	 * => "good.Morning" ('12' and 'toto.jpg" are detected as a parameter, they
 	 * will be decoded in the class called later).
+	 * 
+ * */ static public String pathInfoToClassName(final String pathInfo) { @@ -480,9 +487,13 @@ public class SimpleServletDispatcher extends HttpServlet * Sometimes, URL has to be rewrited because we need to put parameter in the * page name. * - * Example: "/good/give_file?id=123&filename=foo.jpg" => - * rewriteShorturl("/good/give_file", "123", "foo.jpg"); => - * "/good/give_file-/123/foo.jpg" + * Example: + * + *
+	 *  "/good/give_file?id=123&filename=foo.jpg"
+	 *  => rewriteShorturl("/good/give_file", "123", "foo.jpg");
+	 *  => "/good/give_file-/123/foo.jpg"
+	 * 
* * Note: "-/" is used to indicate the start of parameters. * @@ -557,9 +568,13 @@ public class SimpleServletDispatcher extends HttpServlet * Sometimes, URL has to be rewrited because we need to put parameter in the * page name. * - * Example: "/good/article.xhtm?id=123&class=today&title=story's about me" + * Example: + * + *
+	 * "/good/article.xhtm?id=123&class=today&title=story's about me"
 	 * => rewriteShorturl("/good/article", "xhtml", "123", "Story's aboute me");
 	 * => "/good/article-123-today-story-s-about-me.xhtml"
+	 * 
*/ static public String rewriteShortUrl(final String uri, final String extension, final String... parameters) { diff --git a/src/fr/devinsy/kiss4web/security/Group.java b/src/fr/devinsy/kiss4web/security/Group.java index 59749e0..620b100 100644 --- a/src/fr/devinsy/kiss4web/security/Group.java +++ b/src/fr/devinsy/kiss4web/security/Group.java @@ -1,7 +1,6 @@ package fr.devinsy.kiss4web.security; - -import java.util.*; +import java.util.Vector; /** * @@ -11,47 +10,43 @@ public class Group protected String name; protected Vector members; - /** * */ - public Group () + public Group() { this.name = null; - this.members = new Vector (); + this.members = new Vector(); } - /** * */ - public void addMember (String login) + public void addMember(final String login) { - if ((login != null) && (login.length () != 0)) + if ((login != null) && (login.length() != 0)) { - this.members.add (login); - } + this.members.add(login); + } } - /** * */ - public boolean contains (String name) + public boolean contains(final String name) { boolean result = false; - result = this.members.contains (name); + result = this.members.contains(name); // - return (result); + return (result); } - /** * */ - public Vector members () + public Vector members() { Vector result; @@ -61,11 +56,10 @@ public class Group return (result); } - /** * */ - public String name () + public String name() { String result; @@ -75,11 +69,10 @@ public class Group return (result); } - /** * */ - public Group setName (String name) + public Group setName(final String name) { this.name = name; @@ -87,11 +80,11 @@ public class Group return (this); } - /** * */ - public String toString () + @Override + public String toString() { String result; diff --git a/src/fr/devinsy/kiss4web/security/Groups.java b/src/fr/devinsy/kiss4web/security/Groups.java index 1cfdfd1..b5fba2a 100644 --- a/src/fr/devinsy/kiss4web/security/Groups.java +++ b/src/fr/devinsy/kiss4web/security/Groups.java @@ -1,7 +1,7 @@ package fr.devinsy.kiss4web.security; - -import java.util.*; +import java.util.Iterator; +import java.util.Vector; /** * @@ -13,16 +13,35 @@ public class Groups extends Vector /** * */ - public Groups () + public Groups() { - super (); + super(); } + /* + * + */ + public boolean contains(final String name) + { + boolean result; + + if (get(name) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } /** * */ - public Group get (String name) + public Group get(final String name) { Group result; @@ -34,13 +53,13 @@ public class Groups extends Vector { result = null; boolean ended = false; - Iterator iterator = this.iterator (); + Iterator iterator = this.iterator(); while (!ended) { - if (iterator.hasNext ()) + if (iterator.hasNext()) { - Group group = iterator.next (); - if (group.name ().equals (name)) + Group group = iterator.next(); + if (group.name().equals(name)) { ended = true; result = group; @@ -54,49 +73,27 @@ public class Groups extends Vector } } - // - return (result); - } - - - /* - * - */ - public boolean contains (String name) - { - boolean result; - - if (get (name) == null) - { - result = false; - } - else - { - result = true; - } - // return (result); } - /** * */ - public Vector getLoginGroups (String login) + public Vector getLoginGroups(final String login) { Vector result; - result = new Vector (); - Iterator iterator = this.iterator (); + result = new Vector(); + Iterator iterator = this.iterator(); - while (iterator.hasNext ()) + while (iterator.hasNext()) { - Group group = iterator.next (); + Group group = iterator.next(); - if (group.members ().contains (login)) + if (group.members().contains(login)) { - result.add (group.name ()); + result.add(group.name()); } } @@ -104,56 +101,55 @@ public class Groups extends Vector return (result); } - /** * */ - public String getLoginGroupsString (String login) + public String getLoginGroupsString(final String login) { String result; - Vector groups = getLoginGroups (login); + Vector groups = getLoginGroups(login); - StringBuffer string = new StringBuffer (); + StringBuffer string = new StringBuffer(); for (String group : groups) { - if (string.length () == 0) + if (string.length() == 0) { - string.append (group); + string.append(group); } else { - string.append (","); - string.append (group); + string.append(","); + string.append(group); } } - result = string.toString (); + result = string.toString(); // return (result); } - /** * */ - public String toString () + @Override + public String toString() { String result; StringBuffer out; - out = new StringBuffer (); + out = new StringBuffer(); - Iterator iterator = this.iterator (); + Iterator iterator = this.iterator(); - while (iterator.hasNext ()) + while (iterator.hasNext()) { - out.append (iterator.next ().toString () + "\n"); + out.append(iterator.next().toString() + "\n"); } - result = out.toString (); + result = out.toString(); // return (result); diff --git a/src/fr/devinsy/kiss4web/security/GroupsFileReader.java b/src/fr/devinsy/kiss4web/security/GroupsFileReader.java index ab44853..45ec7e2 100644 --- a/src/fr/devinsy/kiss4web/security/GroupsFileReader.java +++ b/src/fr/devinsy/kiss4web/security/GroupsFileReader.java @@ -1,8 +1,8 @@ package fr.devinsy.kiss4web.security; -import java.io.*; -import java.util.*; - +import java.io.FileInputStream; +import java.util.Iterator; +import java.util.Properties; /** * @@ -12,43 +12,43 @@ public class GroupsFileReader /** * */ - static public Groups load (String fileName) throws Exception + static public Groups load(final String fileName) throws Exception { Groups result; - result = new Groups (); + result = new Groups(); Properties properties = new Properties(); try { - properties.load (new FileInputStream(fileName)); + properties.load(new FileInputStream(fileName)); - Iterator iterator = properties.keySet ().iterator (); - while (iterator.hasNext ()) + Iterator iterator = properties.keySet().iterator(); + while (iterator.hasNext()) { - String key = (String) iterator.next (); - String valueLine = (String) properties.get (key); + String key = (String) iterator.next(); + String valueLine = (String) properties.get(key); // - String[] values = valueLine.split (","); + String[] values = valueLine.split(","); - Group group = new Group (); - group.setName (key); + Group group = new Group(); + group.setName(key); for (String value : values) { - group.addMember (value.trim ()); + group.addMember(value.trim()); } - result.add (group); + result.add(group); } } catch (Exception exception) { - throw new Exception ("can't load (" + fileName + ")", exception); - } + throw new Exception("can't load (" + fileName + ")", exception); + } // - return (result); + return (result); } } diff --git a/src/fr/devinsy/kiss4web/security/SecurityAgent.java b/src/fr/devinsy/kiss4web/security/SecurityAgent.java index 72a926b..241b2ad 100644 --- a/src/fr/devinsy/kiss4web/security/SecurityAgent.java +++ b/src/fr/devinsy/kiss4web/security/SecurityAgent.java @@ -1,9 +1,6 @@ package fr.devinsy.kiss4web.security; - -import java.util.*; - - +import java.util.Iterator; /** * @@ -14,78 +11,37 @@ public class SecurityAgent protected Groups groups; protected Groups permissions; - /** * */ - public SecurityAgent (String path) throws Exception + public SecurityAgent(final String path) throws Exception { - this.users = UsersFileReader.load (path + "users.conf"); - this.groups = GroupsFileReader.load (path + "groups.conf"); - this.permissions = GroupsFileReader.load (path + "permissions.conf"); - } - - - /** - * - */ - public Groups groups () - { - Groups result; - - result = this.groups; - - // - return (result); + this.users = UsersFileReader.load(path + "users.conf"); + this.groups = GroupsFileReader.load(path + "groups.conf"); + this.permissions = GroupsFileReader.load(path + "permissions.conf"); } /** * */ - public Groups permissions () - { - Groups result; - - result = this.permissions; - - // - return (result); - } - - /** - * - */ - public Users users () - { - Users result; - - result = this.users; - - // - return (result); - } - - /** - * - */ - public boolean authenticate (String login, String password) + public boolean authenticate(final String login, final String password) { boolean result; - User user = this.users.getByLogin (login); + User user = this.users.getByLogin(login); if (user == null) { result = false; } - else if (user.password ().equals (password)) + else if (user.password().equals(password)) { result = true; } else { result = false; - } + } // return (result); @@ -94,11 +50,11 @@ public class SecurityAgent /** * */ - public boolean checkPermission (String url, String login) + public boolean checkPermission(final String url, final String login) { boolean result = false; - Group permitGroups = this.permissions.get (url); + Group permitGroups = this.permissions.get(url); if (permitGroups == null) { @@ -108,25 +64,25 @@ public class SecurityAgent { // boolean ended = false; - Iterator iterator = permitGroups.members ().iterator (); + Iterator iterator = permitGroups.members().iterator(); while (!ended) { - if (!iterator.hasNext ()) + if (!iterator.hasNext()) { ended = true; result = false; } else { - String groupName = iterator.next (); - if (groupName.equals ("*")) + String groupName = iterator.next(); + if (groupName.equals("*")) { result = true; ended = true; } else { - Group members = this.groups.get (groupName); + Group members = this.groups.get(groupName); if (members == null) { @@ -134,7 +90,7 @@ public class SecurityAgent } else { - if (members.contains (login)) + if (members.contains(login)) { ended = true; result = true; @@ -148,4 +104,43 @@ public class SecurityAgent // return (result); } + + /** + * + */ + public Groups groups() + { + Groups result; + + result = this.groups; + + // + return (result); + } + + /** + * + */ + public Groups permissions() + { + Groups result; + + result = this.permissions; + + // + return (result); + } + + /** + * + */ + public Users users() + { + Users result; + + result = this.users; + + // + return (result); + } } diff --git a/src/fr/devinsy/kiss4web/security/User.java b/src/fr/devinsy/kiss4web/security/User.java index 0054a75..450aecd 100644 --- a/src/fr/devinsy/kiss4web/security/User.java +++ b/src/fr/devinsy/kiss4web/security/User.java @@ -1,8 +1,5 @@ package fr.devinsy.kiss4web.security; - - - /** * */ @@ -13,11 +10,10 @@ public class User protected String realName; protected String email; - /** * */ - public User () + public User() { this.login = null; this.password = null; @@ -25,11 +21,10 @@ public class User this.email = null; } - /** * */ - public String email () + public String email() { String result; @@ -42,7 +37,7 @@ public class User /** * */ - public String login () + public String login() { String result; @@ -55,7 +50,7 @@ public class User /** * */ - public String password () + public String password() { String result; @@ -68,7 +63,7 @@ public class User /** * */ - public String realName () + public String realName() { String result; @@ -78,14 +73,13 @@ public class User return (result); } - /** * */ - public User setEmail (String email) + public User setEmail(final String email) { this.email = email; - + // return (this); } @@ -93,7 +87,7 @@ public class User /** * */ - public User setLogin (String login) + public User setLogin(final String login) { this.login = login; @@ -104,7 +98,7 @@ public class User /** * */ - public User setPassword (String password) + public User setPassword(final String password) { this.password = password; @@ -115,7 +109,7 @@ public class User /** * */ - public User setRealName (String realName) + public User setRealName(final String realName) { this.realName = realName; @@ -126,7 +120,8 @@ public class User /** * */ - public String toString () + @Override + public String toString() { String result; @@ -136,4 +131,3 @@ public class User return (result); } } - diff --git a/src/fr/devinsy/kiss4web/security/Users.java b/src/fr/devinsy/kiss4web/security/Users.java index 479852b..c42e30c 100644 --- a/src/fr/devinsy/kiss4web/security/Users.java +++ b/src/fr/devinsy/kiss4web/security/Users.java @@ -1,8 +1,7 @@ package fr.devinsy.kiss4web.security; - -import java.util.*; - +import java.util.Iterator; +import java.util.Vector; /** * @@ -11,20 +10,38 @@ public class Users extends Vector { private static final long serialVersionUID = 6140538630004281217L; - /** * */ - public Users () + public Users() { - super (); + super(); } + /* + * + */ + public boolean contains(final String login) + { + boolean result; + + if (getByLogin(login) == null) + { + result = false; + } + else + { + result = true; + } + + // + return (result); + } /** * */ - public User getByLogin (String login) + public User getByLogin(final String login) { User result; @@ -36,13 +53,13 @@ public class Users extends Vector { result = null; boolean ended = false; - Iterator iterator = this.iterator (); + Iterator iterator = this.iterator(); while (!ended) { - if (iterator.hasNext ()) + if (iterator.hasNext()) { - User user = iterator.next (); - if (user.login ().equals (login)) + User user = iterator.next(); + if (user.login().equals(login)) { ended = true; result = user; @@ -56,50 +73,29 @@ public class Users extends Vector } } - // - return (result); - } - - - /* - * - */ - public boolean contains (String login) - { - boolean result; - - if (getByLogin (login) == null) - { - result = false; - } - else - { - result = true; - } - // return (result); } - /** * */ - public String toString () + @Override + public String toString() { String result; StringBuffer out; - out = new StringBuffer (); + out = new StringBuffer(); - Iterator iterator = this.iterator (); + Iterator iterator = this.iterator(); - while (iterator.hasNext ()) + while (iterator.hasNext()) { - out.append (iterator.next ().toString () + "\n"); + out.append(iterator.next().toString() + "\n"); } - result = out.toString (); + result = out.toString(); // return (result); diff --git a/src/fr/devinsy/kiss4web/security/UsersFileReader.java b/src/fr/devinsy/kiss4web/security/UsersFileReader.java index eb07585..cc77cce 100644 --- a/src/fr/devinsy/kiss4web/security/UsersFileReader.java +++ b/src/fr/devinsy/kiss4web/security/UsersFileReader.java @@ -1,8 +1,8 @@ package fr.devinsy.kiss4web.security; -import java.io.*; -import java.util.*; - +import java.io.FileInputStream; +import java.util.Iterator; +import java.util.Properties; /** * @@ -12,44 +12,43 @@ public class UsersFileReader /** * */ - static public Users load (String fileName) throws Exception + static public Users load(final String fileName) throws Exception { Users result; - result = new Users (); + result = new Users(); Properties properties = new Properties(); try { - properties.load (new FileInputStream(fileName)); + properties.load(new FileInputStream(fileName)); - Iterator iterator = properties.keySet ().iterator (); - while (iterator.hasNext ()) + Iterator iterator = properties.keySet().iterator(); + while (iterator.hasNext()) { - String key = (String) iterator.next (); - String valueLine = (String) properties.get (key); + String key = (String) iterator.next(); + String valueLine = (String) properties.get(key); // - String[] values = valueLine.split (","); + String[] values = valueLine.split(","); - User user = new User (); - user.setLogin (key); - user.setPassword (values[0]); - user.setRealName (values[1]); - user.setEmail (values[2]); + User user = new User(); + user.setLogin(key); + user.setPassword(values[0]); + user.setRealName(values[1]); + user.setEmail(values[2]); - result.add (user); + result.add(user); // } } catch (Exception exception) { - throw new Exception ("can't load (" + fileName + ")", exception); - } - + throw new Exception("can't load (" + fileName + ")", exception); + } // - return (result); + return (result); } } diff --git a/src/fr/devinsy/util/web/FileItemHelper.java b/src/fr/devinsy/util/web/FileItemHelper.java index 67a520a..619275c 100644 --- a/src/fr/devinsy/util/web/FileItemHelper.java +++ b/src/fr/devinsy/util/web/FileItemHelper.java @@ -4,41 +4,18 @@ import java.util.List; import org.apache.commons.fileupload.FileItem; - /** * */ public class FileItemHelper { - //static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (FileItemHelper.class); + // static private org.apache.log4j.Logger logger = + // org.apache.log4j.Logger.getLogger (FileItemHelper.class); /** * List FileItem */ - static public String getItemValue (List items, String name) - { - String result; - - FileItem item = getItem(items, name); - - if (item == null) - { - result = null; - } - else - { - result = item.getString(); - } - - // - return(result); - } - - - /** - * List FileItem - */ - static public FileItem getItem (List items, String name) + static public FileItem getItem(final List items, final String name) { FileItem result; @@ -56,7 +33,7 @@ public class FileItemHelper if (itemIndex < items.size()) { FileItem item = (FileItem) items.get(itemIndex); - + if (name.equals(item.getFieldName())) { ended = true; @@ -74,9 +51,31 @@ public class FileItemHelper } } } - - // - return(result); + + // + return (result); + } + + /** + * List FileItem + */ + static public String getItemValue(final List items, final String name) + { + String result; + + FileItem item = getItem(items, name); + + if (item == null) + { + result = null; + } + else + { + result = item.getString(); + } + + // + return (result); } } diff --git a/src/fr/devinsy/util/web/Redirector.java b/src/fr/devinsy/util/web/Redirector.java index 2d10f70..ea43c88 100644 --- a/src/fr/devinsy/util/web/Redirector.java +++ b/src/fr/devinsy/util/web/Redirector.java @@ -2,24 +2,22 @@ package fr.devinsy.util.web; import javax.servlet.http.HttpServletResponse; - - /** * */ public class Redirector { - static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (Redirector.class); + static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Redirector.class); /** * */ - static public void redirect (HttpServletResponse response, String destination) + static public void redirect(final HttpServletResponse response, final String destination) { - logger.info ("Redirect to <" + destination + ">"); + logger.info("Redirect to <" + destination + ">"); - response.setHeader ("Location", destination); - response.setStatus (HttpServletResponse.SC_MOVED_TEMPORARILY); + response.setHeader("Location", destination); + response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); } } diff --git a/src/fr/devinsy/util/web/SimpleSecurityAgent.java b/src/fr/devinsy/util/web/SimpleSecurityAgent.java index 59dc55e..6cc6dcc 100644 --- a/src/fr/devinsy/util/web/SimpleSecurityAgent.java +++ b/src/fr/devinsy/util/web/SimpleSecurityAgent.java @@ -1,18 +1,16 @@ package fr.devinsy.util.web; -import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import fr.devinsy.kiss4web.CookieHelper; - /** * */ public class SimpleSecurityAgent { - static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (SimpleSecurityAgent.class); + static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SimpleSecurityAgent.class); final static public String USERID_LABEL = "securityAgent.userId"; final static public String ACCOUNTID_LABEL = "securityAgent.accountId"; final static public String AUTH_LABEL = "securityAgent.auth"; @@ -20,83 +18,48 @@ public class SimpleSecurityAgent protected String accountIdLabel; protected String authLabel; protected String secretKey; - - - + /** * */ - public SimpleSecurityAgent(String prefix, String secretKey) + public SimpleSecurityAgent(final String prefix, final String secretKey) { this.userIdLabel = prefix + "." + USERID_LABEL; this.accountIdLabel = prefix + "." + ACCOUNTID_LABEL; - this.authLabel= prefix + "." + AUTH_LABEL; + this.authLabel = prefix + "." + AUTH_LABEL; this.secretKey = secretKey; } - - - /** - * - */ - static String md5sum(String source) - { - String result; - - result = org.apache.commons.codec.digest.DigestUtils.md5Hex(source); - - // - return(result); - } - - /** * */ - public String userId(HttpServletRequest request) + public String accountId(final HttpServletRequest request) { String result; - - result = (String) CookieHelper.getCookieValue(request, this.userIdLabel); - - // - return(result); - } - - - /** - * - */ - public String accountId(HttpServletRequest request) - { - String result; - + result = (String) CookieHelper.getCookieValue(request, this.accountIdLabel); - + // - return(result); + return (result); } - - - + /** * */ - public String auth(HttpServletRequest request) + public String auth(final HttpServletRequest request) { String result; - + result = (String) CookieHelper.getCookieValue(request, this.authLabel); - + // - return(result); + return (result); } - /** * Check authentication and refresh it (reset countdown). */ - public boolean isAuthenticated (HttpServletRequest request, HttpServletResponse response) + public boolean isAuthenticated(final HttpServletRequest request, final HttpServletResponse response) { boolean result; @@ -104,7 +67,7 @@ public class SimpleSecurityAgent String userId = userId(request); String auth = auth(request); logger.info("cook=[" + auth + "]"); - + if (auth == null) { result = false; @@ -112,7 +75,7 @@ public class SimpleSecurityAgent else if (auth.equals(computeAuth(accountId, userId, request.getRemoteAddr(), this.secretKey))) { result = true; - + // Refresh cookies. setAuthenticated(request, response, accountId, userId); } @@ -122,47 +85,71 @@ public class SimpleSecurityAgent } // - return (result); + return (result); } - /** * */ - static public String computeAuth(String key1, String key2, String key3, String key4) - { - String result; - - result = md5sum(key1 + key2 + key3+ key4); - - // - return(result); - } - - /** - * - */ - public void setAuthenticated (HttpServletRequest request, HttpServletResponse response, String accountId, String userId) - { - // Refresh cookie. - int duration = 60*60; - String auth = computeAuth(String.valueOf(accountId), userId, request.getRemoteAddr(), this.secretKey); - - response.addCookie (CookieHelper.buildCookie(this.authLabel, auth, duration)); - response.addCookie (CookieHelper.buildCookie(this.accountIdLabel, accountId, duration)); - response.addCookie (CookieHelper.buildCookie(this.userIdLabel, userId, duration)); - - logger.info("set [" + auth + "," + accountId + "," + userId + "," + request.getRemoteAddr() + ")"); - } - - - /** - * - */ - public void reset(HttpServletRequest request, HttpServletResponse response) + public void reset(final HttpServletRequest request, final HttpServletResponse response) { CookieHelper.reset(response, this.authLabel); CookieHelper.reset(response, this.accountIdLabel); CookieHelper.reset(response, this.userIdLabel); } + + /** + * + */ + public void setAuthenticated(final HttpServletRequest request, final HttpServletResponse response, final String accountId, final String userId) + { + // Refresh cookie. + int duration = 60 * 60; + String auth = computeAuth(String.valueOf(accountId), userId, request.getRemoteAddr(), this.secretKey); + + response.addCookie(CookieHelper.buildCookie(this.authLabel, auth, duration)); + response.addCookie(CookieHelper.buildCookie(this.accountIdLabel, accountId, duration)); + response.addCookie(CookieHelper.buildCookie(this.userIdLabel, userId, duration)); + + logger.info("set [" + auth + "," + accountId + "," + userId + "," + request.getRemoteAddr() + ")"); + } + + /** + * + */ + public String userId(final HttpServletRequest request) + { + String result; + + result = (String) CookieHelper.getCookieValue(request, this.userIdLabel); + + // + return (result); + } + + /** + * + */ + static public String computeAuth(final String key1, final String key2, final String key3, final String key4) + { + String result; + + result = md5sum(key1 + key2 + key3 + key4); + + // + return (result); + } + + /** + * + */ + static String md5sum(final String source) + { + String result; + + result = org.apache.commons.codec.digest.DigestUtils.md5Hex(source); + + // + return (result); + } } \ No newline at end of file diff --git a/tests/FooTester.java b/tests/FooTester.java index d9ade54..c598806 100644 --- a/tests/FooTester.java +++ b/tests/FooTester.java @@ -2,8 +2,7 @@ * Kiss4Web tests. */ -import fr.devinsy.kiss4web.*; - +import fr.devinsy.kiss4web.ServletDispatcher; /** * @@ -13,78 +12,73 @@ class FooTester static private org.apache.log4j.Logger logger; static - { + { // Initialize logger. org.apache.log4j.Logger logger = null; - org.apache.log4j.BasicConfigurator.configure (); + org.apache.log4j.BasicConfigurator.configure(); + logger = org.apache.log4j.Logger.getRootLogger(); + // logger.setLevel (org.apache.log4j.Level.INFO); + logger.setLevel(org.apache.log4j.Level.INFO); - logger = org.apache.log4j.Logger.getRootLogger (); - //logger.setLevel (org.apache.log4j.Level.INFO); - logger.setLevel (org.apache.log4j.Level.INFO); - - logger.info ("Enter"); + logger.info("Enter"); // - logger.info ("Set the log file format..."); + logger.info("Set the log file format..."); + // log = + // org.apache.log4j.Category.getInstance(Application.class.getName()); + logger.info("... done."); - // log = org.apache.log4j.Category.getInstance(Application.class.getName()); - logger.info ("... done."); - - logger.debug ("Exit"); - logger = org.apache.log4j.Logger.getLogger (FooTester.class.getName ()); + logger.debug("Exit"); + logger = org.apache.log4j.Logger.getLogger(FooTester.class.getName()); } - - /** * */ - public static String check (String title, StringBuffer source, String model) + public static String check(final String title, final StringBuffer source, final String model) { String result; - if (source.indexOf (model) == -1) + if (source.indexOf(model) == -1) { - result = String.format ("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model; + result = String.format("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model; } else { - result = String.format ("%-40s [ OK ] ", title); + result = String.format("%-40s [ OK ] ", title); } - // return (result); } - - /** - * - */ - public static String testCaller (String pathInfo, String prefix) - { - String result; - - result = "[" + pathInfo + "]=>[" + ServletDispatcher.pathInfoToClassName(pathInfo, prefix) + "]"; - - // - return(result); - } - /** * */ - public static void main(String[] args) + public static void main(final String[] args) { - System.out.println ("----------------------------"); + System.out.println("----------------------------"); System.out.println(testCaller("/", "fr.devinsy.website")); System.out.println(testCaller("/good/", "fr.devinsy.website")); System.out.println(testCaller("/good/morning", "fr.devinsy.website")); System.out.println(testCaller("/good/day_day", "fr.devinsy.website")); System.out.println(testCaller("/good/day.xhtml", "fr.devinsy.website")); } + + /** + * + */ + public static String testCaller(final String pathInfo, final String prefix) + { + String result; + + result = "[" + pathInfo + "]=>[" + ServletDispatcher.pathInfoToClassName(pathInfo, prefix) + "]"; + + // + return (result); + } }