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
021 package org.apache.directory.server.changepw.io;
022
023
024 import java.io.IOException;
025 import java.util.Enumeration;
026
027 import org.apache.directory.server.changepw.value.ChangePasswordData;
028 import org.apache.directory.server.changepw.value.ChangePasswordDataModifier;
029 import org.apache.directory.server.kerberos.shared.io.decoder.PrincipalNameDecoder;
030 import org.apache.directory.shared.asn1.der.ASN1InputStream;
031 import org.apache.directory.shared.asn1.der.DEREncodable;
032 import org.apache.directory.shared.asn1.der.DERGeneralString;
033 import org.apache.directory.shared.asn1.der.DEROctetString;
034 import org.apache.directory.shared.asn1.der.DERSequence;
035 import org.apache.directory.shared.asn1.der.DERTaggedObject;
036
037
038 /**
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 * @version $Rev: 540371 $, $Date: 2007-05-22 03:00:43 +0300 (Tue, 22 May 2007) $
041 */
042 public class ChangePasswordDataDecoder
043 {
044 /**
045 * Decodes bytes into a ChangePasswordData.
046 *
047 * @param encodedChangePasswdData
048 * @return The {@link ChangePasswordData}.
049 * @throws IOException
050 */
051 public ChangePasswordData decodeChangePasswordData( byte[] encodedChangePasswdData ) throws IOException
052 {
053 ASN1InputStream ais = new ASN1InputStream( encodedChangePasswdData );
054
055 DERSequence sequence = ( DERSequence ) ais.readObject();
056
057 return decodeChangePasswdData( sequence );
058 }
059
060
061 protected ChangePasswordData decodeChangePasswdData( DERSequence sequence )
062 {
063 ChangePasswordDataModifier modifier = new ChangePasswordDataModifier();
064
065 for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
066 {
067 DERTaggedObject object = ( ( DERTaggedObject ) e.nextElement() );
068 int tag = object.getTagNo();
069 DEREncodable derObject = object.getObject();
070 switch ( tag )
071 {
072 case 0:
073 DEROctetString tag0 = ( DEROctetString ) derObject;
074 modifier.setNewPassword( tag0.getOctets() );
075 break;
076 case 1:
077 DERSequence tag1 = ( DERSequence ) derObject;
078 modifier.setTargetName( PrincipalNameDecoder.decode( tag1 ) );
079 break;
080 case 2:
081 DERGeneralString tag2 = ( DERGeneralString ) derObject;
082 modifier.setTargetRealm( tag2.getString() );
083 break;
084 default:
085 break;
086 }
087 }
088
089 return modifier.getChangePasswdData();
090 }
091 }