Clean code.

This commit is contained in:
Christian P. MOMON 2013-06-20 03:04:07 +02:00
parent e96e9ddf13
commit 2fb862090f
16 changed files with 721 additions and 758 deletions

View file

@ -4,7 +4,6 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
*/
@ -15,7 +14,20 @@ public class CookieHelper
/**
*
*/
static public Cookie buildCookie (String name, String value, int duration, boolean isSecure)
static public Cookie buildCookie(final String name, final String value, final int duration)
{
Cookie result;
result = buildCookie(name, value, duration, false);
//
return (result);
}
/**
*
*/
static public Cookie buildCookie(final String name, final String value, final int duration, final boolean isSecure)
{
Cookie result;
@ -28,25 +40,30 @@ public class CookieHelper
return (result);
}
/**
*
*/
static public Cookie buildCookie (String name, String value, int duration)
static public boolean exists(final HttpServletRequest request, final String key)
{
Cookie result;
boolean result;
result = buildCookie(name, value, duration, false);
if (getCookieValue(request, key) == null)
{
result = false;
}
else
{
result = true;
}
//
return (result);
}
/**
*
*/
static public Cookie getCookie (Cookie[] cookies, String key)
static public Cookie getCookie(final Cookie[] cookies, final String key)
{
Cookie result = null;
@ -84,11 +101,10 @@ public class CookieHelper
return (result);
}
/**
*
*/
static public Cookie getCookie (HttpServletRequest request, String key)
static public Cookie getCookie(final HttpServletRequest request, final String key)
{
Cookie result = null;
@ -98,11 +114,10 @@ public class CookieHelper
return (result);
}
/**
*
*/
static public Object getCookieValue (Cookie[] cookies, String key)
static public Object getCookieValue(final Cookie[] cookies, final String key)
{
Object result;
@ -121,11 +136,10 @@ public class CookieHelper
return (result);
}
/**
*
*/
static public Object getCookieValue (HttpServletRequest request, String key)
static public Object getCookieValue(final HttpServletRequest request, final String key)
{
Object result;
@ -135,42 +149,19 @@ public class CookieHelper
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));
}
}

View file

@ -1,9 +1,10 @@
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;
/**
*
@ -13,6 +14,5 @@ public interface Page
/**
*
*/
public void doIt (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException;
public void doIt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}

View file

@ -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;
/**
*
@ -19,6 +26,85 @@ public class PageManager extends HttpServlet
/**
*
*/
@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("<html><head></head><body>");
out.println("Permission denied.");
out.println("<form method='get' action='javascript:window.back ();'>");
out.println(" <input type='submit' name='retour' value='Retour' />");
out.println("</form>");
out.println("</body></html>");
}
}
}
}
/**
*
*/
@Override
public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
/**
*
*/
@Override
public void init() throws ServletException
{
//
@ -35,17 +121,67 @@ public class PageManager extends HttpServlet
}
/**
*
*/
public static PageManager instance ()
public Page instanciatePage(final String className)
{
return instance;
Page result;
Class<Page> pageClass = null;
try
{
pageClass = (Class<Page>) 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);
}
/**
*
*/
static public String buildClassName (String pathInfo)
public SecurityAgent securityAgent()
{
SecurityAgent result;
result = this.securityAgent;
//
return (result);
}
/**
*
*/
static public String buildClassName(final String pathInfo)
{
String result;
result = null;
@ -84,7 +220,7 @@ public class PageManager extends HttpServlet
/**
*
*/
static public String buildClassName2 (String pathInfo)
static public String buildClassName2(final String pathInfo)
{
String result;
@ -196,136 +332,10 @@ public class PageManager extends HttpServlet
return (result);
}
/**
*/
public Page instanciatePage (String className)
{
Page result;
Class<Page> pageClass = null;
try
{
pageClass = (Class<Page>) 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 ("<html><head></head><body>");
out.println ("Permission denied.");
out.println ("<form method='get' action='javascript:window.back ();'>");
out.println (" <input type='submit' name='retour' value='Retour' />");
out.println ("</form>");
out.println ("</body></html>");
}
}
}
}
/**
*
*/
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;
@ -351,25 +361,10 @@ public class PageManager extends HttpServlet
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;
@ -396,4 +391,12 @@ public class PageManager extends HttpServlet
//
return (result);
}
/**
*
*/
public static PageManager instance()
{
return instance;
}
}

View file

