Return-Path: X-Original-To: apmail-abdera-commits-archive@www.apache.org Delivered-To: apmail-abdera-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A1DB37718 for ; Thu, 20 Oct 2011 20:10:03 +0000 (UTC) Received: (qmail 22005 invoked by uid 500); 20 Oct 2011 20:10:03 -0000 Delivered-To: apmail-abdera-commits-archive@abdera.apache.org Received: (qmail 21962 invoked by uid 500); 20 Oct 2011 20:10:03 -0000 Mailing-List: contact commits-help@abdera.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@abdera.apache.org Delivered-To: mailing list commits@abdera.apache.org Received: (qmail 21955 invoked by uid 99); 20 Oct 2011 20:10:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Oct 2011 20:10:03 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Oct 2011 20:09:56 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 24E352388962 for ; Thu, 20 Oct 2011 20:09:34 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@abdera.apache.org From: jmsnell@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111020200934.24E352388962@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 matchesAny(final EntityTag tag) { + return new Predicate() { + public boolean apply(String input) { + return matchesAny(tag,input); + } + }; + } + + public static Predicate matchesAnyWeak(final EntityTag tag) { + return new Predicate() { + public boolean apply(String input) { + return matchesAny(tag,input,true); + } + }; + } + + public static Predicate> matchesAnyInList(final EntityTag tag) { + return new Predicate>() { + public boolean apply(Iterable input) { + return matchesAny(tag,input,false); + } + }; + } + + public static Predicate> matchesAnyWeakInList(final EntityTag tag) { + return new Predicate>() { + public boolean apply(Iterable input) { + return matchesAny(tag,input,true); + } + }; + } + + public static Predicate matchesAny(final String tag) { + return new Predicate() { + public boolean apply(String input) { + return matchesAny(tag,input); + } + }; + } + + public static Predicate matchesAnyWeak(final String tag) { + return new Predicate() { + 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 matches(final EntityTag tag1) { + return new Predicate() { + public boolean apply(EntityTag tag) { + return matches(tag1,tag); + } + }; + } + + public static Predicate matchesWeak(final EntityTag tag1) { + return new Predicate() { + public boolean apply(EntityTag tag) { + return matches(tag1,tag,true); + } + }; + } + + public static Predicate matches(final String tag1) { + return new Predicate() { + 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 variation(final EntityTag tag) { + return new Function() { + 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 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 generate() { + return new Function() { + public EntityTag apply(String[] material) { + return generate(material); + } + }; + } + + public static Function generateFromObject() { + return new Function() { + 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 matches(final String... material) { + return new Predicate() { + 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 normalizeIRI() { + return new Function() { + public IRI apply(IRI input) { + return input.normalize(); + } + }; + } + + public static Function resolveIRI(final IRI base) { + return new Function() { + public IRI apply(IRI input) { + return base.resolve(input); + } + }; + } + + public static Function relativizeIRI(final IRI base) { + return new Function() { + 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 matches() { + final Range thisRange = this; + return new Predicate() { + public boolean apply(Lang input) { + return thisRange.matches(input); + } + }; + } + + public Predicate matchesString() { + final Range thisRange = this; + return new Predicate() { + public boolean apply(String input) { + return thisRange.matches(input); + } + }; + } + + public Predicate matchesExtended() { + final Range thisRange = this; + return new Predicate() { + public boolean apply(Lang input) { + return thisRange.matches(input,true); + } + }; + } + + public Predicate matchesStringExtended() { + final Range thisRange = this; + return new Predicate() { + 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 filter() { + final Range thisRange = this; + return new Function() { + public Lang[] apply(Lang[] input) { + return thisRange.filter(input); + } + }; + } + + public Function filterString() { + final Range thisRange = this; + return new Function() { + public String[] apply(String[] input) { + return thisRange.filter(input); + } + }; + } + public Lang[] filter(Lang... lang) { List langs = new LinkedList(); 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 matchesString(final String range) { + return new Range(range).matchesString(); + } + + public static Predicate matchesStringExtended(final String range) { + return new Range(range).matchesStringExtended(); + } + + public static Predicate matchesLang(final String range) { + return new Range(range).matches(); + } + + public static Predicate 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 isMatch(final String a) { + return new Predicate() { + public boolean apply(String input) { + return isMatch(a,input); + } + }; + } + + public static Predicate isMatch(final MimeType a) { + return new Predicate() { + public boolean apply(MimeType input) { + return isMatch(a,input); + } + }; + } + + public static Predicate isMatch(final MimeType a, final boolean includeparams) { + return new Predicate() { + 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 isApp() { + return isMatch(Constants.APP_MEDIA_TYPE); + } + + public static Predicate isAtom() { + return isMatch(Constants.ATOM_MEDIA_TYPE); + } + + public static Predicate isJson() { + return isMatch(Constants.JSON_MEDIA_TYPE); + } + + public static Predicate isAtomEntry() { + return new Predicate() { + public boolean apply(String input) { + return isEntry(input); + } + }; + } + + public static Predicate isAtomFeed() { + return new Predicate() { + public boolean apply(String input) { + return isFeed(input); + } + }; + } + + public static Predicate isXml() { + return new Predicate() { + public boolean apply(String input) { + return isXml(input); + } + }; + } + + public static Predicate isText() { + return new Predicate() { + public boolean apply(String input) { + return isText(input); + } + }; + } + + public static Predicate isMimeType() { + return new Predicate() { + 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 condense() { + return new Function() { + 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 equivalence() { + return new Equivalence() { + 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 Predicate isNull(Class _class, String method) { + return PropertySelector.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 Predicate isNotNull(Class _class, String method) { + return PropertySelector.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 Predicate instanceOf(Class _class, String method, Class _test) { + return PropertySelector.create(_class, method, Predicates.instanceOf(_test)); + } + + public static Predicate assignableFrom(Class _class, String method, Class _test) { + return PropertySelector.create(_class, method, Predicates.assignableFrom(_test)); + } + + public static Predicate containsPattern(Class _class, String method, Pattern pattern) { + return PropertySelector.create(_class, method, Predicates.contains(pattern)); + } + + public static Predicate containsPattern(Class _class, String method, String pattern) { + return containsPattern(_class,method,Pattern.compile(pattern)); + } + + public static Predicate matches(Class _class, String method, CodepointMatcher matcher) { + return PropertySelector.create(_class, method, matcher); + } + + public static Predicate equalTo(Class _class, String method, Object obj) { + return PropertySelector.create(_class, method, Predicates.equalTo(obj)); + } + + public static Predicate in(Class _class, String method, Collection items) { + return PropertySelector.create(_class, method, Predicates.in(items)); + } + + public static Predicate in(Class _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 implements Listener { - private final Selector selector; + private final Selector selector; private final Listener listener; public SelectorListener( - Selector selector, + Selector selector, Listener 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 supplier(ApiKey key) { + return new ApiKeySupplier(key); + } + + public static Supplier supplierHex(ApiKey key) { + return new ApiKeySupplier(key,true); + } + + public static Supplier weakSupplier(byte[] key) { + return new ApiKeySupplier(WEAK(key)); + } + + public static Supplier weakSupplier(Key key) { + return new ApiKeySupplier(WEAK(key)); + } + + public static Supplier weakSupplier(String key) { + return new ApiKeySupplier(WEAK(key)); + } + + public static Supplier weakSupplierHex(byte[] key) { + return new ApiKeySupplier(WEAK(key),true); + } + + public static Supplier weakSupplierHex(Key key) { + return new ApiKeySupplier(WEAK(key),true); + } + + public static Supplier weakSupplierHex(String key) { + return new ApiKeySupplier(WEAK(key),true); + } + + public static Supplier mediumSupplier(byte[] key) { + return new ApiKeySupplier(MEDIUM(key)); + } + + public static Supplier mediumSupplier(Key key) { + return new ApiKeySupplier(MEDIUM(key)); + } + + public static Supplier mediumSupplier(String key) { + return new ApiKeySupplier(MEDIUM(key)); + } + + public static Supplier mediumSupplierHex(byte[] key) { + return new ApiKeySupplier(MEDIUM(key),true); + } + + public static Supplier mediumSupplierHex(Key key) { + return new ApiKeySupplier(MEDIUM(key),true); + } + + public static Supplier mediumSupplierHex(String key) { + return new ApiKeySupplier(MEDIUM(key),true); + } + + public static Supplier strongSupplier(byte[] key) { + return new ApiKeySupplier(STRONG(key)); + } + + public static Supplier strongSupplier(Key key) { + return new ApiKeySupplier(STRONG(key)); + } + + public static Supplier strongSupplier(String key) { + return new ApiKeySupplier(STRONG(key)); + } + + public static Supplier strongSupplierHex(byte[] key) { + return new ApiKeySupplier(STRONG(key),true); + } + + public static Supplier strongSupplierHex(Key key) { + return new ApiKeySupplier(STRONG(key),true); + } + + public static Supplier strongSupplierHex(String key) { + return new ApiKeySupplier(STRONG(key),true); + } + + private static class ApiKeySupplier + implements Supplier { + 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 sig( + final PrivateKey key, final String alg) { + return new Function() { + public String apply(byte[] input) { + return sig(key,alg,input); + } + }; + } + + public static Function hmac( + final Key key, final String alg) { + return new Function() { + public String apply(byte[] input) { + return hmac(key,alg,input); + } + }; + } + + public static Predicate signatureValid( + final PublicKey key, + final String alg, + final byte[] source) { + return new Predicate() { + public boolean apply(byte[] mat) { + return sigval(key,alg,source,mat); + } + }; + } + + public static Predicate hmacValid( + final Key key, + final String alg, + final byte[] source) { + return new Predicate() { + 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 { + public T update(byte[] buf) { + update(buf,0,buf.length); + return (T)this; + } + public abstract 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 md5() { + return new Function() { + 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 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 update(byte[] buf, int s, int e) { mac.update(buf, s, e); + return (T)this; } public byte[] digest() { return mac.doFinal(); } } + + public static Function sha256(final Key key) { + return new Function() { + public String apply(byte[] input) { + return new SHA256(key).update(input).get(); + } + }; + } + + public static Function sha384(final Key key) { + return new Function() { + public String apply(byte[] input) { + return new SHA384(key).update(input).get(); + } + }; + } + + public static Function sha512(final Key key) { + return new Function() { + 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 { + 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 md5stream() { + return new IOHasherFunction() { + protected Hasher hasher() { + return new Md5(); + } + }; + } + + public static Function sha256stream(final Key key) { + return new IOHasherFunction() { + protected Hasher hasher() { + return new SHA256(key); + } + }; + } + + public static Function sha384stream(final Key key) { + return new IOHasherFunction() { + protected Hasher hasher() { + return new SHA384(key); + } + }; + } + + public static Function 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 { + private final Otp otp; + OtpSupplier(Otp otp) { + this.otp = otp; + } + public String get() { + return otp.generateNext(); + } + + } + + public static Supplier supplier(Otp otp) { + return new OtpSupplier(otp); + } + + public static Supplier totpSupplier(byte[] key, int step, int size) { + return new OtpSupplier(new Totp(step,key,size)); + } + + public static Supplier totpSupplier(byte[] key, int step) { + return new OtpSupplier(new Totp(step,key)); + } + + public static Supplier totpSupplier(byte[] key, int step, int size, String alg) { + return new OtpSupplier(new Totp(step,key,alg,size)); + } + + public static Supplier totpSupplier(Key key, int step, int size) { + return new OtpSupplier(new Totp(step,key,size)); + } + + public static Supplier totpSupplier(Key key, int step) { + return new OtpSupplier(new Totp(step,key)); + } + + public static Supplier totpSupplier(Key key, int step, int size, String alg) { + return new OtpSupplier(new Totp(step,key,alg,size)); + } + + public static Supplier totpSupplier(String key, int step, int size) { + return new OtpSupplier(new Totp(step,key,size)); + } + + public static Supplier totpSupplier(String key, int step) { + return new OtpSupplier(new Totp(step,key)); + } + + public static Supplier 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 asFunction() { + return new TFunction(this); + } + private static class TSupplier implements Supplier { 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 { + 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 decode() { + return new DecodeFunction(); + } + + public static Function encodeB(String charset) { + return new EncodeFunction(Codec.B,charset); + } + + public static Function encodeB() { + return encodeB("UTF-8"); + } + + public static Function encodeQ(String charset) { + return new EncodeFunction(Codec.Q,charset); + } + + public static Function encodeQ() { + return encodeQ("UTF-8"); + } + + public static Function encodeStar() { + return new EncodeFunction(Codec.STAR, "UTF-8"); + } + + public static Function encodeStar(String charset) { + return new EncodeFunction(Codec.STAR, charset); + } + + private static class EncodeFunction + implements Function { + 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 { + 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 equivalence() { return ne; } @@ -80,4 +81,31 @@ public enum NormalizationForm { } } + + private static class NormalFunction + implements Function { + private final NormalizationForm form; + NormalFunction(NormalizationForm form) { + this.form = form; + } + public CharSequence apply(CharSequence input) { + return form.normalize(input); + } + } + + public static Function D() { + return new NormalFunction(NormalizationForm.D); + } + + public static Function C() { + return new NormalFunction(NormalizationForm.C); + } + + public static Function KD() { + return new NormalFunction(NormalizationForm.KD); + } + + public static Function 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 createFunction() { + return new Function() { + public Slug apply(String input) { + return create(input); + } + }; + } + + public static Function createFunction( + final String filler) { + return new Function() { + public Slug apply(String input) { + return create(input, filler); + } + }; + } + + public static Function createFunction( + final String filler, + final boolean lower) { + return new Function() { + public Slug apply(String input) { + return create(input, filler, lower); + } + }; + } + + public static Function createFunction( + final String filler, + final boolean lower, + final NormalizationForm nf) { + return new Function() { + public Slug apply(String input) { + return create(input, filler, lower, nf); + } + }; + } + + public static Function createFunction( + final String filler, + final boolean lower, + final NormalizationForm nf, + final String pattern) { + return new Function() { + public Slug apply(String input) { + return create(input, filler, lower, nf, pattern); + } + }; + } + + public static Function createFunction( + final String filler, + final boolean lower, + final String pattern) { + return new Function() { + public Slug apply(String input) { + return create(input, filler, lower, pattern); + } + }; + } + + public static Function createFunction( + final String filler, + final String pattern) { + return new Function() { + 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 encoder( + final Profile... profiles) { + return encoder("UTF-8", profiles); + } + + public static Function encoder( + final String charset, + final Profile... profiles) { + return new Function() { + public String apply(CharSequence input) { + try { + return encode(input, charset, profiles); + } catch (Throwable t) { + throw ExceptionHelper.propogate(t); + } + } + }; + } + + public static Function decoder() { + return decoder("UTF-8"); + } + + public static Function decoder(final String charset) { + return new Function() { + public String apply(String input) { + try { + return decode(input,charset); + } catch (Throwable t) { + throw ExceptionHelper.propogate(t); + } + } + }; + } }