You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2007/10/22 18:28:39 UTC

svn commit: r587146 - in /directory/apacheds/branches/bigbang: kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/ kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/ kerberos-shared/src/main/jav...

Author: elecharny
Date: Mon Oct 22 09:28:37 2007
New Revision: 587146

URL: http://svn.apache.org/viewvc?rev=587146&view=rev
Log:
Modified the PrincipalName to stick to the RFC's names. Added some encoding methods using what we have done in shared-asn.1. Created a PrincipalNameType Enum to replace an existing class. Added some encoding tests

Added:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/KerberosUtils.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/PrincipalNameType.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/KerberosUtilsTest.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/
    directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameTest.java
Removed:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameModifier.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameType.java
Modified:
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfo2Decoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfoDecoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/PrincipalNameDecoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/PrincipalNameEncoder.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java
    directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalName.java
    directory/apacheds/branches/bigbang/protocol-changepw/src/test/java/org/apache/directory/server/changepw/protocol/ChangepwProtocolHandlerTest.java
    directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractAuthenticationServiceTest.java
    directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractTicketGrantingServiceTest.java

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/KerberosUtils.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/KerberosUtils.java?rev=587146&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/KerberosUtils.java (added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/KerberosUtils.java Mon Oct 22 09:28:37 2007
@@ -0,0 +1,199 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.directory.shared.ldap.util.StringTools;
+
+/**
+ * An utility class for Kerberos.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class KerberosUtils
+{
+    /** A constant for integer optional values */
+    public static final int NULL = -1;
+
+    private static final List<String> EMPTY_PRINCIPAL_NAME = new ArrayList<String>();
+    
+    /**
+     * Parse a KerberosPrincipal instance and return the names. The Principal name
+     * is described in RFC 1964 : <br/>
+     * <br/>
+     * This name type corresponds to the single-string representation of a<br/>
+     * Kerberos name.  (Within the MIT Kerberos V5 implementation, such<br/>
+     * names are parseable with the krb5_parse_name() function.)  The<br/>
+     * elements included within this name representation are as follows,<br/>
+     * proceeding from the beginning of the string:<br/>
+     * <br/>
+     *  (1) One or more principal name components; if more than one<br/>
+     *  principal name component is included, the components are<br/>
+     *  separated by `/`.  Arbitrary octets may be included within<br/>
+     *  principal name components, with the following constraints and<br/>
+     *  special considerations:<br/>
+     * <br/>
+     *     (1a) Any occurrence of the characters `@` or `/` within a<br/>
+     *     name component must be immediately preceded by the `\`<br/>
+     *     quoting character, to prevent interpretation as a component<br/>
+     *     or realm separator.<br/>
+     * <br/>
+     *     (1b) The ASCII newline, tab, backspace, and null characters<br/>
+     *     may occur directly within the component or may be<br/>
+     *     represented, respectively, by `\n`, `\t`, `\b`, or `\0`.<br/>
+     * <br/>
+     *     (1c) If the `\` quoting character occurs outside the contexts<br/>
+     *     described in (1a) and (1b) above, the following character is<br/>
+     *     interpreted literally.  As a special case, this allows the<br/>
+     *     doubled representation `\\` to represent a single occurrence<br/>
+     *     of the quoting character.<br/>
+     * <br/>
+     *     (1d) An occurrence of the `\` quoting character as the last<br/>
+     *     character of a component is illegal.<br/>
+     * <br/>
+     *  (2) Optionally, a `@` character, signifying that a realm name<br/>
+     *  immediately follows. If no realm name element is included, the<br/>
+     *  local realm name is assumed.  The `/` , `:`, and null characters<br/>
+     *  may not occur within a realm name; the `@`, newline, tab, and<br/>
+     *  backspace characters may be included using the quoting<br/>
+     *  conventions described in (1a), (1b), and (1c) above.<br/>
+     * 
+     * @param principal The principal to be parsed
+     * @return The names as a List of nameComponent
+     * 
+     * @throws ParseException if the name is not valid
+     */
+    public static List<String> getNames( KerberosPrincipal principal ) throws ParseException
+    {
+        if ( principal == null )
+        {
+            return EMPTY_PRINCIPAL_NAME;
+        }
+        
+        String names = principal.getName();
+        
+        if ( StringTools.isEmpty( names ) )
+        {
+            // Empty name...
+            return EMPTY_PRINCIPAL_NAME;
+        }
+        
+        return getNames( names );
+    }
+
+    /**
+     * Parse a PrincipalName and return the names.
+     */
+    public static List<String> getNames( String principalNames ) throws ParseException
+    {
+        if ( principalNames == null )
+        {
+            return EMPTY_PRINCIPAL_NAME;
+        }
+        
+        List<String> nameComponents = new ArrayList<String>();
+        
+        // Start the parsing. Another State Machine :)
+        char[] chars = principalNames.toCharArray();
+        
+        boolean escaped = false;
+        boolean done = false;
+        int start = 0;
+        int pos = 0;
+        
+        for ( int i = 0; i < chars.length; i++ )
+        {
+            pos = i;
+            
+            switch ( chars[i] )
+            {
+                case '\\' :
+                    escaped = !escaped;
+                    break;
+                    
+                case '/'  :
+                    if ( escaped )
+                    {
+                        escaped = false;
+                    }
+                    else 
+                    {
+                        // We have a new name component
+                        if ( i - start > 0 )
+                        {
+                            String nameComponent = new String( chars, start, i - start );
+                            nameComponents.add( nameComponent );
+                            start = i + 1;
+                        }
+                        else
+                        {
+                            throw new ParseException( "An empty name is not valid in a kerberos name", i );
+                        }
+                    }
+                    
+                    break;
+                    
+                case '@'  :
+                    if ( escaped )
+                    {
+                        escaped = false;
+                    }
+                    else
+                    {
+                        // We have reached the realm : let's get out
+                        done = true;
+                        // We have a new name component
+
+                        if ( i - start > 0 )
+                        {
+                            String nameComponent = new String( chars, start, i - start );
+                            nameComponents.add( nameComponent );
+                            start = i + 1;
+                        }
+                        else
+                        {
+                            throw new ParseException( "An empty name is not valid in a kerberos name", i );
+                        }
+                    }
+                    
+                    break;
+                    
+                default :
+            }
+            
+            if ( done )
+            {
+                break;
+            }
+        } 
+        
+        if ( escaped )
+        {
+            throw new ParseException( "A '/' at the end of a Kerberos Name is not valid.", pos );
+        }
+        
+        return nameComponents;
+    }
+}

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfo2Decoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfo2Decoder.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfo2Decoder.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfo2Decoder.java Mon Oct 22 09:28:37 2007
@@ -65,9 +65,9 @@
         EncryptionTypeInfo2Entry[] entrySequence = new EncryptionTypeInfo2Entry[sequence.size()];
 
         int ii = 0;
