abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1187030 - in /abdera/abdera2/common/src/main/java/org/apache/abdera2/common: http/ iri/ lang/ mediatype/ misc/ pusher/ security/ templates/ text/
Date Thu, 20 Oct 2011 20:09:33 GMT
Author: jmsnell
Date: Thu Oct 20 20:09:32 2011
New Revision: 1187030

URL: http://svn.apache.org/viewvc?rev=1187030&view=rev
Log:
Additional Guava integration...

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/EntityTag.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/iri/IRI.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/Range.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/mediatype/MimeTypeHelper.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/pusher/SelectorListener.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/NormalizationForm.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Slug.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/EntityTag.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/EntityTag.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/EntityTag.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/http/EntityTag.java Thu
Oct 20 20:09:32 2011
@@ -28,6 +28,9 @@ import java.util.List;
 
 import org.apache.abdera2.common.text.UrlEncoding;
 
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+
 /**
  * Implements an EntityTag.
  */
@@ -73,6 +76,54 @@ public class EntityTag 
         return etags;
     }
 
+    public static Predicate<String> matchesAny(final EntityTag tag) {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return matchesAny(tag,input);
+        }
+      };
+    }
+    
+    public static Predicate<String> matchesAnyWeak(final EntityTag tag) {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return matchesAny(tag,input,true);
+        }
+      };
+    }
+    
+    public static Predicate<Iterable<EntityTag>> matchesAnyInList(final EntityTag
tag) {
+      return new Predicate<Iterable<EntityTag>>() {
+        public boolean apply(Iterable<EntityTag> input) {
+          return matchesAny(tag,input,false);
+        }
+      };
+    }
+    
+    public static Predicate<Iterable<EntityTag>> matchesAnyWeakInList(final EntityTag
tag) {
+      return new Predicate<Iterable<EntityTag>>() {
+        public boolean apply(Iterable<EntityTag> input) {
+          return matchesAny(tag,input,true);
+        }
+      };
+    }
+    
+    public static Predicate<String> matchesAny(final String tag) {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return matchesAny(tag,input);
+        }
+      };
+    }
+    
+    public static Predicate<String> matchesAnyWeak(final String tag) {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return matchesAny(tag,input,true);
+        }
+      };
+    }
+    
     public static boolean matchesAny(EntityTag tag1, String tags) {
         return matchesAny(tag1, parseTags(tags), false);
     }
@@ -111,6 +162,30 @@ public class EntityTag 
         return false;
     }
 
+    public static Predicate<EntityTag> matches(final EntityTag tag1) {
+      return new Predicate<EntityTag>() {
+        public boolean apply(EntityTag tag) {
+          return matches(tag1,tag);
+        }
+      };
+    }
+    
+    public static Predicate<EntityTag> matchesWeak(final EntityTag tag1) {
+      return new Predicate<EntityTag>() {
+        public boolean apply(EntityTag tag) {
+          return matches(tag1,tag,true);
+        }
+      };
+    }
+    
+    public static Predicate<String> matches(final String tag1) {
+      return new Predicate<String>() {
+        public boolean apply(String tag) {
+          return matches(tag1,tag);
+        }
+      };
+    }
+    
     public static boolean matches(EntityTag tag1, EntityTag tag2) {
         return tag1.equals(tag2);
     }
@@ -129,6 +204,21 @@ public class EntityTag 
         return tag1.equals(parse(tag2));
     }
 
+    public static Function<String[],EntityTag> variation(final EntityTag tag) {
+      return new Function<String[],EntityTag>() {
+        public EntityTag apply(String[] labels) {
+          if (labels.length > 1) {
+            String label = labels[0];
+            String[] other = new String[labels.length-1];
+            System.arraycopy(labels, 1, other, 0, other.length);
+            return variation(tag,label,other);
+          } else {
+            return variation(tag,labels[0]);
+          }
+        }
+      };
+    }
+    
     /**
      * Produces a variation of the entity tag by appending one or 
      * more labels to the tag. This is helpful when generating 
@@ -181,6 +271,10 @@ public class EntityTag 
         this.wild = wild;
     }
 
+    public Function<String[],EntityTag> variation() {
+      return variation(this);
+    }
+    
     public EntityTag variation(String label, String... labels) {
       return variation(this,label,labels);
     }
@@ -265,6 +359,22 @@ public class EntityTag 
         }
     }
 
+    public static Function<String[],EntityTag> generate() {
+      return new Function<String[],EntityTag>() {
+        public EntityTag apply(String[] material) {
+          return generate(material);
+        }
+      };
+    }
+    
+    public static Function<Object,EntityTag> generateFromObject() {
+      return new Function<Object,EntityTag>() {
+        public EntityTag apply(Object obj) {
+          return generate(obj);
+        }
+      };
+    }
+    
     /**
      * Utility method for generating ETags. Works by concatenating the UTF-8 bytes of the
provided strings then
      * generating an MD5 hash of the result.
@@ -289,6 +399,14 @@ public class EntityTag 
         return new EntityTag(etag);
     }
     
+    public static Predicate<EntityTag> matches(final String... material) {
+      return new Predicate<EntityTag>() {
+        public boolean apply(EntityTag input) {
+          return matches(input,material);
+        }
+      };
+    }
+    
     /**
      * Checks that the passed in ETag matches the ETag generated by the generate method
      */
