View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.io.encoder;
21  
22  
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import javax.security.auth.kerberos.KerberosPrincipal;
28  
29  import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
30  import org.apache.directory.shared.asn1.der.DERGeneralString;
31  import org.apache.directory.shared.asn1.der.DERInteger;
32  import org.apache.directory.shared.asn1.der.DERSequence;
33  import org.apache.directory.shared.asn1.der.DERTaggedObject;
34  
35  
36  /**
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 587146 $, $Date: 2007-10-22 18:28:37 +0200 (Mo, 22 Okt 2007) $
39   */
40  public class PrincipalNameEncoder
41  {
42      private static final String COMPONENT_SEPARATOR = "/";
43      private static final String REALM_SEPARATOR = "@";
44  
45  
46      /**
47       * Encodes a {@link KerberosPrincipal} into a {@link DERSequence}.
48       * 
49       * PrincipalName ::=   SEQUENCE {
50       *               name-type[0]     INTEGER,
51       *               name-string[1]   SEQUENCE OF GeneralString
52       * }
53       * 
54       * @param principal 
55       * @return The {@link DERSequence}.
56       */
57      public static DERSequence encode( KerberosPrincipal principal )
58      {
59          DERSequence vector = new DERSequence();
60  
61          vector.add( new DERTaggedObject( 0, DERInteger.valueOf( principal.getNameType() ) ) );
62          vector.add( new DERTaggedObject( 1, encodeNameSequence( principal ) ) );
63  
64          return vector;
65      }
66  
67  
68      /**
69       * Encodes a {@link PrincipalName} into a {@link DERSequence}.
70       *
71       * @param name
72       * @return The {@link DERSequence}.
73       */
74      public static DERSequence encode( PrincipalName name )
75      {
76          DERSequence vector = new DERSequence();
77  
78          vector.add( new DERTaggedObject( 0, DERInteger.valueOf( name.getNameType().getOrdinal() ) ) );
79          vector.add( new DERTaggedObject( 1, encodeNameSequence( name ) ) );
80  
81          return vector;
82      }
83  
84  
85      private static DERSequence encodeNameSequence( KerberosPrincipal principal )
86      {
87          Iterator<String> it = getNameStrings( principal ).iterator();
88  
89          DERSequence vector = new DERSequence();
90  
91          while ( it.hasNext() )
92          {
93              vector.add( DERGeneralString.valueOf( it.next() ) );
94          }
95  
96          return vector;
97      }
98  
99  
100     private static List<String> getNameStrings( KerberosPrincipal principal )
101     {
102         String nameComponent = principal.getName().split( REALM_SEPARATOR )[0];
103         String[] components = nameComponent.split( COMPONENT_SEPARATOR );
104         return Arrays.asList( components );
105     }
106 
107 
108     private static DERSequence encodeNameSequence( PrincipalName principalName )
109     {
110         DERSequence vector = new DERSequence();
111 
112         for ( String name:principalName.getNames() )
113         {
114             vector.add( DERGeneralString.valueOf( name ) );
115         }
116 
117         return vector;
118     }
119 }