-        for ( Enumeration<DERSequence> e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
-            DERSequence object = e.nextElement();
+            DERSequence object = (DERSequence)e.nextElement();
             entrySequence[ii] = decode( object );
             ii++;
         }
@@ -89,9 +89,9 @@
         String salt = new String();
         byte[] s2kparams = new byte[0];
 
-        for ( Enumeration<DERTaggedObject> e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
-            DERTaggedObject object = e.nextElement();
+            DERTaggedObject object = (DERTaggedObject)e.nextElement();
             int tag = object.getTagNo();
             DEREncodable derObject = object.getObject();
 

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfoDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfoDecoder.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfoDecoder.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/EncryptionTypeInfoDecoder.java Mon Oct 22 09:28:37 2007
@@ -64,9 +64,9 @@
         EncryptionTypeInfoEntry[] entrySequence = new EncryptionTypeInfoEntry[sequence.size()];
 
         int ii = 0;
-        for ( Enumeration<DERSequence> e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
-            DERSequence object = e.nextElement();
+            DERSequence object = (DERSequence)e.nextElement();
             entrySequence[ii] = decode( object );
             ii++;
         }
@@ -86,9 +86,9 @@
         EncryptionType encryptionType = EncryptionType.NULL;
         byte[] salt = new byte[0];
 
-        for ( Enumeration<DERTaggedObject> e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
-            DERTaggedObject object = e.nextElement();
+            DERTaggedObject object = (DERTaggedObject)e.nextElement();
             int tag = object.getTagNo();
             DEREncodable derObject = object.getObject();
 

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/PrincipalNameDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/PrincipalNameDecoder.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/PrincipalNameDecoder.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/decoder/PrincipalNameDecoder.java Mon Oct 22 09:28:37 2007
@@ -23,7 +23,6 @@
 import java.util.Enumeration;
 
 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameModifier;
 import org.apache.directory.shared.asn1.der.DEREncodable;
 import org.apache.directory.shared.asn1.der.DERGeneralString;
 import org.apache.directory.shared.asn1.der.DERInteger;
@@ -50,9 +49,9 @@
      */
     public static PrincipalName decode( DERSequence sequence )
     {
-        PrincipalNameModifier modifier = new PrincipalNameModifier();
+        PrincipalName principalName = new PrincipalName();
 
-        for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
             DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
             int tag = object.getTagNo();
@@ -62,25 +61,26 @@
             {
                 case 0:
                     DERInteger nameType = ( DERInteger ) derObject;
-                    modifier.setType( nameType.intValue() );
+                    principalName.setNameType( nameType.intValue() );
                     break;
+                    
                 case 1:
                     DERSequence nameString = ( DERSequence ) derObject;
-                    decodeNameString( nameString, modifier );
+                    decodeNameString( nameString, principalName );
                     break;
             }
         }
 
-        return modifier.getPrincipalName();
+        return principalName;
     }
 
 
-    private static void decodeNameString( DERSequence sequence, PrincipalNameModifier modifier )
+    private static void decodeNameString( DERSequence sequence, PrincipalName principalName )
     {
-        for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
+        for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
         {
             DERGeneralString object = ( DERGeneralString ) e.nextElement();
-            modifier.addName( object.getString() );
+            principalName.addName( object.getString() );
         }
     }
 }

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/PrincipalNameEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/PrincipalNameEncoder.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/PrincipalNameEncoder.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/io/encoder/PrincipalNameEncoder.java Mon Oct 22 09:28:37 2007
@@ -75,7 +75,7 @@
     {
         DERSequence vector = new DERSequence();
 
-        vector.add( new DERTaggedObject( 0, DERInteger.valueOf( name.getNameType() ) ) );
+        vector.add( new DERTaggedObject( 0, DERInteger.valueOf( name.getNameType().getOrdinal() ) ) );
         vector.add( new DERTaggedObject( 1, encodeNameSequence( name ) ) );
 
         return vector;
@@ -105,24 +105,15 @@
     }
 
 