@@ -296,7 +414,7 @@ public class EntityTag 
         EntityTag etag2 = generate(material);
         return EntityTag.matches(etag, etag2);
     }
-
+    
     public static String toString(EntityTag tag, EntityTag... tags) {
         if (tag == null) return null;
         StringBuilder buf = new StringBuilder();

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/iri/IRI.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/iri/IRI.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/iri/IRI.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/iri/IRI.java Thu Oct 20
20:09:32 2011
@@ -29,6 +29,7 @@ import org.apache.abdera2.common.text.No
 import org.apache.abdera2.common.text.UrlEncoding;
 import org.apache.abdera2.common.text.CharUtils.Profile;
 
+import com.google.common.base.Function;
 import com.ibm.icu.text.IDNA;
 
 public final class IRI implements Serializable, Cloneable {
@@ -596,4 +597,28 @@ public final class IRI implements Serial
       return this.resolve(path + "/");
     }
 
+    public static Function<IRI,IRI> normalizeIRI() {
+      return new Function<IRI,IRI>() {
+        public IRI apply(IRI input) {
+          return input.normalize();
+        }
+      };
+    }
+    
+    public static Function<IRI,IRI> resolveIRI(final IRI base) {
+      return new Function<IRI,IRI>() {
+        public IRI apply(IRI input) {
+          return base.resolve(input);
+        }
+      };
+    }
+    
+    public static Function<IRI,IRI> relativizeIRI(final IRI base) {
+      return new Function<IRI,IRI>() {
+        public IRI apply(IRI input) {
+          return base.relativize(input);
+        }
+      };
+    }
+    
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/Range.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/Range.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/Range.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/Range.java Thu Oct
20 20:09:32 2011
@@ -26,6 +26,9 @@ import java.util.regex.Pattern;
 
 import org.apache.abdera2.common.lang.Subtag.Type;
 
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+
 /**
  * A language range used for matching language tags
  */
@@ -108,6 +111,42 @@ public final class Range 
         return true;
     }
 
+    public Predicate<Lang> matches() {
+      final Range thisRange = this;
+      return new Predicate<Lang>() {
+        public boolean apply(Lang input) {
+          return thisRange.matches(input);
+        }
+      };
+    }
+    
+    public Predicate<String> matchesString() {
+      final Range thisRange = this;
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return thisRange.matches(input);
+        }
+      };
+    }
+    
+    public Predicate<Lang> matchesExtended() {
+      final Range thisRange = this;
+      return new Predicate<Lang>() {
+        public boolean apply(Lang input) {
+          return thisRange.matches(input,true);
+        }
+      };
+    }
+    
+    public Predicate<String> matchesStringExtended() {
+      final Range thisRange = this;
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return thisRange.matches(input,true);
+        }
+      };
+    }
+    
     public boolean matches(String lang) {
         return matches(new Lang(lang), extended);
     }
@@ -165,6 +204,24 @@ public final class Range 
         }
     }
 
+    public Function<Lang[],Lang[]> filter() {
+      final Range thisRange = this;
+      return new Function<Lang[],Lang[]>() {
+        public Lang[] apply(Lang[] input) {
+          return thisRange.filter(input);
+        }
+      };
+    }
+    
+    public Function<String[],String[]> filterString() {
+      final Range thisRange = this;
+      return new Function<String[],String[]>() {
+        public String[] apply(String[] input) {
+          return thisRange.filter(input);
+        }
+      };
+    }
+    
     public Lang[] filter(Lang... lang) {
         List<Lang> langs = new LinkedList<Lang>();
         for (Lang l : lang)
@@ -188,7 +245,7 @@ public final class Range 
     public static String[] filter(String range, String... lang) {
         return new Range(range).filter(lang);
     }
-
+    
     public static boolean matches(String range, Lang lang, boolean extended) {
         return new Range(range, extended).matches(lang);
     }
@@ -205,6 +262,22 @@ public final class Range 
         return new Range(range).matches(lang);
     }
 
