abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1177398 - in /abdera/abdera2: activities/src/main/java/org/apache/abdera2/activities/extra/ common/src/main/java/org/apache/abdera2/common/io/ common/src/main/java/org/apache/abdera2/common/misc/ common/src/main/java/org/apache/abdera2/com...
Date Thu, 29 Sep 2011 18:57:09 GMT
Author: jmsnell
Date: Thu Sep 29 18:57:08 2011
New Revision: 1177398

URL: http://svn.apache.org/viewvc?rev=1177398&view=rev
Log:
Number of general enhancements

Added:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java
  (with props)
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
  (with props)
Modified:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Jwt.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java?rev=1177398&r1=1177397&r2=1177398&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/BinaryObject.java
Thu Sep 29 18:57:08 2011
@@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 
 import javax.activation.DataHandler;
 import javax.activation.MimeType;
@@ -12,6 +13,9 @@ import org.apache.abdera2.activities.io.
 import org.apache.abdera2.activities.io.gson.Property;
 import org.apache.abdera2.activities.model.objects.FileObject;
 import org.apache.abdera2.common.anno.Name;
+import org.apache.abdera2.common.io.Compression;
+import org.apache.abdera2.common.io.Compression.CompressionCodec;
+import org.apache.abdera2.common.security.HashHelper.Hasher;
 import org.apache.commons.codec.binary.Base64InputStream;
 import org.apache.commons.codec.binary.Base64OutputStream;
 
@@ -48,6 +52,14 @@ public class BinaryObject extends FileOb
     setDisplayName(displayName);
   }
     
+  public void setContent(DataHandler data, CompressionCodec... comps) throws IOException
{
+    setContent(data,(Hasher)null,comps);
+  }
+  
+  public void setContent(byte[] data, Hasher hash, CompressionCodec... comps) throws IOException
{
+    setContent(new ByteArrayInputStream(data),hash,comps);
+  }
+  
   /**
    * Set the Content and the MimeType from the DatHandler. 
    * This method defers to the setContent(InputStream) method
@@ -58,37 +70,61 @@ public class BinaryObject extends FileOb
    * and use a custom TypeAdapter for the BinaryObject to read and
    * consume the DataHandler during the actual Serialization.
    */
