You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2012/05/30 15:44:10 UTC

svn commit: r1344258 - in /empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2: pages/Page.java pages/PageDefinition.java utils/ParameterMap.java

Author: doebele
Date: Wed May 30 13:44:10 2012
New Revision: 1344258

URL: http://svn.apache.org/viewvc?rev=1344258&view=rev
Log:
EMPIREDB-140
improved ParameterMap

Modified:
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinition.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ParameterMap.java

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java?rev=1344258&r1=1344257&r2=1344258&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java (original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java Wed May 30 13:44:10 2012
@@ -30,10 +30,13 @@ import javax.faces.application.Navigatio
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
 
+import org.apache.empire.commons.StringUtils;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBRowSet;
+import org.apache.empire.exceptions.ItemNotFoundException;
 import org.apache.empire.jsf2.app.FacesApplication;
 import org.apache.empire.jsf2.app.FacesUtils;
+import org.apache.empire.jsf2.utils.ParameterMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,7 +46,7 @@ public abstract class Page implements Se
 
     private static final String SESSION_MESSAGE  = "PAGE_SESSION_MESSAGE";
 
-    private static final String INVALID_ACTION   = "XXXXXXXXXXXX";
+    // private static final String INVALID_ACTION   = "XXXXXXXXXXXX";
 
     private static final Logger log              = LoggerFactory.getLogger(Page.class);
 
@@ -54,8 +57,10 @@ public abstract class Page implements Se
 
     protected Page()
     {
-        String name = this.getClass().getSimpleName();
-        Page.log.info("PageBean {} created.", name);
+        if (log.isDebugEnabled())
+        {   String name = this.getClass().getSimpleName();
+            Page.log.debug("PageBean {} created.", name);
+        }
     }
 
     public String getPageName()
@@ -78,16 +83,33 @@ public abstract class Page implements Se
 
     public String getAction()
     {
-        return this.action;
+        if (this.action==null)
+            return null;
+        
+        // if (this.action==INVALID_ACTION)
+        //      return null;
+
+        // Generate key
+        ParameterMap pm = FacesUtils.getParameterMap(FacesUtils.getContext());
+        String actionParam = (pm!=null ? pm.encodeString(action) : action);
+        return actionParam;
     }
 
-    public void setAction(String action)
+    public void setAction(String actionParam)
     {
         if (!initialized)
-            Page.log.info("Setting PageBean action {} for bean {}.", action, getPageName());
+            Page.log.debug("Setting PageBean action {} for bean {}.", action, getPageName());
         else
             Page.log.trace("Re-setting PageBeanAction {} for bean {}.", action, getPageName());
-        this.action = action;
+        
+        // actionParam
+        if (StringUtils.isEmpty(actionParam))
+            return;
+
+        // Set action from param
+        this.action = PageDefinition.decodeActionParam(actionParam);
+        if (this.action==null)
+            throw new ItemNotFoundException(actionParam); 
     }
 
     public PageDefinition getPageDefinition()
@@ -144,15 +166,17 @@ public abstract class Page implements Se
         // Execute Action
         if (this.action != null)
         {
+            /*
             if (this.action.equals(Page.INVALID_ACTION))
             {
                 Page.log.error("Action probably executed twice. Ignoring action.");
                 return;
             }
+            */
             try
             {
-                Page.log.debug("Executing action {} on {}.", String.valueOf(this.action), getPageName());
-                Method method = getClass().getMethod(this.action);
+                Page.log.debug("Executing action {} on {}.", String.valueOf(action), getPageName());
+                Method method = getClass().getMethod(action);
                 Object result = method.invoke(this);
                 if (result != null)
                 {
@@ -178,7 +202,7 @@ public abstract class Page implements Se
             finally
             {
                 // Clear action
-                this.action = Page.INVALID_ACTION;
+                this.action = null; // Page.INVALID_ACTION;
             }
         }
         else

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinition.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinition.java?rev=1344258&r1=1344257&r2=1344258&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinition.java (original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageDefinition.java Wed May 30 13:44:10 2012
@@ -19,14 +19,62 @@
 package org.apache.empire.jsf2.pages;
 
 import org.apache.empire.commons.StringUtils;
+import org.apache.empire.jsf2.app.FacesUtils;
+import org.apache.empire.jsf2.utils.ParameterMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 public class PageDefinition
 {
+    private static final Logger log = LoggerFactory.getLogger(PageDefinitions.class);
+
+    private static final String ACTION_PARAMETER_TYPE = "ACTION";
+    
     private String path;
     private String pageBeanName;
     private Class<? extends Page> pageBeanClass;
     private PageDefinition parent = null;
+
+    /*
+    private static Hashtable<String, String> actionCodeMap = new Hashtable<String, String>();
+    
+    private static Hashtable<String, String> actionMap = new Hashtable<String, String>();
+
+    private static String encodeActionParam(String action)
+    {
+        String param = ParameterMap.encodeString(action);
+        actionMap.put(param, action);
+        return param;
+    }
+    
+    public static String decodeActionParam(String param)
+    {
+        String action = actionMap.get(param);
+        if (action==null)
+            log.warn("no action available for param {}.", param);
+        return action;
+    }
+    */
+
+    private static String encodeActionParam(String action)
+    {
+        ParameterMap pm = FacesUtils.getParameterMap(FacesUtils.getContext());
+        if (pm==null)
+            return action;
+        return pm.put(ACTION_PARAMETER_TYPE, action, true);
+    }
+    
+    public static String decodeActionParam(String param)
+    {
+        ParameterMap pm = FacesUtils.getParameterMap(FacesUtils.getContext());
+        if (pm==null)
+            return param;
+        String action = StringUtils.valueOf(pm.get(ACTION_PARAMETER_TYPE, param));
+        if (action==null)
+            log.warn("no action available for param {}.", param);
+        return action;
+    }
     
     public PageDefinition(String path, Class<? extends Page> pageBeanClass)
     { 
@@ -77,7 +125,7 @@ public class PageDefinition
     {
         PageOutcome outcome = getOutcome();
         if (StringUtils.isNotEmpty(action))
-            outcome = outcome.addParam("action", action);
+            outcome = outcome.addParam("action", encodeActionParam(action));
         return outcome;
     }
     
@@ -92,7 +140,7 @@ public class PageDefinition
     {   
         PageOutcome outcome = getRedirect();
         if (StringUtils.isNotEmpty(action))
-            outcome = outcome.addParam("action", action);
+            outcome = outcome.addParam("action", encodeActionParam(action));
         return outcome;
     }
     
@@ -107,7 +155,7 @@ public class PageDefinition
     {
         PageOutcome outcome = getRedirectWithViewParams();
         if (StringUtils.isNotEmpty(action))
-            outcome = outcome.addParam("action", action);
+            outcome = outcome.addParam("action", encodeActionParam(action));
         return outcome;
     }
 

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ParameterMap.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ParameterMap.java?rev=1344258&r1=1344257&r2=1344258&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ParameterMap.java (original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/utils/ParameterMap.java Wed May 30 13:44:10 2012
@@ -19,18 +19,22 @@
 package org.apache.empire.jsf2.utils;
 
 import java.io.Serializable;
-import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
-import java.util.HashMap;
+import java.text.SimpleDateFormat;
+import java.util.Hashtable;
+import java.util.Locale;
 
+import org.apache.empire.commons.DateUtils;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.db.DBRowSet;
+import org.apache.empire.exceptions.InvalidArgumentException;
+import org.apache.empire.exceptions.UnexpectedReturnValueException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * This class manages request parameters in a way that they cannot be analysed and modified by the user 
+ * This class manages request parameters in a way that they cannot be analyzed and modified by the user 
  * @author doebele
  *
  */
@@ -40,6 +44,8 @@ public class ParameterMap implements Ser
 
     private static final Logger           log              = LoggerFactory.getLogger(ParameterMap.class);
 
+    private static final SimpleDateFormat dateFormat       = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss", Locale.GERMAN);
+    
     static private MessageDigest          md5              = null;
     {
         try
@@ -52,24 +58,93 @@ public class ParameterMap implements Ser
             throw new RuntimeException(e);
         }
     }
+    
+    private final byte[] salt;
+    
+    public ParameterMap()
+    {
+        String dateTime = dateFormat.format(DateUtils.getTimeNow());
+        salt = dateTime.getBytes();
+    }
+    
+    public synchronized String encodeString(String valueAsString)
+    {
+        if (valueAsString==null)
+            throw new InvalidArgumentException("valueAsString", valueAsString);
+        // log
+        if (log.isTraceEnabled())
+            log.trace("Generating code for value {}.", valueAsString);
+        // generate code
+        md5.reset();
+        if (salt!=null)
+            md5.update(salt);
+        md5.update(valueAsString.getBytes());
+        byte s[] = ParameterMap.md5.digest();
+        StringBuilder hash = new StringBuilder(32);
+        for (int i = 0; i < s.length; i++)
+        {   // add the hash part
+            // String check = Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);;
+            String part = Integer.toHexString(0x000000ff & s[i]);
+            switch(part.length())
+            {
+                case 1: hash.append('0');
+                case 2: hash.append(part);
+                        break;
+                default:
+                        throw new UnexpectedReturnValueException(part, "Integer.toHexString");
+                        // hash.append(part.substring(2));
+            }
+        }
+        return hash.toString();
+    }
+
+    private Hashtable<String, String> codeMap = new Hashtable<String, String>();
 
-    private final HashMap<String, HashMap<String, Object>> typeMap = new HashMap<String, HashMap<String, Object>>();
+    public String encodeStringWithCache(String valueAsString)
+    {
+        String code = codeMap.get(valueAsString);
+        if (code==null)
+        {   // generate code
+            code = encodeString(valueAsString);
+            codeMap.put(valueAsString, code);
+        }
+        /*
+        else
+        {   // Trace
+            if (log.isTraceEnabled())
+                log.trace("Using already generated code {} for value {}.", code, valueAsString);
+        }
+        */
+        return code;
+    }
+    
+    private final Hashtable<String, Hashtable<String, Object>> typeMap = new Hashtable<String, Hashtable<String, Object>>();
     
     private void putValue(String typeName, String key, Object value)
     {
-        HashMap<String, Object> map = typeMap.get(typeName);
+        Hashtable<String, Object> map = typeMap.get(typeName);
         if (map==null)
-        {   map = new HashMap<String, Object>(1);
+        {   map = new Hashtable<String, Object>(1);
             typeMap.put(typeName, map);
         }
+        if (key==null || value==null)
+            log.warn("Key or value is null.");
         map.put(key, value);
     }
 
+    public String put(String type, String key, boolean useCache)
+    {
+        // Generate id and put in map
+        String id = (useCache ? encodeStringWithCache(key) : encodeString(key));
+        putValue(type, id, key);
+        return id;
+    }
+
     public String put(Class<? extends Object> c, Object[] key)
     {
         // Generate id and put in map
         String ref = StringUtils.valueOf(key);
-        String id = generateId(ref);
+        String id = encodeString(ref);
         String type = c.getSimpleName();
         putValue(type, id, key);
         return id;
@@ -79,30 +154,36 @@ public class ParameterMap implements Ser
     {
         // Generate id and put in map
         String ref = StringUtils.valueOf(key);
-        String id = generateId(ref);
+        String id = encodeString(ref);
         String type = rowset.getClass().getSimpleName();
         putValue(type, id, key);
         return id;
     }
 
+    public Object get(String type, String id)
+    {
+        Hashtable<String, Object> map = typeMap.get(type);
+        return (map!=null ? map.get(id) : null);
+    }
+
     public Object[] get(Class<? extends Object> c, String id)
     {
         String type = c.getSimpleName();
-        HashMap<String, Object> map = typeMap.get(type);
+        Hashtable<String, Object> map = typeMap.get(type);
         return (map!=null ? ((Object[])map.get(id)) : null);
     }
 
     public Object[] get(DBRowSet rowset, String id)
     {
         String type = rowset.getClass().getSimpleName();
-        HashMap<String, Object> map = typeMap.get(type);
+        Hashtable<String, Object> map = typeMap.get(type);
         return (map!=null ? ((Object[])map.get(id)) : null);
     }
 
     public void clear(Class<? extends Object> c)
     {
         String type = c.getSimpleName();
-        HashMap<String, Object> map = typeMap.get(type);
+        Hashtable<String, Object> map = typeMap.get(type);
         if (map!=null)
             map.clear();
     }
@@ -110,31 +191,9 @@ public class ParameterMap implements Ser
     public void clear(DBRowSet rowset)
     {
         String type = rowset.getClass().getSimpleName();
-        HashMap<String, Object> map = typeMap.get(type);
+        Hashtable<String, Object> map = typeMap.get(type);
         if (map!=null)
             map.clear();
     }
 
-    private String generateId(String valueAsString)
-    {
-        // byte salt[] = UserBean.SALT.getBytes();
-        try
-        {
-            String result = "";
-            // md5.update(salt);
-            ParameterMap.md5.update(valueAsString.getBytes("UTF8"));
-            byte s[] = ParameterMap.md5.digest();
-            for (int i = 0; i < s.length; i++)
-            {
-                // TODO DW: avoid magic numbers for readability || add few words of comments, else
-                result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
-            }
-            return result;
-        }
-        catch (UnsupportedEncodingException e)
-        {
-            throw new RuntimeException(e);
-        }
-    }
-
 }