mcardle     2005/10/09 18:50:33 CEST

  Modified files:
    core/src/java/org/jahia/services/esi EsiURLGenerator.java 
  Log:
  * added java.security version of MD5 generator
  
  Revision  Changes    Path
  1.5       +106 -78   
jahia/core/src/java/org/jahia/services/esi/EsiURLGenerator.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/services/esi/EsiURLGenerator.java.diff?r1=1.4&r2=1.5&f=h
  
  
  
  Index: EsiURLGenerator.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/services/esi/EsiURLGenerator.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- EsiURLGenerator.java      7 Oct 2005 14:59:57 -0000       1.4
  +++ EsiURLGenerator.java      9 Oct 2005 16:50:33 -0000       1.5
  @@ -5,6 +5,7 @@
   import org.jahia.services.usermanager.JahiaUser;
   import org.jahia.bin.Jahia;
   import org.jahia.taglibs.esi.JesiConst;
  +import org.jahia.utils.Base64;
   
   import java.io.UnsupportedEncodingException;
   import java.net.URLEncoder;
  @@ -22,12 +23,12 @@
       private static org.apache.log4j.Logger logger =
               org.apache.log4j.Logger.getLogger(EsiURLGenerator.class);
   
  -       //TODO: Convert this to a instanciated Bean 
  +    //TODO: Convert this to a instanciated Bean
   
  -       public static final String ALL_ESI = "all";
  -       public static final String INDIV_ESI = "indiv";
  -       public static final String DEFAULT_ESI_USER = ALL_ESI;
  -       public static final String DEFAULT_ESI_GROUP = ALL_ESI;
  +    public static final String ALL_ESI = "all";
  +    public static final String INDIV_ESI = "indiv";
  +    public static final String DEFAULT_ESI_USER = ALL_ESI;
  +    public static final String DEFAULT_ESI_GROUP = ALL_ESI;
   
   
       public static String getESIURLPart(ProcessingContext processingContext, 
JahiaUser theUser,
  @@ -49,96 +50,96 @@
        * (until encrypted cookies replace URL rewritting method)
        *
        */
  -   public static String getESITemplURLPart(int pageID, ProcessingContext 
processingContext, JahiaUser theUser,
  -                                           int siteID) {
  +    public static String getESITemplURLPart(int pageID, ProcessingContext 
processingContext, JahiaUser theUser,
  +                                            int siteID) {
   
           //if ESI isn't active, or if Cookie based identifier are actived, 
ignore the ESI param
           if 
(!processingContext.settings().lookupBoolean(SettingsBean.ESI_CACHE_ACTIVATED)
  -            || processingContext.settings().getEsiUseCookieUserIdentifiers() 
)
  +                || 
processingContext.settings().getEsiUseCookieUserIdentifiers() )
               return "";
   
   
   
  -       StringBuffer result = new StringBuffer();
  +        StringBuffer result = new StringBuffer();
   
  -       //-get user ESI key  (user either Marc's JahiaUserDB.getUserESIKey or 
existing JahiaUSer.hashCode() )
  -       //theUser.hashCode();
  +        //-get user ESI key  (user either Marc's JahiaUserDB.getUserESIKey 
or existing JahiaUSer.hashCode() )
  +        //theUser.hashCode();
   
  -       //TODO: check user exists - resolveUser() - dude, it should be the 
case since we have a processingContext object
  -       String ESIuserVal = encodeUsername(theUser, processingContext);
  +        //TODO: check user exists - resolveUser() - dude, it should be the 
case since we have a processingContext object
  +        String ESIuserVal = encodeUsername(theUser, processingContext);
   
  -       //JahiaPageDefinition template;
  +        //JahiaPageDefinition template;
   
  -       //TODO : Read ESIuser and ESIgroup setting for each template from an 
XML file
  -       String ESIuser = ALL_ESI;          //= template.getESIuser();  //note 
this will always be null as currently not set in template.xml
  -       String ESIgroup = ALL_ESI;         //= template.getESIgroup(); //note 
this will always be null as currently not set in template.xml
  +        //TODO : Read ESIuser and ESIgroup setting for each template from an 
XML file
  +        String ESIuser = ALL_ESI;          //= template.getESIuser();  
//note this will always be null as currently not set in template.xml
  +        String ESIgroup = ALL_ESI;         //= template.getESIgroup(); 