+    public static Predicate<String> matchesString(final String range) {
+      return new Range(range).matchesString();
+    }
+    
+    public static Predicate<String> matchesStringExtended(final String range) {
+      return new Range(range).matchesStringExtended();
+    }
+    
+    public static Predicate<Lang> matchesLang(final String range) {
+      return new Range(range).matches();
+    }
+    
+    public static Predicate<Lang> matchesLangExtended(final String range) {
+      return new Range(range).matchesExtended();
+    }
+    
     // Parsing logic //
 
     private static final String SEP = "\\s*[-_]\\s*";

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/mediatype/MimeTypeHelper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/mediatype/MimeTypeHelper.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/mediatype/MimeTypeHelper.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/mediatype/MimeTypeHelper.java
Thu Oct 20 20:09:32 2011
@@ -28,6 +28,10 @@ import javax.activation.MimeTypeParamete
 
 import org.apache.abdera2.common.Constants;
 
+import com.google.common.base.Equivalence;
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+
 /**
  * Utilities for working with MIME Media Types
  */
@@ -45,6 +49,14 @@ public final class MimeTypeHelper {
             return null;
         }
     }
+    
+    public static MimeType create(String mimeType) {
+      try {
+        return new MimeType(mimeType);
+      } catch (javax.activation.MimeTypeParseException e) {
+        throw MimeTypeParseException.wrap(e);
+      }
+    }
 
     private static MimeType createWildcard() {
         try {
@@ -54,6 +66,30 @@ public final class MimeTypeHelper {
         }
     }
 
+    public static Predicate<String> isMatch(final String a) {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isMatch(a,input);
+        }
+      };
+    }
+    
+    public static Predicate<MimeType> isMatch(final MimeType a) {
+      return new Predicate<MimeType>() {
+        public boolean apply(MimeType input) {
+          return isMatch(a,input);
+        }
+      };
+    }
+    
+    public static Predicate<MimeType> isMatch(final MimeType a, final boolean includeparams)
{
+      return new Predicate<MimeType>() {
+        public boolean apply(MimeType input) {
+          return isMatch(a,input,includeparams);
+        }
+      };
+    }
+    
     /**
      * Returns true if media type a matches media type b
      */
@@ -126,6 +162,58 @@ public final class MimeTypeHelper {
         return (actual != null && actual.equalsIgnoreCase(expected) || true);
     }
 
+    public static Predicate<String> isApp() {
+      return isMatch(Constants.APP_MEDIA_TYPE);
+    }
+    
+    public static Predicate<String> isAtom() {
+      return isMatch(Constants.ATOM_MEDIA_TYPE);
+    }
+    
+    public static Predicate<String> isJson() {
+      return isMatch(Constants.JSON_MEDIA_TYPE);
+    }
+    
+    public static Predicate<String> isAtomEntry() {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isEntry(input);
+        }
+      };
+    }
+    
+    public static Predicate<String> isAtomFeed() {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isFeed(input);
+        }
+      };
+    }
+    
+    public static Predicate<String> isXml() {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isXml(input);
+        }
+      };
+    }
+    
+    public static Predicate<String> isText() {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isText(input);
+        }
+      };
+    }
+    
+    public static Predicate<String> isMimeType() {
+      return new Predicate<String>() {
+        public boolean apply(String input) {
+          return isMimeType(input);
+        }
+      };
+    }
+    
     /**
      * Returns true if media type a matches application/atomsrv+xml
      */
@@ -212,6 +300,14 @@ public final class MimeTypeHelper {
         return answer;
     }
 
+    public static Function<String[],String[]> condense() {
+      return new Function<String[],String[]>() {
+        public String[] apply(String[] input) {
+          return condense(input);
+        }
+      };
+    }
+    
     /**
      * This will take an array of media types and will condense them based on wildcards,
etc. For instance,
      * condense("image/png", "image/jpg", "image/*") condenses to [image/*] condense("application/atom",
@@ -292,4 +388,17 @@ public final class MimeTypeHelper {
         return isMatch(Constants.MULTIPART_RELATED_TYPE, a);
     }
 
+    /**
+     * Returns an Equivalence object to compare two MimeTypes
+     */
+    public Equivalence<MimeType> equivalence() {
+      return new Equivalence<MimeType>() {
+        protected boolean doEquivalent(MimeType a, MimeType b) {
+          return isMatch(a,b);
+        }
+        protected int doHash(MimeType t) {
+          return t.hashCode();
+        }        
+      };
+    }
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
Thu Oct 20 20:09:32 2011
@@ -1,6 +1,15 @@
 package org.apache.abdera2.common.misc;
 
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.regex.Pattern;
+
+import org.apache.abdera2.common.selector.PropertySelector;
+import org.apache.abdera2.common.text.CodepointMatcher;
+
 import com.google.common.base.Predicate; 
+import com.google.common.base.Predicates;
+
 import static com.google.common.base.Preconditions.*;
 
 public final class MorePredicates {
@@ -24,4 +33,61 @@ public final class MorePredicates {
       }
     };
   }
