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.kerberos.shared.io.decoder;
21  
22  
23  import java.util.Enumeration;
24  
25  import org.apache.directory.server.kerberos.shared.messages.value.HostAddress;
26  import org.apache.directory.server.kerberos.shared.messages.value.types.HostAddrType;
27  import org.apache.directory.server.kerberos.shared.messages.value.HostAddresses;
28  import org.apache.directory.shared.asn1.der.DEREncodable;
29  import org.apache.directory.shared.asn1.der.DERInteger;
30  import org.apache.directory.shared.asn1.der.DEROctetString;
31  import org.apache.directory.shared.asn1.der.DERSequence;
32  import org.apache.directory.shared.asn1.der.DERTaggedObject;
33  
34  
35  /**
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 587242 $, $Date: 2007-10-22 22:42:40 +0200 (Mo, 22 Okt 2007) $
38   */
39  public class HostAddressDecoder
40  {
41      /**
42       * HostAddress ::=     SEQUENCE  {
43       *                     addr-type[0]             INTEGER,
44       *                     address[1]               OCTET STRING
45       * }
46       */
47      protected static HostAddress decode( DERSequence sequence )
48      {
49          HostAddrType type = HostAddrType.ADDRTYPE_INET;
50          byte[] value = null;
51  
52          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
53          {
54              DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
55              int tag = object.getTagNo();
56              DEREncodable derObject = object.getObject();
57  
58              switch ( tag )
59              {
60                  case 0:
61                      DERInteger addressType = ( DERInteger ) derObject;
62                      type = HostAddrType.getTypeByOrdinal( addressType.intValue() );
63                      break;
64                      
65                  case 1:
66                      DEROctetString address = ( DEROctetString ) derObject;
67                      value = address.getOctets();
68                      break;
69              }
70          }
71  
72          return new HostAddress( type, value );
73      }
74  
75  
76      /**
77       * HostAddresses ::=   SEQUENCE OF SEQUENCE {
78       *                     addr-type[0]             INTEGER,
79       *                     address[1]               OCTET STRING
80       * }
81       */
82      protected static HostAddresses decodeSequence( DERSequence sequence )
83      {
84          HostAddress[] addresses = new HostAddress[sequence.size()];
85  
86          int ii = 0;
87          
88          for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
89          {
90              DERSequence object = ( DERSequence ) e.nextElement();
91              HostAddress address = decode( object );
92              addresses[ii] = address;
93              ii++;
94          }
95  
96          return new HostAddresses( addresses );
97      }
98  }