001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.kerberos.shared.io.decoder;
021
022
023 import java.util.Enumeration;
024
025 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
026 import org.apache.directory.shared.asn1.der.DEREncodable;
027 import org.apache.directory.shared.asn1.der.DERGeneralString;
028 import org.apache.directory.shared.asn1.der.DERInteger;
029 import org.apache.directory.shared.asn1.der.DERSequence;
030 import org.apache.directory.shared.asn1.der.DERTaggedObject;
031
032
033 /**
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 587146 $, $Date: 2007-10-22 19:28:37 +0300 (Mon, 22 Oct 2007) $
036 */
037 public class PrincipalNameDecoder
038 {
039 /**
040 * Decodes a {@link DERSequence} into a {@link PrincipalName}.
041 *
042 * PrincipalName ::= SEQUENCE {
043 * name-type[0] INTEGER,
044 * name-string[1] SEQUENCE OF GeneralString
045 * }
046 *
047 * @param sequence
048 * @return The {@link PrincipalName}.
049 */
050 public static PrincipalName decode( DERSequence sequence )
051 {
052 PrincipalName principalName = new PrincipalName();
053
054 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
055 {
056 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
057 int tag = object.getTagNo();
058 DEREncodable derObject = object.getObject();
059
060 switch ( tag )
061 {
062 case 0:
063 DERInteger nameType = ( DERInteger ) derObject;
064 principalName.setNameType( nameType.intValue() );
065 break;
066
067 case 1:
068 DERSequence nameString = ( DERSequence ) derObject;
069 decodeNameString( nameString, principalName );
070 break;
071 }
072 }
073
074 return principalName;
075 }
076
077
078 private static void decodeNameString( DERSequence sequence, PrincipalName principalName )
079 {
080 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
081 {
082 DERGeneralString object = ( DERGeneralString ) e.nextElement();
083 principalName.addName( object.getString() );
084 }
085 }
086 }