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.decoder;
21
22
23 import java.util.Enumeration;
24
25 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
26 import org.apache.directory.shared.asn1.der.DEREncodable;
27 import org.apache.directory.shared.asn1.der.DERGeneralString;
28 import org.apache.directory.shared.asn1.der.DERInteger;
29 import org.apache.directory.shared.asn1.der.DERSequence;
30 import org.apache.directory.shared.asn1.der.DERTaggedObject;
31
32
33 /**
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 * @version $Rev: 587146 $, $Date: 2007-10-22 18:28:37 +0200 (Mo, 22 Okt 2007) $
36 */
37 public class PrincipalNameDecoder
38 {
39 /**
40 * Decodes a {@link DERSequence} into a {@link PrincipalName}.
41 *
42 * PrincipalName ::= SEQUENCE {
43 * name-type[0] INTEGER,
44 * name-string[1] SEQUENCE OF GeneralString
45 * }
46 *
47 * @param sequence
48 * @return The {@link PrincipalName}.
49 */
50 public static PrincipalName decode( DERSequence sequence )
51 {
52 PrincipalName principalName = new PrincipalName();
53
54 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
55 {
56 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
57 int tag = object.getTagNo();
58 DEREncodable derObject = object.getObject();
59
60 switch ( tag )
61 {
62 case 0:
63 DERInteger nameType = ( DERInteger ) derObject;
64 principalName.setNameType( nameType.intValue() );
65 break;
66
67 case 1:
68 DERSequence nameString = ( DERSequence ) derObject;
69 decodeNameString( nameString, principalName );
70 break;
71 }
72 }
73
74 return principalName;
75 }
76
77
78 private static void decodeNameString( DERSequence sequence, PrincipalName principalName )
79 {
80 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
81 {
82 DERGeneralString object = ( DERGeneralString ) e.nextElement();
83 principalName.addName( object.getString() );
84 }
85 }
86 }