+  
+  /**
+   * Returns a Predicate that checks if the named property of instances of
+   * the specified class are null. The named property MUST NOT be private 
+   * and MUST NOT require any input parameters. The method name is case
+   * sensitive. 
+   */
+  public static <T>Predicate<T> isNull(Class<T> _class, String method)
{
+    return PropertySelector.<T>create(_class, method, Predicates.isNull());
+  }
+  
+  /**
+   * Returns a Predicate that checks if the named property of instances of
+   * the specified class are not null. The named property MUST NOT be private 
+   * and MUST NOT require any input parameters. The method name is case
+   * sensitive. 
+   */
+  public static <T>Predicate<T> isNotNull(Class<T> _class, String method)
{
+    return PropertySelector.<T>create(_class, method, Predicates.not(Predicates.isNull()));
+  }
+  
+  /**
+   * Returns a Predicate that checks if the value of a named property of 
+   * instances of the specified class is an instance of the given test class 
+   */
+  public static <T>Predicate<T> instanceOf(Class<T> _class, String method,
Class<?> _test) {
+    return PropertySelector.<T>create(_class, method, Predicates.instanceOf(_test));
+  }
+  
+  public static <T>Predicate<T> assignableFrom(Class<T> _class, String
method, Class<?> _test) {
+    return PropertySelector.<T>create(_class, method, Predicates.assignableFrom(_test));
+  }
+  
+  public static <T>Predicate<T> containsPattern(Class<T> _class, String
method, Pattern pattern) {
+    return PropertySelector.<T>create(_class, method, Predicates.contains(pattern));
+  }
+  
+  public static <T>Predicate<T> containsPattern(Class<T> _class, String
method, String pattern) {
+    return containsPattern(_class,method,Pattern.compile(pattern));
+  }
+  
+  public static <T>Predicate<T> matches(Class<T> _class, String method,
CodepointMatcher matcher) {
+    return PropertySelector.<T>create(_class, method, matcher);
+  }
+  
+  public static <T>Predicate<T> equalTo(Class<T> _class, String method,
Object obj) {
+    return PropertySelector.<T>create(_class, method, Predicates.equalTo(obj));
+  }
+  
+  public static <T>Predicate<T> in(Class<T> _class, String method, Collection<T>
items) {
+    return PropertySelector.<T>create(_class, method, Predicates.in(items));
+  }
+  
+  public static <T>Predicate<T> in(Class<T> _class, String method, T...
items) {
+    return in(_class,method,Arrays.asList(items));
+  }
+  
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/pusher/SelectorListener.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/pusher/SelectorListener.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/pusher/SelectorListener.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/pusher/SelectorListener.java
Thu Oct 20 20:09:32 2011
@@ -21,11 +21,11 @@ import org.apache.abdera2.common.selecto
 public class SelectorListener<T> 
   implements Listener<T> {
 
-  private final Selector<?> selector;
+  private final Selector<T> selector;
   private final Listener<T> listener;
 
   public SelectorListener(
-    Selector<?> selector,
+    Selector<T> selector,
     Listener<T> listener) {
     this.selector = selector;
     this.listener = listener;

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/ApiKey.java Thu
Oct 20 20:09:32 2011
@@ -2,11 +2,11 @@ package org.apache.abdera2.common.securi
 
 import java.security.Key;
 
-import javax.crypto.spec.SecretKeySpec;
-
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;
 
+import com.google.common.base.Supplier;
+
 /**
  * Utility Class used for Generating API Keys
  */
@@ -75,7 +75,7 @@ public class ApiKey extends KeyBase {
     return new ApiKey(key,"HmacSHA1",20);
   }
   
-  public static ApiKey WEAK(SecretKeySpec key) {
+  public static ApiKey WEAK(Key key) {
     return new ApiKey(key,"HmacSHA1",20);
   }
   
@@ -87,7 +87,7 @@ public class ApiKey extends KeyBase {
     return new ApiKey(key,"HmacSHA256",256);
   }
   
-  public static ApiKey MEDIUM(SecretKeySpec key) {
+  public static ApiKey MEDIUM(Key key) {
     return new ApiKey(key,"HmacSHA256",256);
   }
   
@@ -99,11 +99,110 @@ public class ApiKey extends KeyBase {
     return new ApiKey(key,"HmacSHA512",512);
   }
   
-  public static ApiKey STRONG(SecretKeySpec key) {
+  public static ApiKey STRONG(Key key) {
     return new ApiKey(key,"HmacSHA512",512);
   }
   
   public static ApiKey STRONG(String key) {
     return new ApiKey(key,"HmacSHA512",512);
   }
+  
+  public static Supplier<String> supplier(ApiKey key) {
+    return new ApiKeySupplier(key);
+  }
+  
+  public static Supplier<String> supplierHex(ApiKey key) {
+    return new ApiKeySupplier(key,true);
+  }
+  
+  public static Supplier<String> weakSupplier(byte[] key) {
+    return new ApiKeySupplier(WEAK(key));
+  }
+  
+  public static Supplier<String> weakSupplier(Key key) {
+    return new ApiKeySupplier(WEAK(key));
+  }
+  
+  public static Supplier<String> weakSupplier(String key) {
+    return new ApiKeySupplier(WEAK(key));
+  }
+  
+  public static Supplier<String> weakSupplierHex(byte[] key) {
+    return new ApiKeySupplier(WEAK(key),true);
+  }
+  
+  public static Supplier<String> weakSupplierHex(Key key) {
+    return new ApiKeySupplier(WEAK(key),true);
+  }
+  
+  public static Supplier<String> weakSupplierHex(String key) {
+    return new ApiKeySupplier(WEAK(key),true);
+  }
+  
+  public static Supplier<String> mediumSupplier(byte[] key) {
+    return new ApiKeySupplier(MEDIUM(key));
+  }
+  
+  public static Supplier<String> mediumSupplier(Key key) {
+    return new ApiKeySupplier(MEDIUM(key));
+  }
+  
+  public static Supplier<String> mediumSupplier(String key) {
+    return new ApiKeySupplier(MEDIUM(key));
+  }
+  
+  public static Supplier<String> mediumSupplierHex(byte[] key) {
+    return new ApiKeySupplier(MEDIUM(key),true);
+  }
+  
+  public static Supplier<String> mediumSupplierHex(Key key) {
+    return new ApiKeySupplier(MEDIUM(key),true);
+  }
+  
+  public static Supplier<String> mediumSupplierHex(String key) {
+    return new ApiKeySupplier(MEDIUM(key),true);
+  }
+  
+  public static Supplier<String> strongSupplier(byte[] key) {
+    return new ApiKeySupplier(STRONG(key));
+  }
+  
+  public static Supplier<String> strongSupplier(Key key) {
+    return new ApiKeySupplier(STRONG(key));
+  }
+  
+  public static Supplier<String> strongSupplier(String key) {
+    return new ApiKeySupplier(STRONG(key));
+  }
+  
+  public static Supplier<String> strongSupplierHex(byte[] key) {
+    return new ApiKeySupplier(STRONG(key),true);
+  }
+  
+  public static Supplier<String> strongSupplierHex(Key key) {
+    return new ApiKeySupplier(STRONG(key),true);
+  }
+  
+  public static Supplier<String> strongSupplierHex(String key) {
+    return new ApiKeySupplier(STRONG(key),true);
+  }
+  
+  private static class ApiKeySupplier 
+    implements Supplier<String> {
+    private final ApiKey key;
+    private final boolean hex;
+    ApiKeySupplier(ApiKey key) {
+      this.key = key;
+      this.hex = false;
+    }
+    ApiKeySupplier(ApiKey key, boolean hex) {
+      this.key = key;
+      this.hex = hex;
+    }
+    public String get() {
+      return !hex ? key.generateNext() : key.generateNextHex();
+    }
+    
+  }
+
 }

Modified: 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=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/HashHelper.java
Thu Oct 20 20:09:32 2011
@@ -1,6 +1,8 @@
 package org.apache.abdera2.common.security;
 
+import java.io.InputStream;
 import java.security.Key;
+
 import java.security.MessageDigest;
 import java.security.PrivateKey;
 import java.security.PublicKey;
@@ -13,10 +15,55 @@ import org.apache.abdera2.common.misc.Ex
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;
 
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Supplier;
+
+@SuppressWarnings("unchecked")
 public final class HashHelper {
 
   private HashHelper() {}
   
+  public static Function<byte[],String> sig(
+    final PrivateKey key, final String alg) {
+      return new Function<byte[],String>() {
+        public String apply(byte[] input) {
+          return sig(key,alg,input);
+        }
+      };
+  }
+  
+  public static Function<byte[],String> hmac(
+    final Key key, final String alg) {
+      return new Function<byte[],String>() {
+        public String apply(byte[] input) {
+          return hmac(key,alg,input);
+        }
+      };
+  }
+  
+  public static Predicate<byte[]> signatureValid(
+    final PublicKey key, 
+    final String alg, 
+    final byte[] source) {
+    return new Predicate<byte[]>() {
+      public boolean apply(byte[] mat) {
+        return sigval(key,alg,source,mat);
+      }
+    };
+  }
+  
+  public static Predicate<byte[]> hmacValid(
+      final Key key, 
+      final String alg, 
+      final byte[] source) {
+      return new Predicate<byte[]>() {
+        public boolean apply(byte[] mat) {
+          return hmacval(key,alg,source,mat);
+        }
+      };
+    }
+  
   public static String sig(PrivateKey key, String alg, byte[] mat) {
     try {
       Signature sig = Signature.getInstance(alg);
@@ -62,8 +109,13 @@ public final class HashHelper {
     }
   }
   
-  public static abstract class Hasher {
-    public abstract void update(byte[] buf, int s, int e);
+  public static abstract class Hasher 
+    implements Supplier<String> {
+    public <T extends Hasher>T update(byte[] buf) {
+      update(buf,0,buf.length);
+      return (T)this;
+    }
+    public abstract <T extends Hasher>T update(byte[] buf, int s, int e);
     protected abstract byte[] digest();
     public String get() {
       return Hex.encodeHexString(digest());
@@ -72,6 +124,14 @@ public final class HashHelper {
       return getClass().getSimpleName().toLowerCase();
     }
   }
+    
+  public static Function<byte[],String> md5() {
+    return new Function<byte[],String>() {
+      public String apply(byte[] input) {
+        return new Md5().update(input).get();
+      }
+    };
+  }
   
   public static class Md5 extends Hasher {
     private final MessageDigest md;
@@ -85,9 +145,10 @@ public final class HashHelper {
         throw ExceptionHelper.propogate(t);
       }
     }
-    public void update(byte[] buf, int s, int e) {
+    public <T extends Hasher>T update(byte[] buf, int s, int e) {
       if (md != null)
         md.update(buf, s, e);
+      return (T)this;
     }
     public byte[] digest() {
       return md.digest();
@@ -108,14 +169,40 @@ public final class HashHelper {
         throw ExceptionHelper.propogate(t);
       }
     }
-    public void update(byte[] buf, int s, int e) {
+    public <T extends Hasher>T update(byte[] buf, int s, int e) {
       mac.update(buf, s, e);
+      return (T)this;
     }
     public byte[] digest() {
       return mac.doFinal();
     }
   }
   
+  
+  public static Function<byte[],String> sha256(final Key key) {
+    return new Function<byte[],String>() {
+      public String apply(byte[] input) {
+        return new SHA256(key).update(input).get();
+      }
+    };
+  }
+  
+  public static Function<byte[],String> sha384(final Key key) {
+    return new Function<byte[],String>() {
+      public String apply(byte[] input) {
+        return new SHA384(key).update(input).get();
+      }
+    };
+  }
+  
+  public static Function<byte[],String> sha512(final Key key) {
+    return new Function<byte[],String>() {
+      public String apply(byte[] input) {
+        return new SHA512(key).update(input).get();
+      }
+    };
+  }
+  
   public static class SHA256 extends SHA {
     public SHA256(Key key) {
       super(key, "HmacSHA256");
@@ -133,4 +220,86 @@ public final class HashHelper {
       super(key, "HmacSHA512");
     }
   }
+  
+  private static abstract class IOHasherFunction 
+    implements Function<InputStream,String> {
+    protected abstract Hasher hasher();
+    public String apply(InputStream input) {
+      try {
+        Md5 md5 = new Md5();
+        byte[] buf = new byte[1024];
+        int r = -1;
+        while((r = input.read(buf)) > -1)
+          md5.update(buf, 0, r);
+        return md5.get();
+      } catch (Throwable t) {
+        throw ExceptionHelper.propogate(t);
+      }
+    }
+  }
+  
+  public static Function<InputStream,String> md5stream() {
+    return new IOHasherFunction() {
+      protected Hasher hasher() {
+        return new Md5();
+      }
+    };
+  }
+  
+  public static Function<InputStream,String> sha256stream(final Key key) {
+    return new IOHasherFunction() {
+      protected Hasher hasher() {
+        return new SHA256(key);
+      }
+    };
+  }
+  
+  public static Function<InputStream,String> sha384stream(final Key key) {
+    return new IOHasherFunction() {
+      protected Hasher hasher() {
+        return new SHA384(key);
+      }
+    };
+  }
+  
+  public static Function<InputStream,String> sha512stream(final Key key) {
+    return new IOHasherFunction() {
+      protected Hasher hasher() {
+        return new SHA512(key);
+      }
+    };
+  }
+  
+  public static String md5(InputStream in) {
+    return md5stream().apply(in);
+  }
+  
+  public static String sha256(Key key, InputStream in) {
+    return sha256stream(key).apply(in);
+  }
+  
+  public static String sha384(Key key, InputStream in) {
+    return sha384stream(key).apply(in);
+  }
+  
+  public static String sha512(Key key, InputStream in) {
+    return sha512stream(key).apply(in);
+  }
+  
+  public static String md5(byte[] in) {
+    return md5().apply(in);
+  }
+  
+  public static String sha256(Key key, byte[] in) {
+    return sha256(key).apply(in);
+  }
+  
+  public static String sha384(Key key, byte[] in) {
+    return sha384(key).apply(in);
+  }
+  
+  public static String sha512(Key key, byte[] in) {
+    return sha512(key).apply(in);
+  }
+  
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/security/Otp.java Thu Oct
20 20:09:32 2011
@@ -1,6 +1,8 @@
 package org.apache.abdera2.common.security;
 import java.security.Key;
 
+import com.google.common.base.Supplier;
+
 /**
  * Utility class for generating One-Time-Passwords using the HOTP algorithm
  */
@@ -72,6 +74,57 @@ public abstract class Otp extends KeyBas
       len,'0');
   } 
   
+  private static class OtpSupplier implements Supplier<String> {
+    private final Otp otp;
+    OtpSupplier(Otp otp) {
+      this.otp = otp;
+    }
+    public String get() {
+      return otp.generateNext();
+    }
+    
+  }
+  
+  public static Supplier<String> supplier(Otp otp) {
+    return new OtpSupplier(otp);
+  }
+  
+  public static Supplier<String> totpSupplier(byte[] key, int step, int size) {
+    return new OtpSupplier(new Totp(step,key,size));
+  }
+  
+  public static Supplier<String> totpSupplier(byte[] key, int step) {
+    return new OtpSupplier(new Totp(step,key));
+  }
+  
+  public static Supplier<String> totpSupplier(byte[] key, int step, int size, String
alg) {
+    return new OtpSupplier(new Totp(step,key,alg,size));
+  }
+  
+  public static Supplier<String> totpSupplier(Key key, int step, int size) {
+    return new OtpSupplier(new Totp(step,key,size));
+  }
+  
+  public static Supplier<String> totpSupplier(Key key, int step) {
+    return new OtpSupplier(new Totp(step,key));
+  }
+  
+  public static Supplier<String> totpSupplier(Key key, int step, int size, String alg)
{
+    return new OtpSupplier(new Totp(step,key,alg,size));
+  }
+  
+  public static Supplier<String> totpSupplier(String key, int step, int size) {
+    return new OtpSupplier(new Totp(step,key,size));
+  }
+  
+  public static Supplier<String> totpSupplier(String key, int step) {
+    return new OtpSupplier(new Totp(step,key));
+  }
+  
+  public static Supplier<String> totpSupplier(String key, int step, int size, String
alg) {
+    return new OtpSupplier(new Totp(step,key,alg,size));
+  }
+  
   /**
    * Utility implementation of the Time-based One Time Password (TOTP) 
    * algorithm. 

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
Thu Oct 20 20:09:32 2011
@@ -33,6 +33,7 @@ import org.apache.abdera2.common.templat
 import org.apache.abdera2.common.templates.Template;
 import org.apache.abdera2.common.templates.Expression.VarSpec;
 
+import com.google.common.base.Function;
 import com.google.common.base.Supplier;
 import static com.google.common.base.Preconditions.*;
 
@@ -263,6 +264,10 @@ public final class Template implements I
       return new TSupplier(this,context);
     }
     
+    public Function<Object,String> asFunction() {
+      return new TFunction(this);
+    }
+    
     private static class TSupplier implements Supplier<String> {
       private final Template template;
       private final Object context;
@@ -274,4 +279,14 @@ public final class Template implements I
         return template.expand(context);
       }
     }
+    
+    private static class TFunction implements Function<Object,String> {
+      private final Template template;
+      TFunction(Template template) {
+        this.template = template;
+      }
+      public String apply(Object object) {
+        return template.expand(object);
+      }
+    }
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java Thu Oct
20 20:09:32 2011
@@ -26,6 +26,8 @@ import org.apache.commons.codec.StringEn
 import org.apache.commons.codec.net.BCodec;
 import org.apache.commons.codec.net.QCodec;
 
+import com.google.common.base.Function;
+
 public enum Codec {
     B, 
     Q,
@@ -158,4 +160,53 @@ public enum Codec {
         return false;
       }
     }
+  
+  public static Function<String,String> decode() {
+    return new DecodeFunction();
+  }
+  
+  public static Function<String,String> encodeB(String charset) {
+    return new EncodeFunction(Codec.B,charset);
+  }
+  
+  public static Function<String,String> encodeB() {
+    return encodeB("UTF-8");
+  }
+  
+  public static Function<String,String> encodeQ(String charset) {
+    return new EncodeFunction(Codec.Q,charset);
+  }
+  
+  public static Function<String,String> encodeQ() {
+    return encodeQ("UTF-8");
+  }
+  
+  public static Function<String,String> encodeStar() {
+    return new EncodeFunction(Codec.STAR, "UTF-8");
+  }
+    
+  public static Function<String,String> encodeStar(String charset) {
+    return new EncodeFunction(Codec.STAR, charset);
+  }
+  
+  private static class EncodeFunction 
+    implements Function<String,String> {
+    private final Codec c;
+    private final String charset;
+    EncodeFunction(Codec c, String charset) {
+      this.c = c;
+      this.charset = charset;
+    }
+    public String apply(String input) {
+      return encode(input,charset,c);
+    }
+  }
+  
+  private static class DecodeFunction
+    implements Function<String,String> {
+    public String apply(String input) {
+      return decode(input);
+    }
+  }
+  
 }
\ No newline at end of file

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/NormalizationForm.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/NormalizationForm.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/NormalizationForm.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/NormalizationForm.java
Thu Oct 20 20:09:32 2011
@@ -19,6 +19,7 @@ package org.apache.abdera2.common.text;
 
 import com.google.common.base.Equivalence;
 import com.google.common.base.Equivalences;
+import com.google.common.base.Function;
 import com.ibm.icu.text.Normalizer2;
 import com.ibm.icu.text.Normalizer2.Mode;
 
@@ -52,7 +53,7 @@ public enum NormalizationForm {
       return ne.equivalent(s1, s2);
     }
     
-    public NormalizedEquivalence equivalence() {
+    public Equivalence<CharSequence> equivalence() {
       return ne;
     }
     
@@ -80,4 +81,31 @@ public enum NormalizationForm {
       }
       
     }
+    
+  private static class NormalFunction 
+    implements Function<CharSequence,CharSequence> {
+    private final NormalizationForm form;
+    NormalFunction(NormalizationForm form) {
+      this.form = form;
+    }
+    public CharSequence apply(CharSequence input) {
+      return form.normalize(input);
+    }
+  }
+  
+  public static Function<CharSequence,CharSequence> D() {
+    return new NormalFunction(NormalizationForm.D);
+  }
+  
+  public static Function<CharSequence,CharSequence> C() {
+    return new NormalFunction(NormalizationForm.C);
+  }
+  
+  public static Function<CharSequence,CharSequence> KD() {
+    return new NormalFunction(NormalizationForm.KD);
+  }
+  
+  public static Function<CharSequence,CharSequence> KC() {
+    return new NormalFunction(NormalizationForm.KC);
+  }
 }
\ No newline at end of file

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Slug.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Slug.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Slug.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Slug.java Thu Oct 20
20:09:32 2011
@@ -19,6 +19,8 @@ package org.apache.abdera2.common.text;
 
 import org.apache.abdera2.common.text.CharUtils.Profile;
 
+import com.google.common.base.Function;
+
 public class Slug {
 
     public static final String SANITIZE_PATTERN = "[^A-Za-z0-9\\%!$&\\\\'()*+,;=_]+";
@@ -130,4 +132,74 @@ public class Slug {
       return true;
     }
     
+    public static Function<String,Slug> createFunction() {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler, 
+      final boolean lower) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler, lower);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler, 
+      final boolean lower, 
+      final NormalizationForm nf) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler, lower, nf);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler, 
+      final boolean lower, 
+      final NormalizationForm nf, 
+      final String pattern) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler, lower, nf, pattern);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler, 
+      final boolean lower, 
+      final String pattern) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler, lower, pattern);
+        }
+      };
+    }
+    
+    public static Function<String,Slug> createFunction(
+      final String filler, 
+      final String pattern) {
+      return new Function<String,Slug>() {
+        public Slug apply(String input) {
+          return create(input, filler, pattern);
+        }
+      };
+    }
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java?rev=1187030&r1=1187029&r2=1187030&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java Thu
Oct 20 20:09:32 2011
@@ -32,8 +32,11 @@ import java.io.UnsupportedEncodingExcept
 import java.io.Writer;
 import java.nio.CharBuffer;
 
+import org.apache.abdera2.common.misc.ExceptionHelper;
 import org.apache.abdera2.common.text.CharUtils.Profile;
 
+import com.google.common.base.Function;
+
 /**
  * Performs URL Percent Encoding
  */
@@ -547,4 +550,38 @@ public final class UrlEncoding {
         return (byte)(decode(c1, 4) | decode(c2, 0));
     }
 
+    public static Function<CharSequence,String> encoder(
+        final Profile... profiles) {
+        return encoder("UTF-8", profiles);
+      }
+    
+    public static Function<CharSequence,String> encoder(
+      final String charset, 
+      final Profile... profiles) {
+      return new Function<CharSequence,String>() {
+        public String apply(CharSequence input) {
+          try {
+            return encode(input, charset, profiles);
+          } catch (Throwable t) {
+            throw ExceptionHelper.propogate(t);
+          }
+        }
+      };
+    }
+    
+    public static Function<String,String> decoder() {
+      return decoder("UTF-8");
+    }
+    
+    public static Function<String,String> decoder(final String charset) {
+      return new Function<String,String>() {
+        public String apply(String input) {
+          try {
+            return decode(input,charset);
+          } catch (Throwable t) {
+            throw ExceptionHelper.propogate(t);
+          }
+        }
+      };
+    }
 }



Mime
View raw message