//note this will always be null as currently not set in template.xml
   
  -       /*
  -       try {
  -
  -
  -          //-get template for current page
  -           template = 
ServicesRegistry.getInstance().getJahiaPageService().lookupPage(pageID, this)
  -                           .getPageTemplate();
  -           //-generate ESI param (make sure ESI fragments get rid of them)
  +        /*
  +        try {
   
   
  -         String tmplName = template.getName();
  -         if ( "Home".equals(template.getName()) ) {
  -             ESIuser = ALL_ESI;//INDIV_ESI;
  -         }
  -         else if ( "Default home".equals(template.getName()) ) {
  -             ESIuser = ALL_ESI;//INDIV_ESI;
  -         }
  -         else if ( "Faq".equals(template.getName()) ) {
  -             ESIuser = INDIV_ESI;
  +           //-get template for current page
  +            template = 
ServicesRegistry.getInstance().getJahiaPageService().lookupPage(pageID, this)
  +                            .getPageTemplate();
  +            //-generate ESI param (make sure ESI fragments get rid of them)
  +
  +
  +          String tmplName = template.getName();
  +          if ( "Home".equals(template.getName()) ) {
  +              ESIuser = ALL_ESI;//INDIV_ESI;
  +          }
  +          else if ( "Default home".equals(template.getName()) ) {
  +              ESIuser = ALL_ESI;//INDIV_ESI;
  +          }
  +          else if ( "Faq".equals(template.getName()) ) {
  +              ESIuser = INDIV_ESI;
  +          }
  +          else if ( "Categories browsing".equals(template.getName()) ) {
  +              ESIgroup = "admin";
            }
  -         else if ( "Categories browsing".equals(template.getName()) ) {
  -             ESIgroup = "admin";
  +         //Blog  - Blog listing - Categories browsing - Directories Files
  +         //Directories Links - Directories Main - Directories People
  +         //Directories Project - Document Detail - Document Listing
  +         //Double - Faq - HTML file clipping - Home - Portal - Simple
  +
  +        //default behaviour if template has no initial ESI settings
  +          else {
  +               //do nothing
  +          }
  +
  +
  +        } catch (JahiaException e) {
  +            //Do something else here, this is a shit way to handle error
  +            logger.fatal("[esi]:  Cannot find template for current page");
  +            e.printStackTrace();
  +        }*/
  +
  +        //if ESIuser set, then add to result
  +        if ( !INDIV_ESI.equals(ESIuser) ) {
  +            //result.append("/"+ JesiConst.USER+"/");
  +            //result.append("user_" + ESIuserVal);
  +            result.append("/"+ JesiConst.ESI_MARKER+"/");
  +            result.append("user_" + ESIuserVal);
           }
  -        //Blog  - Blog listing - Categories browsing - Directories Files
  -        //Directories Links - Directories Main - Directories People
  -        //Directories Project - Document Detail - Document Listing
  -        //Double - Faq - HTML file clipping - Home - Portal - Simple
  -
  -       //default behaviour if template has no initial ESI settings
  -         else {
  -              //do nothing
  -         }
  +        //if ESIgroup set, then add to result
  +        else if ( !ALL_ESI.equals(ESIgroup) ) {
  +            if ( theUser.isMemberOfGroup(siteID,ESIgroup) ) {
  +                //result.append("/"+ JesiConst.GROUP+"/");
  +                //result.append("group_" + ESIgroup);
  +                result.append("/"+ JesiConst.ESI_MARKER+"/");//shouldn't 
have both group and user set at same time, so no need for check
  +                result.append("group_" + ESIgroup);
  +            }
  +        }
  +        else return ""; //return empty string if no ESI params present for 
current page's template
   
  +        return result.toString();
   
  -       } catch (JahiaException e) {
  -           //Do something else here, this is a shit way to handle error
  -           logger.fatal("[esi]:  Cannot find template for current page");
  -           e.printStackTrace();
  -       }*/
  -
  -          //if ESIuser set, then add to result
  -          if ( !INDIV_ESI.equals(ESIuser) ) {
  -               //result.append("/"+ JesiConst.USER+"/");
  -               //result.append("user_" + ESIuserVal);
  -               result.append("/"+ JesiConst.ESI_MARKER+"/");
  -               result.append("user_" + ESIuserVal);
  -           }
  -           //if ESIgroup set, then add to result
  -           else if ( !ALL_ESI.equals(ESIgroup) ) {
  -               if ( theUser.isMemberOfGroup(siteID,ESIgroup) ) {
  -                   //result.append("/"+ JesiConst.GROUP+"/");
  -                   //result.append("group_" + ESIgroup);
  -                   result.append("/"+ JesiConst.ESI_MARKER+"/");//shouldn't 
have both group and user set at same time, so no need for check
  -                   result.append("group_" + ESIgroup);
  -               }
  -           }
  -           else return ""; //return empty string if no ESI params present 
for current page's template
  -
  -       return result.toString();
  -
  -       //TODO: add ESI tag generation to other URL composition fcts such as 
compuseEngineURL
  -
  -       //-get template ESI settings from XML file (need to read it once and 
save it in memory)
  -       //-check that ESI template settings are correct: i.e. user has access 
to template
  -       //and belongs to specified group (if applicable
  -       //--------> ABOVE is all done in JahiaPageDefinition
  +        //TODO: add ESI tag generation to other URL composition fcts such as 
compuseEngineURL
  +
  +        //-get template ESI settings from XML file (need to read it once and 
save it in memory)
  +        //-check that ESI template settings are correct: i.e. user has 
access to template
  +        //and belongs to specified group (if applicable
  +        //--------> ABOVE is all done in JahiaPageDefinition
   
       }
   
  @@ -162,7 +163,7 @@
   
           if (processingContext.settings().getEsiEncryptUserIdentifiers()) {
               byte[] buf = stringToEncode.getBytes();
  -            stringToEncode = MD5.asHex(buf);
  +            stringToEncode = getMD5Hash(buf);
           }
           try {
               stringToEncode = URLEncoder.encode(stringToEncode,"UTF-8");
  @@ -173,4 +174,31 @@
           return stringToEncode;
       }
   
  +    /**
  +     * uses faster MD5 implementation
  +     * @param content in bytes
  +     * @return hash
  +     */
  +    //TODO: do tests to see if it's faster than getMD5Hash()
  +    public static String getFastMD5Hash(byte[] content) {
  +        return MD5.asHex(content).toString();
  +    }
  +
  +    /**
  +     * standard java.security MD5 implementation
  +     * @param content in bytes
  +     * @return hash
  +     */
  +    public static String getMD5Hash(byte[] content) {
  +        try {
  +            java.security.MessageDigest digest = 
java.security.MessageDigest.getInstance("MD5");
  +            byte[] b = digest.digest(content);
  +            // Base64 encoding allows for unwanted slash characters.
  +            return new String (Base64.encode (b));
  +        } catch (Exception e) {
  +            logger.error(" Error generating jdk MD5 Content Hash ");
  +        }
  +        return null;
  +    }
  +
   }
  

Reply via email to