abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r541101 - /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java
Date Wed, 23 May 2007 22:18:29 GMT
Author: jmsnell
Date: Wed May 23 15:18:25 2007
New Revision: 541101

URL: http://svn.apache.org/viewvc?view=rev&rev=541101
Log:
Utility code for generating Etags using a MD5 hash of source material

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java?view=diff&rev=541101&r1=541100&r2=541101
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/EntityTag.java Wed
May 23 15:18:25 2007
@@ -18,11 +18,16 @@
 package org.apache.abdera.util;
 
 import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.codec.binary.Hex;
+
 /**
- * Represents a simple Entity Tag
+ * Implements an EntityTag.
  */
 public class EntityTag 
   implements Cloneable, 
@@ -165,4 +170,34 @@
       return super.clone();
   }
   
+
+  /**
+   * Utility method for generating ETags. Works by concatenating the UTF-8
+   * bytes of the provided strings then generating an MD5 hash of the result.
+   */
+  public static EntityTag generate(String... material) {
+    String etag = null;
+    try {
+      MessageDigest md = MessageDigest.getInstance("md5");
+      for (String s : material) md.update(s.getBytes("utf-8"));
+      byte[] digest = md.digest();
+      etag = new String(Hex.encodeHex(digest));
+    } catch (NoSuchAlgorithmException e) {
+      throw new UnsupportedOperationException(
+        "The hashing function is not available");
+    } catch (UnsupportedEncodingException e) {
+      // should not happen
+      throw new RuntimeException("UTF-8 not supported?", e);
+    }
+    return new EntityTag(etag);
+  }
+  
+  /**
+   * Checks that the passed in ETag matches the ETag generated by the 
+   * generate method
+   */
+  public static boolean matches(EntityTag etag, String... material) {
+    EntityTag etag2 = generate(material);
+    return EntityTag.matches(etag,etag2);
+  }
 }



Mime
View raw message