-    private static DERSequence encodeNameSequence( PrincipalName name )
+    private static DERSequence encodeNameSequence( PrincipalName principalName )
     {
-        Iterator<String> it = getNameStrings( name ).iterator();
-
         DERSequence vector = new DERSequence();
 
-        while ( it.hasNext() )
+        for ( String name:principalName.getNames() )
         {
-            vector.add( DERGeneralString.valueOf( it.next() ) );
+            vector.add( DERGeneralString.valueOf( name ) );
         }
 
         return vector;
-    }
-
-
-    private static List<String> getNameStrings( PrincipalName name )
-    {
-        String[] components = name.getNameComponent().split( COMPONENT_SEPARATOR );
-        return Arrays.asList( components );
     }
 }

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/KerberosPrincipalModifier.java Mon Oct 22 09:28:37 2007
@@ -45,7 +45,7 @@
         if ( nameComponent != null )
         {
             StringBuffer sb = new StringBuffer();
-            sb.append( nameComponent.getNameComponent() );
+            sb.append( nameComponent.getNameString() );
 
             if ( realm != null )
             {
@@ -53,7 +53,7 @@
                 sb.append( realm );
             }
 
-            return new KerberosPrincipal( sb.toString(), nameComponent.getNameType() );
+            return new KerberosPrincipal( sb.toString(), nameComponent.getNameType().getOrdinal() );
         }
 
         return null;

Modified: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalName.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalName.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalName.java (original)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalName.java Mon Oct 22 09:28:37 2007
@@ -17,13 +17,13 @@
  *  under the License. 
  *  
  */
