abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1225951 - in /abdera/abdera2: examples/src/main/java/org/apache/abdera2/examples/security/ security/src/main/java/org/apache/abdera2/security/ security/src/main/java/org/apache/abdera2/security/util/ security/src/main/java/org/apache/abder...
Date Fri, 30 Dec 2011 22:35:34 GMT
Author: jmsnell
Date: Fri Dec 30 22:35:32 2011
New Revision: 1225951

URL: http://svn.apache.org/viewvc?rev=1225951&view=rev
Log:
For consistency, use the same new immutable/threadsafe factory pattern for SignatureOptions
and EncryptionOptions that we use throughout Abdera2... 

Modified:
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/DSig.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/Enc.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Encryption.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/EncryptionOptions.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SecurityOptions.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Signature.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SignatureOptions.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/DHContext.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/AESEncryptedResponseFilter.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/SignedResponseFilter.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryption.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryptionOptions.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignature.java
    abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignatureOptions.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/DigitalSignatureTest.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/EncryptionTest.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/filter/SecurityFilterTest.java

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/DSig.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/DSig.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/DSig.java (original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/DSig.java Fri
Dec 30 22:35:32 2011
@@ -66,9 +66,11 @@ public class DSig {
 
         // Prepare the digital signature options
         Signature sig = absec.getSignature();
-        SignatureOptions options = sig.getDefaultSignatureOptions();
-        options.setCertificate(cert);
-        options.setSigningKey(signingKey);
+        SignatureOptions options = 
+          sig.getDefaultSignatureOptions()
+            .certificate(cert)
+            .signingKey(signingKey)
+            .get();
 
         // Sign the entry
         entry = sig.sign(entry, options);

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/Enc.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/Enc.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/Enc.java (original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/security/Enc.java Fri
Dec 30 22:35:32 2011
@@ -58,8 +58,9 @@ public class Enc {
 
         // Prepare the encryption options
         Encryption enc = absec.getEncryption();
-        EncryptionOptions options = enc.getDefaultEncryptionOptions();
-        options.setDataEncryptionKey(key);
+        EncryptionOptions options = 
+          enc.getDefaultEncryptionOptions()
+            .dataEncryptionKey(key).get();
 
         // Encrypt the document using the generated key
         Document<?> enc_doc = enc.encrypt(entry.getDocument(), options);

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Encryption.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Encryption.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Encryption.java (original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Encryption.java Fri
Dec 30 22:35:32 2011
@@ -19,6 +19,7 @@ package org.apache.abdera2.security;
 
 import org.apache.abdera2.model.Document;
 import org.apache.abdera2.model.Element;
+import org.apache.abdera2.security.EncryptionOptions.EncryptionOptionsBuilder;
 
 /**
  * Interface used for encrypting/decrypting Abdera documents.
@@ -55,6 +56,6 @@ public interface Encryption {
      * 
      * @see org.apache.abdera.security.EncryptionOptions
      */
-    EncryptionOptions getDefaultEncryptionOptions();
+    EncryptionOptionsBuilder getDefaultEncryptionOptions();
 
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/EncryptionOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/EncryptionOptions.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/EncryptionOptions.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/EncryptionOptions.java
Fri Dec 30 22:35:32 2011
@@ -19,63 +19,91 @@ package org.apache.abdera2.security;
 
 import java.security.Key;
 
+import org.apache.abdera2.Abdera;
+
 /**
  * Provides access to the information necessary to encrypt or decrypt a document
  */
 public interface EncryptionOptions extends SecurityOptions {
 
+  public static abstract class EncryptionOptionsBuilder
+    extends SecurityOptions.Builder<EncryptionOptions,EncryptionOptionsBuilder> {
+    
+    protected Abdera abdera;
+    protected boolean includeKeyInfo;
+    protected Key dek;
+    protected Key kek;
+    protected String kca;
+    protected String dca;
+    
+    protected EncryptionOptionsBuilder() {
+      keyCipherAlgorithm("http://www.w3.org/2001/04/xmlenc#kw-aes128");
+      dataCipherAlgorithm("http://www.w3.org/2001/04/xmlenc#aes128-cbc");
+      doNotIncludeKeyInfo();
+    }
+    
+    public EncryptionOptionsBuilder abdera(Abdera abdera) {
+      this.abdera = abdera;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder includeKeyInfo() {
+      this.includeKeyInfo = true;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder doNotIncludeKeyInfo() {
+      this.includeKeyInfo = false;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder dataEncryptionKey(Key key) {
+      this.dek = key;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder keyEncryptionKey(Key key) {
+      this.kek = key;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder keyCipherAlgorithm(String alg) {
+      this.kca = alg;
+      return this;
+    }
+    
+    public EncryptionOptionsBuilder dataCipherAlgorithm(String alg) {
+      this.dca = alg;
+      return this;
+    }
+    
+  }
+  
     /**
      * Return the secret key used to encrypt/decrypt the document content
      */
     Key getDataEncryptionKey();
 
     /**
-     * Set the secret key used to encrypt/decrypt the document content
-     */
-    EncryptionOptions setDataEncryptionKey(Key key);
-
-    /**
      * Return the secret key used to encrypt/decrypt the data encryption key
      */
     Key getKeyEncryptionKey();
 
     /**
-     * Set the secret key used to encrypt/decrypt the data encryption key
-     */
-    EncryptionOptions setKeyEncryptionKey(Key key);
-
-    /**
      * Return the cipher algorithm used to decrypt/encrypt the data encryption key The default
is
      * "http://www.w3.org/2001/04/xmlenc#kw-aes128"
      */
     String getKeyCipherAlgorithm();
 
     /**
-     * Set the cipher algorithm used to decrypt/encrypt the data encryption key The default
is
-     * "http://www.w3.org/2001/04/xmlenc#kw-aes128"
-     */
-    EncryptionOptions setKeyCipherAlgorithm(String alg);
-
-    /**
      * Return the cipher algorithm used to decrypt/encrypt the document content The default
is
      * "http://www.w3.org/2001/04/xmlenc#aes128-cbc"
      */
     String getDataCipherAlgorithm();
 
     /**
-     * Set the cipher algorithm used to decyrpt/encrypt the document content The default
is
-     * "http://www.w3.org/2001/04/xmlenc#aes128-cbc"
-     */
-    EncryptionOptions setDataCipherAlgorithm(String alg);
-
-    /**
      * Return true if the encryption should include information about the key The default
is false
      */
     boolean includeKeyInfo();
 
-    /**
-     * Set whether the encryption should include information about the key The default is
false
-     */
-    EncryptionOptions setIncludeKeyInfo(boolean includeKeyInfo);
-
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SecurityOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SecurityOptions.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SecurityOptions.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SecurityOptions.java
Fri Dec 30 22:35:32 2011
@@ -19,13 +19,27 @@ package org.apache.abdera2.security;
 
 import org.apache.abdera2.parser.Parser;
 
+import com.google.common.base.Supplier;
+
 /**
  * Base interface for EncryptionOptions and SignatureOptions
  */
 public interface SecurityOptions {
-
+  
     Parser getParser();
 
     <T extends SecurityOptions> T setParser(Parser parser);
 
+  public static abstract class Builder<X extends SecurityOptions, M extends Builder<X,M>>

+    implements Supplier<X> {
+    
+    protected Parser parser;
+    
+    @SuppressWarnings("unchecked")
+    public M parser(Parser parser) {
+      this.parser = parser;
+      return (M)this;
+    }
+    
+  }
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Signature.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Signature.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Signature.java (original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/Signature.java Fri Dec
30 22:35:32 2011
@@ -20,6 +20,7 @@ package org.apache.abdera2.security;
 import java.security.cert.X509Certificate;
 
 import org.apache.abdera2.model.Element;
+import org.apache.abdera2.security.SignatureOptions.SignatureOptionsBuilder;
 import org.apache.xml.security.keys.KeyInfo;
 
 /**
@@ -55,7 +56,7 @@ public interface Signature {
      * 
      * @see org.apache.abdera.security.SignatureOptions
      */
-    SignatureOptions getDefaultSignatureOptions() throws SecurityException;
+    SignatureOptionsBuilder getDefaultSignatureOptions() throws SecurityException;
 
     <T extends Element> T removeInvalidSignatures(T element, SignatureOptions options)
throws SecurityException;
 

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SignatureOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SignatureOptions.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SignatureOptions.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/SignatureOptions.java
Fri Dec 30 22:35:32 2011
@@ -21,53 +21,98 @@ import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.cert.X509Certificate;
 
+import org.apache.abdera2.Abdera;
+
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Provides access to the information necessary to signed an Abdera element
  */
 public interface SignatureOptions extends SecurityOptions {
 
+  public static abstract class SignatureOptionsBuilder
+    extends SecurityOptions.Builder<SignatureOptions,SignatureOptionsBuilder> {
+    
+    protected Abdera abdera;
+    protected String salg;
+    protected PrivateKey skey;   
+    protected X509Certificate cert;
+    protected PublicKey pkey;
+    protected ImmutableSet.Builder<String> refs = 
+      ImmutableSet.builder();
+    protected boolean signlinks;
+    protected ImmutableSet.Builder<String> linkRels = 
+      ImmutableSet.builder();
+    
+    public SignatureOptionsBuilder() {
+      signingAlgorithm("http://www.w3.org/2000/09/xmldsig#dsa-sha1");
+    }
+    
+    public SignatureOptionsBuilder abdera(Abdera abdera) {
+      this.abdera = abdera;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder signingAlgorithm(String alg) {
+      this.salg = alg;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder signingKey(PrivateKey key) {
+      this.skey = key;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder certificate(X509Certificate cert) {
+      this.cert = cert;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder publicKey(PublicKey key) {
+      this.pkey = key;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder ref(String ref) {
+      refs.add(ref);
+      return this;
+    }
+    
+    public SignatureOptionsBuilder signLinks() {
+      this.signlinks = true;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder doNotSignLinks() {
+      this.signlinks = false;
+      return this;
+    }
+    
+    public SignatureOptionsBuilder signLinkRel(String rel) {
+      this.linkRels.add(rel);
+      return this;
+    }
+    
+  }
+  
     String getSigningAlgorithm();
 
-    SignatureOptions setSigningAlgorithm(String algorithm);
-
     /**
      * Return the private key with which to sign the element
      */
     PrivateKey getSigningKey();
 
     /**
-     * Set the private key with which to sign the element
-     */
-    SignatureOptions setSigningKey(PrivateKey privateKey);
-
-    /**
      * Return the X.509 cert to associated with the signature
      */
     X509Certificate getCertificate();
 
     /**
-     * Set the X.509 cert to associate with the signature
-     */
-    SignatureOptions setCertificate(X509Certificate cert);
-
-    /**
      * Get the public key associated with the signature
      */
     PublicKey getPublicKey();
 
-    /**
-     * Set the public key to associate with the signature
-     */
-    SignatureOptions setPublicKey(PublicKey publickey);
-
-    SignatureOptions addReference(String href);
-
-    String[] getReferences();
-
-    /**
-     * True if atom:link/@href and atom:content/@src targets should be included in the signature
-     */
-    SignatureOptions setSignLinks(boolean signlinks);
+    Iterable<String> getReferences();
 
     /**
      * True if atom:link/@href and atom:content/@src targets should be included in the signature
@@ -75,12 +120,7 @@ public interface SignatureOptions extend
     boolean isSignLinks();
 
     /**
-     * Only sign links whose link rels match those provided in the list
-     */
-    SignatureOptions setSignedLinkRels(String... rel);
-
-    /**
      * Get the list of link relations to sign
      */
-    String[] getSignLinkRels();
+    Iterable<String> getSignLinkRels();
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/DHContext.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/DHContext.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/DHContext.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/DHContext.java
Fri Dec 30 22:35:32 2011
@@ -49,10 +49,10 @@ public class DHContext extends DHBase {
     
     public EncryptionOptions getEncryptionOptions(Encryption enc, String alg) 
       throws InvalidKeyException, NoSuchAlgorithmException {
-        EncryptionOptions options = enc.getDefaultEncryptionOptions();
-        options.setDataEncryptionKey(generateSecret());
-        options.setDataCipherAlgorithm(alg);
-        return options;
+        return enc.getDefaultEncryptionOptions()
+          .dataEncryptionKey(generateSecret())
+          .dataCipherAlgorithm(alg)
+          .get();
   }
 
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/AESEncryptedResponseFilter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/AESEncryptedResponseFilter.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/AESEncryptedResponseFilter.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/AESEncryptedResponseFilter.java
Fri Dec 30 22:35:32 2011
@@ -75,12 +75,12 @@ public class AESEncryptedResponseFilter 
                                                       Encryption enc,
                                                       Object arg) {
         try {
-            EncryptionOptions options = enc.getDefaultEncryptionOptions();
-            options.setDataEncryptionKey(KeyHelper.generateKey("AES"));
-            options.setKeyEncryptionKey((PublicKey)arg);
-            options.setKeyCipherAlgorithm(XMLCipher.RSA_v1dot5);
-            options.setIncludeKeyInfo(true);
-            return options;
+          return enc.getDefaultEncryptionOptions()
+            .dataEncryptionKey(KeyHelper.generateKey("AES"))
+            .keyEncryptionKey((PublicKey)arg)
+            .keyCipherAlgorithm(XMLCipher.RSA_v1dot5)
+            .includeKeyInfo()
+            .get();
         } catch (Exception e) {
             return null;
         }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/SignedResponseFilter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/SignedResponseFilter.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/SignedResponseFilter.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/util/filters/SignedResponseFilter.java
Fri Dec 30 22:35:32 2011
@@ -39,7 +39,7 @@ import org.apache.abdera2.protocol.serve
 import org.apache.abdera2.security.Security;
 import org.apache.abdera2.security.SecurityException;
 import org.apache.abdera2.security.Signature;
-import org.apache.abdera2.security.SignatureOptions;
+import org.apache.abdera2.security.SignatureOptions.SignatureOptionsBuilder;
 import org.apache.abdera2.writer.Writer;
 
 /**
@@ -127,13 +127,14 @@ public class SignedResponseFilter implem
         if (signingKey == null || cert == null)
             return doc; // pass through
         Signature sig = security.getSignature();
-        SignatureOptions options = sig.getDefaultSignatureOptions();
-        options.setCertificate(cert);
-        options.setSigningKey(signingKey);
+        SignatureOptionsBuilder options = 
+          sig.getDefaultSignatureOptions()
+            .certificate(cert)
+            .signingKey(signingKey);
         if (algorithm != null)
-            options.setSigningAlgorithm(algorithm);
+            options.signingAlgorithm(algorithm);
         Element element = doc.getRoot();
-        element = sig.sign(element, options);
+        element = sig.sign(element, options.get());
         return element.getDocument();
     }
 

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryption.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryption.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryption.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryption.java
Fri Dec 30 22:35:32 2011
@@ -23,6 +23,7 @@ import org.apache.abdera2.Abdera;
 import org.apache.abdera2.model.Document;
 import org.apache.abdera2.model.Element;
 import org.apache.abdera2.security.EncryptionOptions;
+import org.apache.abdera2.security.EncryptionOptions.EncryptionOptionsBuilder;
 import org.apache.abdera2.security.SecurityException;
 import org.apache.abdera2.security.util.EncryptionBase;
 import org.apache.xml.security.encryption.EncryptedData;
@@ -92,8 +93,8 @@ public class XmlEncryption extends Encry
         }
     }
 
-    public EncryptionOptions getDefaultEncryptionOptions() {
-        return new XmlEncryptionOptions(getAbdera());
+    public EncryptionOptionsBuilder getDefaultEncryptionOptions() {
+        return XmlEncryptionOptions.make().abdera(getAbdera());
     }
 
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryptionOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryptionOptions.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryptionOptions.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlEncryptionOptions.java
Fri Dec 30 22:35:32 2011
@@ -22,60 +22,59 @@ import java.security.Key;
 import org.apache.abdera2.Abdera;
 import org.apache.abdera2.security.EncryptionOptions;
 
-public class XmlEncryptionOptions extends XmlSecurityOptions implements EncryptionOptions
{
-
-    private Key dek = null;
-    private Key kek = null;
-    private String kca = "http://www.w3.org/2001/04/xmlenc#kw-aes128";
-    private String dca = "http://www.w3.org/2001/04/xmlenc#aes128-cbc";
-    private boolean setki = false;
-
-    protected XmlEncryptionOptions(Abdera abdera) {
+public final class XmlEncryptionOptions extends XmlSecurityOptions implements EncryptionOptions
{
+  
+  public static EncryptionOptionsBuilder make() {
+    return new XmlEncryptionOptionsBuilder();
+  }
+  
+  protected static final class XmlEncryptionOptionsBuilder 
+    extends EncryptionOptionsBuilder {
+
+    public EncryptionOptions get() {
+      return new XmlEncryptionOptions(abdera,dek,kek,kca,dca,includeKeyInfo);
+    }
+    
+  }
+  
+    private final Key dek;
+    private final Key kek;
+    private final String kca;
+    private final String dca;
+    private final boolean setki;
+
+    protected XmlEncryptionOptions(
+      Abdera abdera,
+      Key dek, 
+      Key kek, 
+      String kca, 
+      String dca, 
+      boolean setki) {
         super(abdera);
+        this.dek = dek;
+        this.kek = kek;
+        this.kca = kca;
+        this.dca = dca;
+        this.setki = setki;
     }
 
     public Key getDataEncryptionKey() {
         return dek;
     }
 
-    public EncryptionOptions setDataEncryptionKey(Key key) {
-        this.dek = key;
-        return this;
-    }
-
     public Key getKeyEncryptionKey() {
         return kek;
     }
 
-    public EncryptionOptions setKeyEncryptionKey(Key key) {
-        this.kek = key;
-        return this;
-    }
-
     public String getKeyCipherAlgorithm() {
         return kca;
     }
 
-    public EncryptionOptions setKeyCipherAlgorithm(String alg) {
-        this.kca = alg;
-        return this;
-    }
-
     public String getDataCipherAlgorithm() {
         return dca;
     }
 
-    public EncryptionOptions setDataCipherAlgorithm(String alg) {
-        this.dca = alg;
-        return this;
-    }
-
     public boolean includeKeyInfo() {
         return setki;
     }
-
-    public EncryptionOptions setIncludeKeyInfo(boolean includeKeyInfo) {
-        this.setki = includeKeyInfo;
-        return this;
-    }
 }

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignature.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignature.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignature.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignature.java
Fri Dec 30 22:35:32 2011
@@ -31,6 +31,7 @@ import org.apache.abdera2.model.Link;
 import org.apache.abdera2.model.Source;
 import org.apache.abdera2.security.SecurityException;
 import org.apache.abdera2.security.SignatureOptions;
+import org.apache.abdera2.security.SignatureOptions.SignatureOptionsBuilder;
 import org.apache.abdera2.security.util.Constants;
 import org.apache.abdera2.security.util.SignatureBase;
 import org.apache.abdera2.common.iri.IRI;
@@ -42,6 +43,8 @@ import org.apache.xml.security.transform
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import com.google.common.collect.Iterables;
+
 public class XmlSignature extends SignatureBase {
 
     static {
@@ -73,12 +76,11 @@ public class XmlSignature extends Signat
         transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
         transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
         sig.addDocument("", transforms, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
-        String[] refs = options.getReferences();
-        for (String ref : refs)
+        for (String ref : options.getReferences())
             sig.addDocument(ref);
 
         if (options.isSignLinks()) {
-            String[] rels = options.getSignLinkRels();
+            String[] rels = Iterables.toArray(options.getSignLinkRels(),String.class);
             List<Link> links = null;
             Content content = null;
             if (element instanceof Source) {
@@ -227,8 +229,8 @@ public class XmlSignature extends Signat
         }
     }
 
-    public SignatureOptions getDefaultSignatureOptions() throws SecurityException {
-        return new XmlSignatureOptions(getAbdera());
+    public SignatureOptionsBuilder getDefaultSignatureOptions() throws SecurityException
{
+        return XmlSignatureOptions.make().abdera(getAbdera());
     }
 
     @SuppressWarnings("unchecked")

Modified: abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignatureOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignatureOptions.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignatureOptions.java
(original)
+++ abdera/abdera2/security/src/main/java/org/apache/abdera2/security/xmlsec/XmlSignatureOptions.java
Fri Dec 30 22:35:32 2011
@@ -20,89 +20,78 @@ package org.apache.abdera2.security.xmls
 import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.abdera2.Abdera;
 import org.apache.abdera2.security.SignatureOptions;
+public final class XmlSignatureOptions extends XmlSecurityOptions implements SignatureOptions
{
 
-public class XmlSignatureOptions extends XmlSecurityOptions implements SignatureOptions {
-
-    private PrivateKey signingKey = null;
-    private PublicKey publickey = null;
-    private X509Certificate cert = null;
-    private String[] linkrels = null;
-    private boolean signlinks = false;
-    private List<String> references = null;
-    private String algo = "http://www.w3.org/2000/09/xmldsig#dsa-sha1";
-
+  public static SignatureOptionsBuilder make() {
+    return new XmlSignatureOptionsBuilder();
+  }
+  
+  protected static final class XmlSignatureOptionsBuilder 
+    extends SignatureOptionsBuilder {
+  
+    public SignatureOptions get() {
+      return new XmlSignatureOptions(
+        abdera,salg,skey,pkey,cert,refs.build(),signlinks,linkRels.build());
+    }
+    
+  }
+  
+    private final PrivateKey signingKey;
+    private final PublicKey publickey;
+    private final X509Certificate cert;
+    private final Iterable<String> linkrels;
+    private final boolean signlinks;
+    private final Iterable<String> references;
+    private final String algo;
+
+    protected XmlSignatureOptions(
+      Abdera abdera, 
+      String salg, 
+      PrivateKey skey, 
+      PublicKey pkey, 
+      X509Certificate cert, 
+      Iterable<String> refs, 
+      boolean signlinks, 
+      Iterable<String> rels) {
+      super(abdera);
+      this.signingKey = skey;
+      this.publickey = pkey;
+      this.cert = cert;
+      this.linkrels = rels;
+      this.signlinks = signlinks;
+      this.references = refs;
+      this.algo = salg;
+    }
+    
     public String getSigningAlgorithm() {
         return algo;
     }
 
-    public SignatureOptions setSigningAlgorithm(String algorithm) {
-        this.algo = algorithm;
-        return this;
-    }
-
-    protected XmlSignatureOptions(Abdera abdera) {
-        super(abdera);
-        references = new ArrayList<String>();
-    }
-
     public PrivateKey getSigningKey() {
         return signingKey;
     }
 
-    public SignatureOptions setSigningKey(PrivateKey privateKey) {
-        this.signingKey = privateKey;
-        return this;
-    }
-
     public X509Certificate getCertificate() {
         return cert;
     }
 
-    public SignatureOptions setCertificate(X509Certificate cert) {
-        this.cert = cert;
-        return this;
-    }
-
-    public SignatureOptions addReference(String href) {
-        if (!references.contains(href))
-            references.add(href);
-        return this;
-    }
-
-    public String[] getReferences() {
-        return references.toArray(new String[references.size()]);
+    public Iterable<String> getReferences() {
+        return references;
     }
 
     public PublicKey getPublicKey() {
         return publickey;
     }
 
-    public SignatureOptions setPublicKey(PublicKey publickey) {
-        this.publickey = publickey;
-        return this;
-    }
-
     public boolean isSignLinks() {
         return signlinks;
     }
 
-    public SignatureOptions setSignLinks(boolean signlinks) {
-        this.signlinks = signlinks;
-        return this;
-    }
-
-    public String[] getSignLinkRels() {
+    public Iterable<String> getSignLinkRels() {
         return this.linkrels;
     }
 
-    public SignatureOptions setSignedLinkRels(String... rel) {
-        this.linkrels = rel;
-        return this;
-    }
-
 }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/DigitalSignatureTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/DigitalSignatureTest.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/DigitalSignatureTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/DigitalSignatureTest.java
Fri Dec 30 22:35:32 2011
@@ -81,9 +81,11 @@ public class DigitalSignatureTest {
 
         // Prepare the digital signature options
         Signature sig = absec.getSignature();
-        SignatureOptions options = sig.getDefaultSignatureOptions();
-        options.setCertificate(cert);
-        options.setSigningKey(signingKey);
+        SignatureOptions options = 
+          sig.getDefaultSignatureOptions()
+            .certificate(cert)
+            .signingKey(signingKey)
+            .get();
 
         // Sign the entry
         entry = sig.sign(entry, options);

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/EncryptionTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/EncryptionTest.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/EncryptionTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/EncryptionTest.java
Fri Dec 30 22:35:32 2011
@@ -77,8 +77,9 @@ public class EncryptionTest {
 
         // Prepare the encryption options
         Encryption enc = absec.getEncryption();
-        EncryptionOptions options = enc.getDefaultEncryptionOptions();
-        options.setDataEncryptionKey(key);
+        EncryptionOptions options = 
+          enc.getDefaultEncryptionOptions()
+            .dataEncryptionKey(key).get();
 
         // Encrypt the document using the generated key
         Document<?> enc_doc = enc.encrypt(entry.getDocument(), options);

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/filter/SecurityFilterTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/filter/SecurityFilterTest.java?rev=1225951&r1=1225950&r2=1225951&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/filter/SecurityFilterTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/security/filter/SecurityFilterTest.java
Fri Dec 30 22:35:32 2011
@@ -72,7 +72,7 @@ public class SecurityFilterTest {
         Security security = new Security(abdera);
         Signature sig = security.getSignature();
         assertTrue(sig.isSigned(root));
-        assertTrue(sig.verify(root, sig.getDefaultSignatureOptions()));
+        assertTrue(sig.verify(root, sig.getDefaultSignatureOptions().get()));
     }
 
     private static final String keystoreFile = "/key.jks";
@@ -111,9 +111,11 @@ public class SecurityFilterTest {
         assertNotNull(cert);
 
         Signature sig = security.getSignature();
-        SignatureOptions options = sig.getDefaultSignatureOptions();
-        options.setCertificate(cert);
-        options.setSigningKey(signingKey);
+        SignatureOptions options = 
+          sig.getDefaultSignatureOptions()
+            .certificate(cert)
+            .signingKey(signingKey)
+            .get();
 
         // Sign the entry
         entry = sig.sign(entry, options);



Mime
View raw message