@ -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,24 +21,14 @@ public class ServletDispatcher extends SimpleServletDispatcher
{
private static final long serialVersionUID = -3471226305721330069L;
protected org.apache.log4j.Logger logger;
// protected Servlets servlets;
/**
*
*/
public void init () throws ServletException
{
super.init();
this.logger = org.apache.log4j.Logger.getLogger (this.getClass());
//this.servlets = new Servlets();
}
/**
*
*/
public void doIt (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
@Override
public void doIt(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{
logger.info("==================================================");
logger.info("getContextPath=[" + request.getContextPath() + "]");
@ -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();
}
}

View file

@ -55,16 +55,12 @@ 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',
'-', '-',
/* 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',
/* 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',
@ -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:
*
* <pre>
* "/article-/123/doors/open.xhtml";
* => "123", "doors" and "open".
* </pre>
*/
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
* <pre>
* "/"
* => "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).
* </pre>
*
*/
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:
*
* <pre>
* "/good/give_file?id=123&filename=foo.jpg"
* => rewriteShorturl("/good/give_file", "123", "foo.jpg");
* => "/good/give_file-/123/foo.jpg"
* </pre>
*
* 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:
*
* <pre>
* "/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"
* </pre>
*/
static public String rewriteShortUrl(final String uri, final String extension, final String... parameters)
{

View file

@ -1,7 +1,6 @@
package fr.devinsy.kiss4web.security;
import java.util.*;
import java.util.Vector;
/**
*
@ -11,7 +10,6 @@ public class Group
protected String name;
protected Vector<String> members;
/**
*
*/
@ -21,11 +19,10 @@ public class Group
this.members = new Vector<String>();
}
/**
*
*/
public void addMember (String login)
public void addMember(final String login)
{
if ((login != null) && (login.length() != 0))
{
@ -33,11 +30,10 @@ public class Group
}
}
/**
*
*/
public boolean contains (String name)
public boolean contains(final String name)
{
boolean result = false;
@ -47,7 +43,6 @@ public class Group
return (result);
}
/**
*
*/
@ -61,7 +56,6 @@ public class Group
return (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,10 +80,10 @@ public class Group
return (this);
}
/**
*
*/
@Override
public String toString()
{
String result;

View file

@ -1,7 +1,7 @@
package fr.devinsy.kiss4web.security;
import java.util.*;
import java.util.Iterator;
import java.util.Vector;
/**
*
@ -18,11 +18,30 @@ public class Groups extends Vector<Group>
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;
@ -58,32 +77,10 @@ public class Groups extends Vector<Group>
return (result);
}
/*
*
*/
public boolean contains (String name)
{
boolean result;
if (get (name) == null)
{
result = false;
}
else
{
result = true;
}
//
return (result);
}
/**
*
*/
public Vector<String> getLoginGroups (String login)
public Vector<String> getLoginGroups(final String login)
{
Vector<String> result;
@ -104,11 +101,10 @@ public class Groups extends Vector<Group>
return (result);
}
/**
*
*/
public String getLoginGroupsString (String login)
public String getLoginGroupsString(final String login)
{
String result;
@ -135,10 +131,10 @@ public class Groups extends Vector<Group>
return (result);
}
/**
*
*/
@Override
public String toString()
{
String result;

View file

@ -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,7 +12,7 @@ public class GroupsFileReader
/**
*
*/
static public Groups load (String fileName) throws Exception
static public Groups load(final String fileName) throws Exception
{
Groups result;

View file

@ -1,9 +1,6 @@
package fr.devinsy.kiss4web.security;
import java.util.*;
import java.util.Iterator;
/**
*
@ -14,61 +11,20 @@ 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);
}
/**
*
*/
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;
@ -94,7 +50,7 @@ public class SecurityAgent
/**
*
*/
public boolean checkPermission (String url, String login)
public boolean checkPermission(final String url, final String login)
{
boolean result = false;
@ -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);
}
}

View file

@ -1,8 +1,5 @@
package fr.devinsy.kiss4web.security;
/**
*
*/
@ -13,7 +10,6 @@ public class User
protected String realName;
protected String email;
/**
*
*/
@ -25,7 +21,6 @@ public class User
this.email = null;
}
/**
*
*/
@ -78,11 +73,10 @@ public class User
return (result);
}
/**
*
*/
public User setEmail (String email)
public User setEmail(final String email)
{
this.email = email;
@ -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,6 +120,7 @@ public class User
/**
*
*/
@Override
public String toString()
{
String result;
@ -136,4 +131,3 @@ public class User
return (result);
}
}

View file

@ -1,8 +1,7 @@
package fr.devinsy.kiss4web.security;
import java.util.*;
import java.util.Iterator;
import java.util.Vector;
/**
*
@ -11,7 +10,6 @@ public class Users extends Vector<User>
{
private static final long serialVersionUID = 6140538630004281217L;
/**
*
*/
@ -20,11 +18,30 @@ public class Users extends Vector<User>
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;
@ -60,31 +77,10 @@ public class Users extends Vector<User>
return (result);
}
/*
*
*/
public boolean contains (String login)
{
boolean result;
if (getByLogin (login) == null)
{
result = false;
}
else
{
result = true;
}
//
return (result);
}
/**
*
*/
@Override
public String toString()
{
String result;

View file

@ -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,7 +12,7 @@ public class UsersFileReader
/**
*
*/
static public Users load (String fileName) throws Exception
static public Users load(final String fileName) throws Exception
{
Users result;
@ -47,7 +47,6 @@ public class UsersFileReader
throw new Exception("can't load (" + fileName + ")", exception);
}
//
return (result);
}