-package org.apache.directory.server.kerberos.shared.messages.value;
+//package org.apache.directory.server.kerberos.shared.messages.value;
 
 
 /**
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
- */
+ *
 public class PrincipalName
 {
     private String nameComponent;
@@ -35,7 +35,7 @@
      *
      * @param nameComponent
      * @param nameType
-     */
+     *
     public PrincipalName( String nameComponent, int nameType )
     {
         this.nameComponent = nameComponent;
@@ -47,7 +47,7 @@
      * Returns the type of the {@link PrincipalName}.
      *
      * @return The type of the {@link PrincipalName}.
-     */
+     *
     public int getNameType()
     {
         return nameType;
@@ -58,9 +58,419 @@
      * Returns the name component.
      *
      * @return The name component.
-     */
+     *
     public String getNameComponent()
     {
         return nameComponent;
+    }
+}*/
+
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.messages.value;
+
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.directory.server.kerberos.shared.KerberosUtils;
+import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
+import org.apache.directory.shared.asn1.AbstractAsn1Object;
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A principal Name, componsed of a type and N names.
+ * 
+ * PrincipalName   ::= SEQUENCE {
+ *        name-type       [0] Int32,
+ *        name-string     [1] SEQUENCE OF KerberosString
+ * }
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class PrincipalName extends AbstractAsn1Object
+{
+    /** The logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PrincipalName.class );
+
+    /** Speedup for logs */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The type for this principal */
+    private PrincipalNameType nameType;
+
+    /** The principal name - we may have more than one - */
+    private List<String> nameString;
+    
+    /** The principal name as a byte[], for encoding purpose */
+    private transient List<byte[]> nameBytes;
+    
+    // Storage for computed lengths
+    private transient int principalNameSeqLength;
+    private transient int principalTypeTagLength;
+    private transient int principalTypeLength;
+    private transient int principalStringsTagLength;
+    private transient int principalStringsSeqLength;
+
+    /**
+     * Creates a new empty instance of PrincipalName.
+     *
+     * @param principal A Sun kerberosPrincipal instance
+     */
+    public PrincipalName()
+    {
+    }
+
+    /**
+     * Creates a new instance of PrincipalName, given a KerberosPrincipal.
+     * 
+     * We assume that a principal has only one type, even if there are
+     * more than one name component.
+     *
+     * @param principal A Sun kerberosPrincipal instance
+     */
+    public PrincipalName( KerberosPrincipal principal ) throws ParseException
+    {
+        nameString = KerberosUtils.getNames( principal );
+
+        this.nameType = PrincipalNameType.getTypeByOrdinal( principal.getNameType() );
+    }
+    
+    /**
+     * Creates a new instance of PrincipalName given a String and an 
+     * prinipal type.
+     * 
+     * @param nameString The name string, which can contains more than one nameComponent
+     * @param nameType The principal name
+     */
+    public PrincipalName( String nameString, PrincipalNameType nameType )  throws ParseException
+    {
+        this.nameString = KerberosUtils.getNames( nameString );
+        
+        this.nameType = nameType;
+    }
+
+
+    /**
+     * Creates a new instance of PrincipalName.
+     *
+     * @param nameComponent
+     * @param nameType
+     */
+    public PrincipalName( String nameString, int nameType ) throws ParseException
+    {
+        this.nameString = KerberosUtils.getNames( nameString );
+        
+        this.nameType = PrincipalNameType.getTypeByOrdinal( nameType );
+    }
+
+
+    /**
+     * Returns the type of the {@link PrincipalName}.
+     *
+     * @return The type of the {@link PrincipalName}.
+     */
+    public PrincipalNameType getNameType()
+    {
+        return nameType;
+    }
+                    
+    /** 
+     * Set the Principal name Type
+     * @param nameType the Principal name Type
+     */
+    public void setNameType( PrincipalNameType nameType )
+    {
+        this.nameType = nameType;
+    }
+
+    /** 
+     * Set the Principal name Type
+     * @param nameType the Principal name Type
+     */
+    public void setNameType( int nameType )
+    {
+        this.nameType = PrincipalNameType.getTypeByOrdinal( nameType );
+    }
+
+    /**
+     * Returns the name components.
+     *
+     * @return The name components.
+     */
+    public List<String> getNames()
+    {
+        return nameString;
+    }
+
+
+    /**
+     * @return A String representing the principal names as a String 
+     */
+    public String getNameString()
+    {
+        if ( ( nameString == null ) || ( nameString.size() == 0 ) )
+        {
+            return "";
+        }
+        else
+        {
+            StringBuilder sb = new StringBuilder();
+            boolean isFirst = true;
+
+            for ( String name : nameString )
+            {
+                if ( isFirst )
+                {
+                    isFirst = false;
+                }
+                else
+                {
+                    sb.append( '/' );
+                }
+
+                sb.append( name );
+            }
+
+            return sb.toString();
+        }
+    }
+
+
+    /**
+     * Add a new name to the PrincipalName
+     * @param name The name to add
+     */
+    public void addName( String name )
+    {
+        if ( nameString == null )
+        {
+            nameString = new ArrayList<String>();
+        }
+
+        nameString.add( name );
+    }
+
+
+    /**
+     * Compute the PrincipalName length
+     * 
+     * PrincipalName :
+     * 
+     * 0x30 L1 PrincipalName sequence
+     *  |
+     *  +--> 0xA1 L2 name-type tag
+     *  |     |
+     *  |     +--> 0x02 L2-1 addressType (int)
+     *  |
+     *  +--> 0xA2 L3 name-string tag
+     *        |
+     *        +--> 0x30 L3-1 name-string (SEQUENCE OF KerberosString)
+     *              |
+     *              +--> 0x1B L4[1] value (KerberosString)
+     *              |
+     *              +--> 0x1B L4[2] value (KerberosString)
+     *              |
+     *              ...
+     *              |
+     *              +--> 0x1B L4[n] value (KerberosString)
+     */
+    public int computeLength()
+    {
+        // The principalName can't be empty.
+        principalTypeLength = Value.getNbBytes( nameType.getOrdinal() );
+        principalTypeTagLength = 1 + TLV.getNbBytes( principalTypeLength ) + principalTypeLength;
+        
+        principalNameSeqLength = 1 + TLV.getNbBytes( principalTypeTagLength ) + principalTypeTagLength;
+
+        // Compute the keyValue
+        if ( ( nameString == null ) || ( nameString.size() == 0 ) )
+        {
+            principalStringsSeqLength = 0;
+        }
+        else
+        {
+            principalStringsSeqLength = 0;
+            nameBytes = new ArrayList<byte[]>( nameString.size() );
+
+            for ( String name : nameString )
+            {
+                if ( name != null )
+                {
+                    byte[] bytes = StringTools.getBytesUtf8( name );
+                    nameBytes.add( bytes );
+                    principalStringsSeqLength += 1 + TLV.getNbBytes( bytes.length ) + bytes.length;
+                }
+                else
+                {
+                    nameBytes.add( StringTools.EMPTY_BYTES );
+                    principalStringsSeqLength += 1 + 1;
+                }
+            }
+        }
+
+        principalStringsTagLength = 1 + TLV.getNbBytes( principalStringsSeqLength ) + principalStringsSeqLength;
+        principalNameSeqLength += 1 + TLV.getNbBytes( principalStringsTagLength ) + principalStringsTagLength;
+
+        // Compute the whole sequence length
+        return 1 + TLV.getNbBytes( principalNameSeqLength ) + principalNameSeqLength;
+    }
+
+
+    /**
+     * Encode the PrincipalName message to a PDU. 
+     * 
+     * PrincipalName :
+     * 
+     * 0x30 LL
+     *   0xA0 LL 
+     *     0x02 0x01 name-type (integer)
+     *   0xA1 LL 
+     *     0x30 LL name-string (SEQUENCE OF KerberosString)
+     *       0x1B LL name-string[1]
+     *       0x1B LL name-string[2]
+     *       ...
+     *       0x1B LL name-string[n]
+     * 
+     * @param buffer The buffer where to put the PDU. It should have been allocated
+     * before, with the right size.
+     * @return The constructed PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            // The PrincipalName SEQ Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( TLV.getBytes( principalNameSeqLength ) );
+
+            // The name-type, first the tag, then the value
+            buffer.put( ( byte ) 0xA0 );
+            buffer.put( TLV.getBytes( principalTypeTagLength ) );
+            Value.encode( buffer, nameType.getOrdinal() );
+
+            // The name-string tag
+            buffer.put( ( byte ) 0xA1 );
+            buffer.put( TLV.getBytes( principalStringsTagLength ) );
+
+            // The name-string sequence
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+
+            if ( ( nameString == null ) || ( nameString.size() == 0 ) )
+            {
+                buffer.put( ( byte ) 0x00 );
+            }
+            else
+            {
+                buffer.put( TLV.getBytes( principalStringsSeqLength ) );
+
+                // The kerberosStrings
+                for ( byte[] name : nameBytes )
+                {
+                    buffer.put( UniversalTag.GENERALIZED_STRING_TAG );
+
+                    if ( ( name == null ) || ( name.length == 0 ) )
+                    {
+                        buffer.put( ( byte ) 0x00 );
+                    }
+                    else
+                    {
+                        buffer.put( TLV.getBytes( name.length ) );
+                        buffer.put( name );
+                    }
+                }
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            LOG.error(
+                "Cannot encode the principalName object, the PDU size is {} when only {} bytes has been allocated", 1
+                    + TLV.getNbBytes( principalNameSeqLength ) + principalNameSeqLength, buffer.capacity() );
+            throw new EncoderException( "The PDU buffer size is too small !" );
+        }
+
+        if ( IS_DEBUG )
+        {
+            LOG.debug( "PrinipalName encoding : {}", StringTools.dumpBytes( buffer.array() ) );
+            LOG.debug( "PrinipalName initial value : {}", toString() );
+        }
+
+        return buffer;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "PincipalName : {\n" );
+
+        sb.append( "    name-type: " ).append( nameType ).append( '\n' );
+
+        if ( ( nameString != null ) && ( nameString.size() != 0 ) )
+        {
+            sb.append( "    name-string : <" );
+            boolean isFirst = true;
+
+            for ( String name : nameString )
+            {
+                if ( isFirst )
+                {
+                    isFirst = false;
+                }
+                else
+                {
+                    sb.append( ", " );
+                }
+
+                sb.append( '\'' ).append( name ).append( '\'' );
+            }
+
+            sb.append( ">\n}" );
+        }
+        else
+        {
+            sb.append( "    no name-string\n}" );
+        }
+
+        return sb.toString();
     }
 }

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/PrincipalNameType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/PrincipalNameType.java?rev=587146&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/PrincipalNameType.java (added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/messages/value/types/PrincipalNameType.java Mon Oct 22 09:28:37 2007
@@ -0,0 +1,178 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.messages.value.types;
+
+
+/**
+ * An enum describing the differnet types of Principal.
+ * 
+ * Here is the list, taken from RFC 4120 :
+ *  NT-UNKNOWN        0    Name type not known
+ *  NT-PRINCIPAL      1    Just the name of the principal as in DCE,
+ *                           or for users
+ *  NT-SRV-INST       2    Service and other unique instance (krbtgt)
+ *  NT-SRV-HST        3    Service with host name as instance
+ *                           (telnet, rcommands)
+ *  NT-SRV-XHST       4    Service with host as remaining components
+ *  NT-UID            5    Unique ID
+ *  NT-X500-PRINCIPAL 6    Encoded X.509 Distinguished name [RFC2253]
+ *  NT-SMTP-NAME      7    Name in form of SMTP email name
+ *                           (e.g., user@example.com)
+ *  NT-ENTERPRISE    10    Enterprise name - may be mapped to principal
+ *                           name
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $
+ */
+public enum PrincipalNameType
+{
+    /**
+     * Constant for the "Name type not known" principal name type.
+     */
+    KRB_NT_UNKNOWN( 0 ),
+
+    /**
+     * Constant for the "Just the name of the principal as in DCE, or for users" principal name type.
+     */
+    KRB_NT_PRINCIPAL( 1 ),
+
+    /**
+     * Constant for the "Service and other unique instance (krbtgt)" principal name type.
+     */
+    KRB_NT_SRV_INST( 2 ),
+
+    /**
+     * Constant for the "Service with host name as instance (telnet, rcommands)" principal name type.
+     */
+    KRB_NT_SRV_HST( 3 ),
+
+    /**
+     * Constant for the "Service with host as remaining components" principal name type.
+     */
+    KRB_NT_SRV_XHST( 4 ),
+
+    /**
+     * Constant for the "Unique ID" principal name type.
+     */
+    KRB_NT_UID( 5 ),
+
+    /**
+     * Constant for the "Encoded X.509 Distinguished name [RFC2253]" principal name type.
+     */
+    KRB_NT_X500_PRINCIPAL( 6 ),
+
+    /**
+     * Constant for the "Name in form of SMTP email name (e.g., user@example.com)" principal name type.
+     */
+    KRB_NT_SMTP_NAME( 7 ),
+
+    /**
+     * Constant for the "Enterprise name; may be mapped to principal name" principal name type.
+     */
+    KRB_NT_ENTERPRISE( 10 );
+
+    /**
+     * The value/code for the principal name type.
+     */
+    private final int ordinal;
+
+
+    /**
+     * Private constructor prevents construction outside of this class.
+     */
+    private PrincipalNameType( int ordinal )
+    {
+        this.ordinal = ordinal;
+    }
+
+
+    /**
+     * Returns the principal name type when specified by its ordinal.
+     *
+     * @param type
+     * @return The principal name type.
+     */
+    public static PrincipalNameType getTypeByOrdinal( int type )
+    {
+        switch ( type )
+        {
+            case 0 : return KRB_NT_UNKNOWN;
+            case 1 : return KRB_NT_PRINCIPAL;
+            case 2 : return KRB_NT_SRV_INST;
+            case 3 : return KRB_NT_SRV_HST;
+            case 4 : return KRB_NT_SRV_XHST;
+            case 5 : return KRB_NT_UID;
+            case 6 : return KRB_NT_X500_PRINCIPAL;
+            case 7 : return KRB_NT_SMTP_NAME;
+            case 10 : return KRB_NT_ENTERPRISE;
+            default : return KRB_NT_UNKNOWN;
+        }
+    }
+
+
+    /**
+     * Returns the number associated with this principal name type.
+     *
+     * @return The principal name type ordinal.
+     */
+    public int getOrdinal()
+    {
+        return ordinal;
+    }
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        switch ( this )
+        {
+            case KRB_NT_UNKNOWN         : 
+                return "Name type not known" + "(" + ordinal + ")";
+                
+            case KRB_NT_PRINCIPAL       : 
+                return "Just the name of the principal as in DCE, or for users" + "(" + ordinal + ")";
+                
+            case KRB_NT_SRV_INST        : 
+                return "Service and other unique instance (krbtgt)" + "(" + ordinal + ")";
+            
+            case KRB_NT_SRV_HST         : 
+                return "Service with host name as instance (telnet, rcommands)" + "(" + ordinal + ")";
+            
+            case KRB_NT_SRV_XHST        : 
+                return "Service with host as remaining components" + "(" + ordinal + ")";
+            
+            case KRB_NT_UID             : 
+                return "Unique ID" + "(" + ordinal + ")";
+            
+            case KRB_NT_X500_PRINCIPAL  : 
+                return "Encoded X.509 Distinguished name [RFC2253]" + "(" + ordinal + ")";
+            
+            case KRB_NT_SMTP_NAME       : 
+                return "Name in form of SMTP email name (e.g., user@example.com)" + "(" + ordinal + ")";
+            
+            case KRB_NT_ENTERPRISE      : 
+                return "Enterprise name; may be mapped to principal name" + "(" + ordinal + ")";
+            
+            default                     : 
+                return "unknown name type" + "(" + ordinal + ")";
+        }
+    }
+}

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/KerberosUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/KerberosUtilsTest.java?rev=587146&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/KerberosUtilsTest.java (added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/KerberosUtilsTest.java Mon Oct 22 09:28:37 2007
@@ -0,0 +1,139 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared;
+
+import java.text.ParseException;
+import java.util.List;
+
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.directory.server.kerberos.shared.KerberosUtils;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the KerberosUtils class
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class KerberosUtilsTest extends TestCase
+{
+    public void setUp()
+    {
+        // First setup a default realm
+        System.setProperty( "java.security.krb5.realm", "APACHE.ORG" );
+        System.setProperty( "java.security.krb5.kdc", "localhost" );
+    }
+
+    
+    public void testKerberosNameSimple() throws Exception
+    {
+        KerberosPrincipal kp = new KerberosPrincipal( "abc" );
+        List<String> names = KerberosUtils.getNames( kp );
+     
+        assertEquals( 1, names.size() );
+        assertEquals( "abc", names.get( 0 ) );
+    }
+
+    
+    /**
+    public void testKerberosNameEscaped() throws Exception
+    {
+        KerberosPrincipal kp = new KerberosPrincipal( "abc\\//d\\@f/g\\\\hi" );
+        List<String> names = KerberosUtils.getNames( kp );
+     
+        assertEquals( 3, names.size() );
+        assertEquals( "abc\\/", names.get( 0 ) );
+        assertEquals( "d\\@g", names.get( 1 ) );
+        assertEquals( "g\\\\hi", names.get( 2 ) );
+    }
+    */
+
+
+    public void testKerberosNameSimpleWithRealm() throws Exception
+    {
+        KerberosPrincipal kp = new KerberosPrincipal( "abc@APACHE.ORG" );
+        List<String> names = KerberosUtils.getNames( kp );
+     
+        assertEquals( 1, names.size() );
+        assertEquals( "abc", names.get( 0 ) );
+    }
+    
+    public void testKerberosNameThree() throws Exception
+    {
+        KerberosPrincipal kp = new KerberosPrincipal( "abc/def/ghi" );
+        List<String> names = KerberosUtils.getNames( kp );
+     
+        assertEquals( 3, names.size() );
+        assertEquals( "abc", names.get( 0 ) );
+        assertEquals( "def", names.get( 1 ) );
+        assertEquals( "ghi", names.get( 2 ) );
+    }
+
+    public void testKerberosNameThreeWithRealm() throws Exception
+    {
+        KerberosPrincipal kp = new KerberosPrincipal( "abc/def/ghi@APACHE.ORG" );
+        List<String> names = KerberosUtils.getNames( kp );
+     
+        assertEquals( 3, names.size() );
+        assertEquals( "abc", names.get( 0 ) );
+        assertEquals( "def", names.get( 1 ) );
+        assertEquals( "ghi", names.get( 2 ) );
+    }
+
+
+    
+    /*
+    public void testKerberosEndingSlash()
+    {
+        try
+        {
+            KerberosPrincipal kp = new KerberosPrincipal( "abc/def/ghi/" );
+            KerberosUtils.getNames( kp );
+            
+            // Should not reach this point
+            fail();
+        }
+        catch ( ParseException pe )
+        {
+            assertTrue( true );
+        }
+    }
+    */
+    
+    
+    /*
+    public void testKerberosEndingSlashWithRealm()
+    {
+        try
+        {
+            KerberosPrincipal kp = new KerberosPrincipal( "abc/def/ghi/@APACHE.ORG" );
+            KerberosUtils.getNames( kp );
+            
+            // Should not reach this point
+            fail();
+        }
+        catch ( ParseException pe )
+        {
+            assertTrue( true );
+        }
+    }
+    */
+}

Added: directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameTest.java?rev=587146&view=auto
==============================================================================
--- directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameTest.java (added)
+++ directory/apacheds/branches/bigbang/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/messages/value/PrincipalNameTest.java Mon Oct 22 09:28:37 2007
@@ -0,0 +1,110 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.messages.value;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
+import org.apache.directory.shared.ldap.util.StringTools;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test the PrincipalName encoding and decoding
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 542147 $, $Date: 2007-05-28 10:14:21 +0200 (Mon, 28 May 2007) $
+ */
+public class PrincipalNameTest extends TestCase
+{
+    public void testEncodingPrincipalNameOneName() throws Exception
+    {
+        PrincipalName principal = new PrincipalName( "Test@APACHE", PrincipalNameType.KRB_NT_PRINCIPAL );
+
+        ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
+
+        principal.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+              0x30, 0x0F, 
+                (byte) 0xA0, 0x03, 
+                  0x02, 0x01, 0x01, 
+                (byte) 0xA1, 0x08, 
+                  0x30, 0x06, 
+                    0x1B, 0x04, 
+                      'T', 'e', 's', 't' 
+            };
+
+        assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
+    }
+
+
+    public void testEncodingPrincipalName3names() throws Exception
+    {
+        PrincipalName principal = new PrincipalName( "Test1@APACHE", PrincipalNameType.KRB_NT_PRINCIPAL );
+        principal.addName( "Test2" );
+        principal.addName( "Test3" );
+
+        ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
+
+        principal.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+              0x30, 0x1e, 
+                (byte) 0xA0, 0x03, 
+                  0x02, 0x01, 0x01, 
+                (byte) 0xA1, 0x17, 
+                  0x30, 0x15, 
+                    0x1B, 0x05, 
+                      'T', 'e', 's', 't', '1', 
+                    0x1B, 0x05, 
+                      'T', 'e', 's', 't', '2', 
+                    0x1B, 0x05, 
+                      'T', 'e', 's', 't', '3' 
+            };
+
+        assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
+    }
+
+
+    public void testEncodingPrincipalNameNullName() throws Exception
+    {
+        PrincipalName principal = new PrincipalName( null, PrincipalNameType.KRB_NT_PRINCIPAL );
+
+        ByteBuffer encoded = ByteBuffer.allocate( principal.computeLength() );
+
+        principal.encode( encoded );
+
+        byte[] expectedResult = new byte[]
+            { 
+              0x30, 0x09, 
+                ( byte ) 0xA0, 0x03, 
+                  0x02, 0x01, 0x01, 
+                ( byte ) 0xA1, 0x02, 
+                  0x30, 0x00 
+            };
+
+        assertEquals( StringTools.dumpBytes( expectedResult ), StringTools.dumpBytes( encoded.array() ) );
+    }
+}

Modified: directory/apacheds/branches/bigbang/protocol-changepw/src/test/java/org/apache/directory/server/changepw/protocol/ChangepwProtocolHandlerTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-changepw/src/test/java/org/apache/directory/server/changepw/protocol/ChangepwProtocolHandlerTest.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-changepw/src/test/java/org/apache/directory/server/changepw/protocol/ChangepwProtocolHandlerTest.java (original)
+++ directory/apacheds/branches/bigbang/protocol-changepw/src/test/java/org/apache/directory/server/changepw/protocol/ChangepwProtocolHandlerTest.java Mon Oct 22 09:28:37 2007
@@ -55,8 +55,7 @@
 import org.apache.directory.server.kerberos.shared.messages.value.HostAddress;
 import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameModifier;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameType;
+import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
 import org.apache.directory.server.kerberos.shared.store.PrincipalStore;
 import org.apache.directory.server.kerberos.shared.store.TicketFactory;
 import org.apache.mina.common.IoFilterChain;
@@ -320,13 +319,13 @@
     }
 
 
