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  package org.apache.directory.server.changepw.io;
21  
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.nio.ByteBuffer;
26  
27  import org.apache.directory.server.changepw.value.ChangePasswordData;
28  import org.apache.directory.server.kerberos.shared.io.encoder.PrincipalNameEncoder;
29  import org.apache.directory.shared.asn1.der.ASN1OutputStream;
30  import org.apache.directory.shared.asn1.der.DERGeneralString;
31  import org.apache.directory.shared.asn1.der.DEROctetString;
32  import org.apache.directory.shared.asn1.der.DERSequence;
33  import org.apache.directory.shared.asn1.der.DERTaggedObject;
34  
35  
36  /**
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev$, $Date$
39   */
40  public class ChangePasswordDataEncoder
41  {
42      /**
43       * Encodes a {@link ChangePasswordData} into a byte array.
44       *
45       * @param data
46       * @return The byte array.
47       * @throws IOException
48       */
49      public byte[] encode( ChangePasswordData data ) throws IOException
50      {
51          ByteArrayOutputStream baos = new ByteArrayOutputStream();
52          ASN1OutputStream aos = new ASN1OutputStream( baos );
53  
54          DERSequence dataSequence = encodeDataSequence( data );
55          aos.writeObject( dataSequence );
56  
57          aos.close();
58  
59          return baos.toByteArray();
60      }
61  
62  
63      /**
64       * Encodes a {@link ChangePasswordData} into a {@link ByteBuffer}.
65       *
66       * @param data
67       * @param out
68       * @throws IOException
69       */
70      public void encode( ChangePasswordData data, ByteBuffer out ) throws IOException
71      {
72          ASN1OutputStream aos = new ASN1OutputStream( out );
73  
74          DERSequence sequence = encodeDataSequence( data );
75          aos.writeObject( sequence );
76  
77          aos.close();
78      }
79  
80  
81      private DERSequence encodeDataSequence( ChangePasswordData data )
82      {
83          DERSequence sequence = new DERSequence();
84          sequence.add( new DERTaggedObject( 0, new DEROctetString( data.getPassword() ) ) );
85  
86          // OPTIONAL
87          if ( data.getPrincipalName() != null )
88          {
89              sequence.add( new DERTaggedObject( 1, PrincipalNameEncoder.encode( data.getPrincipalName() ) ) );
90          }
91  
92          // OPTIONAL
93          if ( data.getRealm() != null )
94          {
95              sequence.add( new DERTaggedObject( 2, DERGeneralString.valueOf( data.getRealm() ) ) );
96          }
97  
98          return sequence;
99      }
100 }