View file

@ -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;
@ -78,6 +55,28 @@ public class FileItemHelper
//
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);
}
}
// ////////////////////////////////////////////////////////////////////////

View file

@ -2,8 +2,6 @@ package fr.devinsy.util.web;
import javax.servlet.http.HttpServletResponse;
/**
*
*/
@ -14,7 +12,7 @@ public class Redirector
/**
*
*/
static public void redirect (HttpServletResponse response, String destination)
static public void redirect(final HttpServletResponse response, final String destination)
{
logger.info("Redirect to <" + destination + ">");

View file

@ -1,12 +1,10 @@
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;
/**
*
*/
@ -21,12 +19,10 @@ public class SimpleSecurityAgent
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;
@ -34,40 +30,10 @@ public class SimpleSecurityAgent
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)
{
String result;
result = (String) CookieHelper.getCookieValue(request, this.userIdLabel);
//
return(result);
}
/**
*
*/
public String accountId(HttpServletRequest request)
public String accountId(final HttpServletRequest request)
{
String result;
@ -77,12 +43,10 @@ public class SimpleSecurityAgent
return (result);
}
/**
*
*/
public String auth(HttpServletRequest request)
public String auth(final HttpServletRequest request)
{
String result;
@ -92,11 +56,10 @@ public class SimpleSecurityAgent
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;
@ -125,24 +88,20 @@ public class SimpleSecurityAgent
return (result);
}
/**
*
*/
static public String computeAuth(String key1, String key2, String key3, String key4)
public void reset(final HttpServletRequest request, final HttpServletResponse response)
{
String result;
result = md5sum(key1 + key2 + key3+ key4);
//
return(result);
CookieHelper.reset(response, this.authLabel);
CookieHelper.reset(response, this.accountIdLabel);
CookieHelper.reset(response, this.userIdLabel);
}
/**
*
*/
public void setAuthenticated (HttpServletRequest request, HttpServletResponse response, String accountId, String userId)
public void setAuthenticated(final HttpServletRequest request, final HttpServletResponse response, final String accountId, final String userId)
{
// Refresh cookie.
int duration = 60 * 60;
@ -155,14 +114,42 @@ public class SimpleSecurityAgent
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);
}
/**
*
*/
public void reset(HttpServletRequest request, HttpServletResponse response)
static public String computeAuth(final String key1, final String key2, final String key3, final String key4)
{
CookieHelper.reset(response, this.authLabel);
CookieHelper.reset(response, this.accountIdLabel);
CookieHelper.reset(response, this.userIdLabel);
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);
}
}

View file

@ -2,8 +2,7 @@
* Kiss4Web tests.
*/
import fr.devinsy.kiss4web.*;
import fr.devinsy.kiss4web.ServletDispatcher;
/**
*
@ -19,7 +18,6 @@ class FooTester
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);
@ -29,20 +27,18 @@ class FooTester
//
logger.info("Set the log file format...");
// log = org.apache.log4j.Category.getInstance(Application.class.getName());
// log =
// org.apache.log4j.Category.getInstance(Application.class.getName());
logger.info("... done.");
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;
@ -56,21 +52,6 @@ class FooTester
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);
}
@ -78,7 +59,7 @@ class FooTester
/**
*
*/
public static void main(String[] args)
public static void main(final String[] args)
{
System.out.println("----------------------------");
System.out.println(testCaller("/", "fr.devinsy.website"));
@ -87,4 +68,17 @@ class FooTester
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);
}
}