-    private PrincipalName getPrincipalName( String principalName )
+    private PrincipalName getPrincipalName( String name )
     {
-        PrincipalNameModifier principalNameModifier = new PrincipalNameModifier();
-        principalNameModifier.addName( principalName );
-        principalNameModifier.setType( PrincipalNameType.KRB_NT_PRINCIPAL.getOrdinal() );
+        PrincipalName principalName = new PrincipalName();
+        principalName.addName( name );
+        principalName.setNameType( PrincipalNameType.KRB_NT_PRINCIPAL );
 
-        return principalNameModifier.getPrincipalName();
+        return principalName;
     }
 
     private static class DummySession extends BaseIoSession

Modified: directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractAuthenticationServiceTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractAuthenticationServiceTest.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractAuthenticationServiceTest.java (original)
+++ directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractAuthenticationServiceTest.java Mon Oct 22 09:28:37 2007
@@ -41,8 +41,7 @@
 import org.apache.directory.server.kerberos.shared.messages.value.PreAuthenticationDataModifier;
 import org.apache.directory.server.kerberos.shared.messages.value.PreAuthenticationDataType;
 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameModifier;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameType;
+import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
 import org.apache.mina.common.IoFilterChain;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoService;
@@ -98,13 +97,13 @@
     }
 
 
