harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r567530 - in /harmony/enhanced/classlib/branches/java6/modules/auth/src: main/java/common/javax/security/auth/x500/X500Principal.java test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java
Date Mon, 20 Aug 2007 03:02:06 GMT
Author: leoli
Date: Sun Aug 19 20:02:05 2007
New Revision: 567530

URL: http://svn.apache.org/viewvc?rev=567530&view=rev
Log:
Add Java 6 new feature in auth module (javax.security.auth.x500.X500Principal(String, Map<String,
String )and javax.security.auth.x500.X500Principal.getName(String,Map<String,String>)).

Modified:
    harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/x500/X500Principal.java
    harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java

Modified: harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/x500/X500Principal.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/x500/X500Principal.java?rev=567530&r1=567529&r2=567530&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/x500/X500Principal.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/x500/X500Principal.java
Sun Aug 19 20:02:05 2007
@@ -23,6 +23,7 @@
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.security.Principal;
+import java.util.Map;
 
 import org.apache.harmony.auth.internal.nls.Messages;
 import org.apache.harmony.security.x501.Name;
@@ -86,6 +87,22 @@
             throw iae;
         }
     }
+    
+    public X500Principal(String name, Map<String,String> keywordMap){
+        super();
+        if (name == null) {
+            throw new NullPointerException(Messages.getString("auth.00")); //$NON-NLS-1$
+        }
+        
+        try {
+            dn = new Name(substituteNameFromMap(name,keywordMap));
+        } catch (IOException e) {
+            IllegalArgumentException iae = new IllegalArgumentException(Messages
+                    .getString("auth.2D")); //$NON-NLS-1$
+            iae.initCause(e);
+            throw iae;
+        }
+    }
 
     @Override
     public boolean equals(Object o) {
@@ -113,6 +130,68 @@
     public String getName(String format) {
         return dn.getName(format);
     }
+    
+    public String getName(String format, Map<String, String> oidMap) {
+        String rfc1779Name = dn.getName(RFC1779);
+        String rfc2253Name = dn.getName(RFC2253);
+
+        if (format.toUpperCase().equals("RFC1779")) {
+            StringBuilder resultName = new StringBuilder(rfc1779Name);
+            int fromIndex = resultName.length();
+            int equalIndex = -1;
+            while (-1 != (equalIndex = resultName.lastIndexOf("=", fromIndex))) {
+                int commaIndex = resultName.lastIndexOf(",", equalIndex);
+                String subName = resultName.substring(commaIndex + 1,
+                        equalIndex).trim();
+                if (subName.length() > 4
+                        && subName.substring(0, 4).equals("OID.")) {
+                    String subSubName = subName.substring(4);
+                    if (oidMap.containsKey(subSubName)) {
+                        String replaceName = oidMap.get(subSubName);
+                        if(commaIndex > 0) replaceName = " " + replaceName;
+                        resultName.replace(commaIndex + 1, equalIndex, replaceName);
+                    }
+                }
+                fromIndex = commaIndex;
+            }
+            return resultName.toString();
+        } else if (format.toUpperCase().equals("RFC2253")) {
+            StringBuilder resultName = new StringBuilder(rfc2253Name);
+            StringBuilder subsidyName = new StringBuilder(rfc1779Name);
+
+            int fromIndex = resultName.length();
+            int subsidyFromIndex = subsidyName.length();
+            int equalIndex = -1;
+            int subsidyEqualIndex = -1;
+            while (-1 != (equalIndex = resultName.lastIndexOf("=", fromIndex))) {
+                subsidyEqualIndex = subsidyName.lastIndexOf("=",
+                        subsidyFromIndex);
+                int commaIndex = resultName.lastIndexOf(",", equalIndex);
+                String subName = resultName.substring(commaIndex + 1,
+                        equalIndex).trim();
+                if (oidMap.containsKey(subName)) {
+                    int subOrignalEndIndex = resultName
+                            .indexOf(",", equalIndex);
+                    if (subOrignalEndIndex == -1)
+                        subOrignalEndIndex = resultName.length();
+                    int subGoalEndIndex = subsidyName.indexOf(",",
+                            subsidyEqualIndex);
+                    if (subGoalEndIndex == -1)
+                        subGoalEndIndex = subsidyName.length();
+                    resultName.replace(equalIndex + 1, subOrignalEndIndex,
+                            subsidyName.substring(subsidyEqualIndex + 1,
+                                    subGoalEndIndex));
+                    resultName.replace(commaIndex + 1, equalIndex, oidMap
+                            .get(subName));
+                }
+                fromIndex = commaIndex;
+                subsidyFromIndex = subsidyEqualIndex - 1;
+            }
+            return resultName.toString();
+        } else {
+            throw new IllegalArgumentException("invalid format specified");
+        }
+    }
 
     @Override
     public int hashCode() {
@@ -131,5 +210,20 @@
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
 
         dn = (Name) Name.ASN1.decode((byte[]) in.readObject());
+    }
+    
+    private String substituteNameFromMap(String name, Map<String,String> keywordMap){
+        StringBuilder sbName = new StringBuilder(name);
+        int fromIndex = sbName.length();
+        int equalIndex = -1;
+        while(-1 != (equalIndex = sbName.lastIndexOf("=",fromIndex))){
+            int commaIndex = sbName.lastIndexOf(",",equalIndex);
+            String subName = sbName.substring(commaIndex+1, equalIndex).trim();
+            if(keywordMap.containsKey(subName)){
+                sbName.replace(commaIndex+1, equalIndex, keywordMap.get(subName));
+            }
+            fromIndex = commaIndex;
+        }
+        return sbName.toString();
     }
 }

