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  
21  package org.apache.directory.server.changepw.io;
22  
23  
24  import java.io.IOException;
25  import java.util.Enumeration;
26  
27  import org.apache.directory.server.changepw.value.ChangePasswordData;
28  import org.apache.directory.server.changepw.value.ChangePasswordDataModifier;
29  import org.apache.directory.server.kerberos.shared.io.decoder.PrincipalNameDecoder;
30  import org.apache.directory.shared.asn1.der.ASN1InputStream;
31  import org.apache.directory.shared.asn1.der.DEREncodable;
32  import org.apache.directory.shared.asn1.der.DERGeneralString;
33  import org.apache.directory.shared.asn1.der.DEROctetString;
34  import org.apache.directory.shared.asn1.der.DERSequence;
35  import org.apache.directory.shared.asn1.der.DERTaggedObject;
36  
37  
38  /**
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Di, 22 Mai 2007) $
41   */
42  public class ChangePasswordDataDecoder
43  {
44      /**
45       * Decodes bytes into a ChangePasswordData.
46       *
47       * @param encodedChangePasswdData
48       * @return The {@link ChangePasswordData}.
49       * @throws IOException
50       */
51      public ChangePasswordData decodeChangePasswordData( byte[] encodedChangePasswdData ) throws IOException
52      {
53          ASN1InputStream ais = new ASN1InputStream( encodedChangePasswdData );
54  
55          DERSequence sequence = ( DERSequence ) ais.readObject();
56  
57          return decodeChangePasswdData( sequence );
58      }
59  
60  
61      protected ChangePasswordData decodeChangePasswdData( DERSequence sequence )
62      {
63          ChangePasswordDataModifier modifier = new ChangePasswordDataModifier();
64  
65          for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
66          {
67              DERTaggedObject object = ( ( DERTaggedObject ) e.nextElement() );
68              int tag = object.getTagNo();
69              DEREncodable derObject = object.getObject();
70              switch ( tag )
71              {
72                  case 0:
73                      DEROctetString tag0 = ( DEROctetString ) derObject;
74                      modifier.setNewPassword( tag0.getOctets() );
75                      break;
76                  case 1:
77                      DERSequence tag1 = ( DERSequence ) derObject;
78                      modifier.setTargetName( PrincipalNameDecoder.decode( tag1 ) );
79                      break;
80                  case 2:
81                      DERGeneralString tag2 = ( DERGeneralString ) derObject;
82                      modifier.setTargetRealm( tag2.getString() );
83                      break;
84                  default:
85                      break;
86              }
87          }
88  
89          return modifier.getChangePasswdData();
90      }
91  }