-    protected PrincipalName getPrincipalName( String principalName )
+    protected PrincipalName getPrincipalName( String name )
     {
-        PrincipalNameModifier principalNameModifier = new PrincipalNameModifier();
-        principalNameModifier.addName( principalName );
-        principalNameModifier.setType( PrincipalNameType.KRB_NT_PRINCIPAL.getOrdinal() );
+        PrincipalName principalName = new PrincipalName();
+        principalName.addName( name );
+        principalName.setNameType( PrincipalNameType.KRB_NT_PRINCIPAL );
 
-        return principalNameModifier.getPrincipalName();
+        return principalName;
     }
 
 

Modified: directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractTicketGrantingServiceTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractTicketGrantingServiceTest.java?rev=587146&r1=587145&r2=587146&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractTicketGrantingServiceTest.java (original)
+++ directory/apacheds/branches/bigbang/protocol-kerberos/src/test/java/org/apache/directory/server/kerberos/protocol/AbstractTicketGrantingServiceTest.java Mon Oct 22 09:28:37 2007
@@ -57,11 +57,10 @@
 import org.apache.directory.server.kerberos.shared.messages.value.PreAuthenticationDataModifier;
 import org.apache.directory.server.kerberos.shared.messages.value.PreAuthenticationDataType;
 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameModifier;
-import org.apache.directory.server.kerberos.shared.messages.value.PrincipalNameType;
 import org.apache.directory.server.kerberos.shared.messages.value.RequestBody;
 import org.apache.directory.server.kerberos.shared.messages.value.TicketFlags;
 import org.apache.directory.server.kerberos.shared.messages.value.TransitedEncoding;
+import org.apache.directory.server.kerberos.shared.messages.value.types.PrincipalNameType;
 import org.apache.mina.common.IoFilterChain;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoService;
@@ -322,13 +321,13 @@
     }
 
 
-    protected PrincipalName getPrincipalName( String principalName )
+    protected PrincipalName getPrincipalName( String name )
     {
-        PrincipalNameModifier principalNameModifier = new PrincipalNameModifier();
-        principalNameModifier.addName( principalName );
-        principalNameModifier.setType( PrincipalNameType.KRB_NT_PRINCIPAL.getOrdinal() );
+        PrincipalName principalName = new PrincipalName();
+        principalName.addName( name );
+        principalName.setNameType( PrincipalNameType.KRB_NT_PRINCIPAL );
 
-        return principalNameModifier.getPrincipalName();
+        return principalName;
     }
 
     protected static class DummySession extends BaseIoSession