Modified: harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java?rev=567530&r1=567529&r2=567530&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/tests/api/javax/security/auth/x500/X500PrincipalTest.java
Sun Aug 19 20:02:05 2007
@@ -17,6 +17,9 @@
 
 package tests.api.javax.security.auth.x500;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.security.auth.x500.X500Principal;
 
 public class X500PrincipalTest extends junit.framework.TestCase {
@@ -31,6 +34,20 @@
 		String expectedOuput = "CN=Hermione Granger,O=Apache Software Foundation,OU=Harmony,L=Hogwarts,ST=Hants,C=GB";
 		assertEquals("Output order precedence problem", expectedOuput, name);
 	}
+    
+    /**
+     * @tests javax.security.auth.x500.X500Principal#X500Principal(java.lang.String, java.util.Map)
+     */
+    public void test_ConstructorLjava_lang_String_java_util_Map(){
+        Map<String, String> keyword = new HashMap<String, String>();
+        keyword.put("CN", "2.19");
+        keyword.put("OU", "1.2.5.19");
+        keyword.put("O", "1.2.5");
+        X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems,
C=US ,CN=DD",keyword);
+        String name = X500p.getName();
+        String expectedOut = "2.19=#130444756b65,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,2.19=#13024444";
+        assertEquals("Output order precedence problem", expectedOut, name);
+    }
 
 	/**
 	 * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String)
@@ -43,4 +60,30 @@
 		assertEquals("CANONICAL output differs from expected result", expected,
 				canonical);
 	}
+    
+    /**
+     * @tests javax.security.auth.x500.X500Principal#getName(java.lang.String, java.util.Map)
+     */
+    public void test_getNameLjava_lang_String_java_util_Map() {
+        Map<String, String> keyword = new HashMap<String, String>();
+        keyword.put("CN", "2.19");
+        keyword.put("OU", "1.2.5.19");
+        keyword.put("O", "1.2.5");
+        X500Principal X500p = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems,
C=US ,CN=DD",keyword);
+        keyword = new HashMap<String, String>();
+        keyword.put("2.19", "mystring");
+        String rfc1779Name = X500p.getName("RFC1779",keyword);
+        String rfc2253Name = X500p.getName("RFC2253",keyword);
+        String expected1779Out = "mystring=Duke, OID.1.2.5.19=JavaSoft, OID.1.2.5=Sun Microsystems,
C=US, mystring=DD";
+        String expected2253Out = "mystring=Duke,1.2.5.19=#13084a617661536f6674,1.2.5=#131053756e204d6963726f73797374656d73,C=US,mystring=DD";
+        assertEquals("Output order precedence problem", expected1779Out, rfc1779Name);
+        assertEquals("Output order precedence problem", expected2253Out, rfc2253Name);
+        try{
+            X500p.getName("CANONICAL",keyword);
+            fail("Should throw IllegalArgumentException exception here");
+        }
+        catch(IllegalArgumentException e){
+            //expected IllegalArgumentException here
+        }
+    }
 }



Mime
View raw message