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.encoder;
021
022
023 import java.util.Arrays;
024 import java.util.Iterator;
025 import java.util.List;
026
027 import javax.security.auth.kerberos.KerberosPrincipal;
028
029 import org.apache.directory.server.kerberos.shared.messages.value.PrincipalName;
030 import org.apache.directory.shared.asn1.der.DERGeneralString;
031 import org.apache.directory.shared.asn1.der.DERInteger;
032 import org.apache.directory.shared.asn1.der.DERSequence;
033 import org.apache.directory.shared.asn1.der.DERTaggedObject;
034
035
036 /**
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev: 587146 $, $Date: 2007-10-22 19:28:37 +0300 (Mon, 22 Oct 2007) $
039 */
040 public class PrincipalNameEncoder
041 {
042 private static final String COMPONENT_SEPARATOR = "/";
043 private static final String REALM_SEPARATOR = "@";
044
045
046 /**
047 * Encodes a {@link KerberosPrincipal} into a {@link DERSequence}.
048 *
049 * PrincipalName ::= SEQUENCE {
050 * name-type[0] INTEGER,
051 * name-string[1] SEQUENCE OF GeneralString
052 * }
053 *
054 * @param principal
055 * @return The {@link DERSequence}.
056 */
057 public static DERSequence encode( KerberosPrincipal principal )
058 {
059 DERSequence vector = new DERSequence();
060
061 vector.add( new DERTaggedObject( 0, DERInteger.valueOf( principal.getNameType() ) ) );
062 vector.add( new DERTaggedObject( 1, encodeNameSequence( principal ) ) );
063
064 return vector;
065 }
066
067
068 /**
069 * Encodes a {@link PrincipalName} into a {@link DERSequence}.
070 *
071 * @param name
072 * @return The {@link DERSequence}.
073 */
074 public static DERSequence encode( PrincipalName name )
075 {
076 DERSequence vector = new DERSequence();
077
078 vector.add( new DERTaggedObject( 0, DERInteger.valueOf( name.getNameType().getOrdinal() ) ) );
079 vector.add( new DERTaggedObject( 1, encodeNameSequence( name ) ) );
080
081 return vector;
082 }
083
084
085 private static DERSequence encodeNameSequence( KerberosPrincipal principal )
086 {
087 Iterator<String> it = getNameStrings( principal ).iterator();
088
089 DERSequence vector = new DERSequence();
090
091 while ( it.hasNext() )
092 {
093 vector.add( DERGeneralString.valueOf( it.next() ) );
094 }
095
096 return vector;
097 }
098
099
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 }