-  public void setContent(DataHandler data) throws IOException {
-    setContent(data.getInputStream());
+  public void setContent(DataHandler data,Hasher hash, CompressionCodec... comps) throws
IOException {
+    setContent(data.getInputStream(),hash,comps);
     setMimeType(data.getContentType());
   }
   
+  public void setContent(InputStream data) throws IOException {
+    setContent(data,null);
+  }
+  
   /**
    * Set the Content as a Base64 Encoded string. Calling this 
    * method will perform a blocking read that will consume the 
    * InputStream and generate a Base64 Encoded String. 
-   * TODO: A better approach would be to store the InputStream
-   * or DataHandler directly within the exts table and use a 
-   * custom TypeAdapter for the BinaryObject to read and consume
-   * the InputStream during the actual Serialization process.
-   * That would, at the very least, defer the performance hit
-   * and save memory resources while the object is stored in 
-   * memory.
+   * 
+   * If a Hasher class is provided, a hash digest for the 
+   * input data prior to encoding will be generated and 
+   * stored in a property value whose name is the value
+   * returned by Hasher.name(); e.g., HashHelper.Md5 will 
+   * add "md5":"{hex digest}" to the JSON object. 
+   * 
+   * The content may be optionally compressed prior to base64 
+   * encoding by passing in one or more CompressionCodecs. If 
+   * compression is used, a "compression" property will be added
+   * to the object whose value is a comma separated list of 
+   * the applied compression codecs in the order of application. 
+   * The getInputStream method will automatically search for the 
+   * "compression" property and attempt to automatically decompress
+   * the stream when reading.
    */
-  public void setContent(InputStream data) throws IOException {
+  public void setContent(InputStream data, Hasher hash, CompressionCodec... comps) throws
IOException {
     ByteArrayOutputStream out = 
       new ByteArrayOutputStream();
-    Base64OutputStream bout = 
+    OutputStream bout = 
       new Base64OutputStream(out,true,0,null);
+    if (comps.length > 0) {
+      bout = Compression.wrap(bout,comps);
+      String comp = Compression.describe(null, comps);
+      setProperty("compression",comp.substring(1,comp.length()-1));
+    }
+    
     byte[] d = new byte[1024];
     int r = -1;
     while((r = data.read(d)) > -1) { 
+      if (hash != null)
+        hash.update(d, 0, r);
       bout.write(d, 0, r);
       bout.flush();
     }
     bout.close();
     String c = new String(out.toByteArray(),"UTF-8");
     super.setContent(c);
+    if (hash != null)
+      setProperty(
+        hash.name(),
+        hash.get());
   }
   
   /**
@@ -102,16 +138,25 @@ public class BinaryObject extends FileOb
     ByteArrayInputStream in = 
       new ByteArrayInputStream(
         content.getBytes("UTF-8"));
-    Base64InputStream bin = 
+    InputStream bin = 
       new Base64InputStream(in);
+    if (has("compression")) {
+      String comp = getProperty("compression");
+      bin = Compression.wrap(bin, comp);
+    }
     return bin;
   }
   
-  public void setContent(byte[] data) throws IOException {
-    setContent(new ByteArrayInputStream(data));
+  public void setContent(byte[] data, CompressionCodec... comps) throws IOException {
+    setContent(new ByteArrayInputStream(data),null,comps);
   }
   
-  public void setContent(byte[] data, int s, int e) throws IOException {
-    setContent(new ByteArrayInputStream(data,s,e));
+  public void setContent(byte[] data, int s, int e, CompressionCodec... comps) throws IOException
{
+    setContent(new ByteArrayInputStream(data,s,e),null,comps);
   }
+  
+  public void setContent(byte[] data, int s, int e, Hasher hash, CompressionCodec... comps)
throws IOException {
+    setContent(new ByteArrayInputStream(data,s,e),hash,comps);
+  }
+  
 }

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Jwt.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Jwt.java?rev=1177398&r1=1177397&r2=1177398&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Jwt.java (original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Jwt.java Thu
Sep 29 18:57:08 2011
@@ -5,13 +5,10 @@ import java.io.UnsupportedEncodingExcept
 import java.security.Key;
 import java.security.PrivateKey;
 import java.security.PublicKey;
-import java.security.Signature;
-import java.util.Arrays;
-
-import javax.crypto.Mac;
 
 import org.apache.abdera2.activities.model.ASBase;
 import org.apache.abdera2.activities.model.IO;
+import org.apache.abdera2.common.security.HashHelper;
 import org.apache.commons.codec.binary.Base64;
 
 /**
@@ -37,33 +34,20 @@ public class Jwt {
     }
     
     public String sig(Key key, byte[] mat) {
-      try {
-        switch(this) {
-        case HS256:
-        case HS384:
-        case HS512: {
-          Mac mac = Mac.getInstance(internal);
-          mac.init(key);
-          byte[] sig = mac.doFinal(mat);
-          return Base64.encodeBase64URLSafeString(sig);
-        }
-        case RS256:
-        case RS384:
-        case RS512:
-        case ES256:
-        case ES384:
-        case ES512: {
-          Signature sig = Signature.getInstance(internal);
-          sig.initSign((PrivateKey)key);
-          sig.update(mat);
-          byte[] dat = sig.sign();
-          return Base64.encodeBase64URLSafeString(dat);
-        }
-        default:
-          throw new UnsupportedOperationException();
-        }
-      } catch (Throwable t) {
-        throw new RuntimeException(t);
+      switch(this) {
+      case HS256:
+      case HS384:
+      case HS512:
+        return HashHelper.hmac(key, internal, mat);
+      case RS256:
+      case RS384:
+      case RS512:
+      case ES256:
+      case ES384:
+      case ES512:
+        return HashHelper.sig((PrivateKey)key,internal,mat);
+      default:
+        throw new UnsupportedOperationException();
       }
     }
 
@@ -72,22 +56,15 @@ public class Jwt {
         switch(this) {
         case HS256:
         case HS384:
-        case HS512: {
-          Mac mac = Mac.getInstance(internal);
-          mac.init(key);
-          byte[] sig = mac.doFinal(mat);
-          return Arrays.equals(sig, dat);
-        }
+        case HS512:
+          return HashHelper.hmacval(key, internal, mat, dat);
         case RS256:
         case RS384:
         case RS512:
         case ES256:
         case ES384:
         case ES512: {
-          Signature sig = Signature.getInstance(internal);
-          sig.initVerify((PublicKey)key);
-          sig.update(mat);
-          return sig.verify(dat);
+          return HashHelper.sigval((PublicKey)key, internal, mat, dat);
         }
         default:
           throw new UnsupportedOperationException();

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java?rev=1177398&r1=1177397&r2=1177398&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/io/Compression.java Thu
Sep 29 18:57:08 2011
@@ -162,15 +162,21 @@ public class Compression {
     public static String describe(
         CompressionCodec codec, 
         CompressionCodec... codecs) {
-        if (codec == null)
+        if (codec == null && codecs.length == 0)
           throw new IllegalArgumentException(
             "At least one codec must be specified");
+        int i = 0;
+        if (codec == null) {
+          codec = codecs[0];
+          i = 1;
+        }
         StringBuilder buf = new StringBuilder("\"");
         buf.append(codec.name().toLowerCase());
-        for (int n = codecs.length - 1; n >= 0; n--)
+        for (int n = codecs.length - 1; n >= i; n--)
           buf.append(',')
              .append(codecs[n].name().toLowerCase());
         buf.append('"');
         return buf.toString();
     }
+    
 }

Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java?rev=1177398&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java
(added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java
Thu Sep 29 18:57:08 2011
@@ -0,0 +1,13 @@
+package org.apache.abdera2.common.misc;
+
+public class ExceptionHelper {
+
+  public static RuntimeException propogate(Throwable t) {
+    if (t instanceof RuntimeException)
+      throw (RuntimeException)t;
+    else if (t instanceof Error)
+      throw (Error)t;
+    else return new RuntimeException(t);
+  }
+  
+}

Propchange: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/ExceptionHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java?rev=1177398&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
(added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
Thu Sep 29 18:57:08 2011
@@ -0,0 +1,134 @@
+package org.apache.abdera2.common.security;
+
+import java.security.Key;
+import java.security.MessageDigest;
+import java.security.PrivateKey;
+import java.security.PublicKey;
+import java.security.Signature;
+import java.util.Arrays;
+
+import javax.crypto.Mac;
+
+import org.apache.abdera2.common.misc.ExceptionHelper;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.codec.binary.Hex;
+
+public class HashHelper {
+
+  public static String sig(PrivateKey key, String alg, byte[] mat) {
+    try {
+      Signature sig = Signature.getInstance(alg);
+      sig.initSign((PrivateKey)key);
+      sig.update(mat);
+      byte[] dat = sig.sign();
+      return Base64.encodeBase64URLSafeString(dat);
+    } catch (Throwable t) {
+      throw ExceptionHelper.propogate(t);
+    }
+  }
+  
+  public static String hmac(Key key, String alg, byte[] mat) {
+    try {
+      Mac mac = Mac.getInstance(alg);
+      mac.init(key);
+      byte[] sig = mac.doFinal(mat);
+      return Base64.encodeBase64URLSafeString(sig);
+    } catch (Throwable t) {
+      throw ExceptionHelper.propogate(t);
+    }
+  }
+  
+  public static boolean sigval(PublicKey key, String alg, byte[] mat, byte[] dat) {
+    try {
+      Signature sig = Signature.getInstance(alg);
+      sig.initVerify(key);
+      sig.update(mat);
+      return sig.verify(dat);
+    } catch (Throwable t) {
+      throw ExceptionHelper.propogate(t);
+    }
+  }
+  
+  public static boolean hmacval(Key key, String alg, byte[] mat, byte[] dat) {
+    try {
+      Mac mac = Mac.getInstance(alg);
+      mac.init(key);
+      byte[] sig = mac.doFinal(mat);
+      return Arrays.equals(sig, dat);
+    } catch (Throwable t) {
+      throw ExceptionHelper.propogate(t);
+    }
+  }
+  
+  public static abstract class Hasher {
+    public abstract void update(byte[] buf, int s, int e);
+    protected abstract byte[] digest();
+    public String get() {
+      return Hex.encodeHexString(digest());
+    }
+    public String name() {
+      return getClass().getSimpleName().toLowerCase();
+    }
+  }
+  
+  public static class Md5 extends Hasher {
+    private final MessageDigest md;
+    public Md5() {
+      this.md = init();
+    }
+    private MessageDigest init() {
+      try {
+        return MessageDigest.getInstance("MD5");
+      } catch (Throwable t) {
+        throw ExceptionHelper.propogate(t);
+      }
+    }
+    public void update(byte[] buf, int s, int e) {
+      if (md != null)
+        md.update(buf, s, e);
+    }
+    public byte[] digest() {
+      return md.digest();
+    }
+  }
+  
+  public abstract static class SHA extends Hasher {
+    private final Mac mac;
+    SHA(Key key, String alg) {
+      this.mac = init(key,alg);
+    }
+    private Mac init(Key key, String alg) {
+      try {
+        Mac mac = Mac.getInstance(alg);
+        mac.init(key);
+        return mac;
+      } catch (Throwable t) {
+        throw ExceptionHelper.propogate(t);
+      }
+    }
+    public void update(byte[] buf, int s, int e) {
+      mac.update(buf, s, e);
+    }
+    public byte[] digest() {
+      return mac.doFinal();
+    }
+  }
+  
+  public static class SHA256 extends SHA {
+    public SHA256(Key key) {
+      super(key, "HmacSHA256");
+    }
+  }
+  
+  public static class SHA384 extends SHA {
+    public SHA384(Key key) {
+      super(key, "HmacSHA384");
+    }
+  }
+  
+  public static class SHA512 extends SHA {
+    public SHA512(Key key) {
+      super(key, "HmacSHA512");
+    }
+  }
+}

Propchange: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java?rev=1177398&r1=1177397&r2=1177398&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/BinaryDataObjectExample.java
Thu Sep 29 18:57:08 2011
@@ -10,6 +10,8 @@ import org.apache.abdera2.activities.ext
 import org.apache.abdera2.activities.extra.Extra;
 import org.apache.abdera2.activities.model.IO;
 import org.apache.abdera2.activities.model.objects.BadgeObject;
+import org.apache.abdera2.common.io.Compression.CompressionCodec;
+import org.apache.abdera2.common.security.HashHelper;
 
 /**
  * Illustrates the extension "binary" objectType... this can be useful, 
@@ -27,7 +29,10 @@ public class BinaryDataObjectExample {
     DataHandler dataHandler = new DataHandler(url); 
     
     BinaryObject dataObject = new BinaryObject();
-    dataObject.setContent(dataHandler);
+    dataObject.setContent(
+      dataHandler, 
+      new HashHelper.Md5(), 
+      CompressionCodec.DEFLATE);
     
     BadgeObject badge = new BadgeObject();
     badge.addAttachment(dataObject);
@@ -39,11 +44,19 @@ public class BinaryDataObjectExample {
     
     dataObject = (BinaryObject) badge.getAttachments().iterator().next();
     
+    String md5 = dataObject.getProperty("md5");
+    HashHelper.Md5 check = new HashHelper.Md5();
+    
     InputStream in = dataObject.getInputStream(); 
     byte[] buf = new byte[100];
     int r = -1;
-    while((r = in.read(buf)) > -1)
+    while((r = in.read(buf)) > -1) {
+      check.update(buf, 0, r);
       System.out.write(buf,0,r);
+    }
+    String checks = check.get();
+    
+    System.out.println(checks.equalsIgnoreCase(md5));
     
   }
   



Mime
View raw message