View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  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, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1.x509;
20  
21  import java.math.BigInteger;
22  
23  import org.apache.geronimo.util.asn1.ASN1Encodable;
24  import org.apache.geronimo.util.asn1.ASN1EncodableVector;
25  import org.apache.geronimo.util.asn1.ASN1Sequence;
26  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
27  import org.apache.geronimo.util.asn1.DERInteger;
28  import org.apache.geronimo.util.asn1.DERObject;
29  import org.apache.geronimo.util.asn1.DERSequence;
30  import org.apache.geronimo.util.asn1.DERTaggedObject;
31  
32  public class GeneralSubtree
33      extends ASN1Encodable
34  {
35      private GeneralName  base;
36      private DERInteger minimum;
37      private DERInteger maximum;
38  
39      public GeneralSubtree(
40          ASN1Sequence seq)
41      {
42          base = GeneralName.getInstance(seq.getObjectAt(0));
43  
44          switch (seq.size())
45          {
46          case 1:
47              break;
48          case 2:
49              ASN1TaggedObject o = (ASN1TaggedObject)seq.getObjectAt(1);
50              switch (o.getTagNo())
51              {
52              case 0 :
53                  minimum = DERInteger.getInstance(o, false);
54                  break;
55              case 1 :
56                  maximum = DERInteger.getInstance(o, false);
57                  break;
58              default:
59                  throw new IllegalArgumentException("Bad tag number: " + o.getTagNo());
60              }
61              break;
62          case 3 :
63              minimum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(1), false);
64              maximum = DERInteger.getInstance((ASN1TaggedObject)seq.getObjectAt(2), false);
65              break;
66          default:
67              throw new IllegalArgumentException("Bad sequence size: " + seq.size());
68          }
69      }
70  
71      public static GeneralSubtree getInstance(
72          ASN1TaggedObject    o,
73          boolean             explicit)
74      {
75          return new GeneralSubtree(ASN1Sequence.getInstance(o, explicit));
76      }
77  
78      public static GeneralSubtree getInstance(
79          Object obj)
80      {
81          if (obj == null)
82          {
83              return null;
84          }
85  
86          if (obj instanceof GeneralSubtree)
87          {
88              return (GeneralSubtree)obj;
89          }
90  
91          return new GeneralSubtree(ASN1Sequence.getInstance(obj));
92      }
93  
94      public GeneralName getBase()
95      {
96          return base;
97      }
98  
99      public BigInteger getMinimum()
100     {
101         if (minimum == null)
102         {
103             return BigInteger.valueOf(0);
104         }
105 
106         return minimum.getValue();
107     }
108 
109     public BigInteger getMaximum()
110     {
111         if (maximum == null)
112         {
113             return null;
114         }
115 
116         return maximum.getValue();
117     }
118 
119     /*
120      * GeneralSubtree ::= SEQUENCE {
121      *      base                    GeneralName,
122      *      minimum         [0]     BaseDistance DEFAULT 0,
123      *      maximum         [1]     BaseDistance OPTIONAL }
124      */
125     public DERObject toASN1Object()
126     {
127         ASN1EncodableVector v = new ASN1EncodableVector();
128 
129         v.add(base);
130 
131         if (minimum != null)
132         {
133             v.add(new DERTaggedObject(false, 0, minimum));
134         }
135 
136         if (maximum != null)
137         {
138             v.add(new DERTaggedObject(false, 1, maximum));
139         }
140 
141         return new DERSequence(v);
142     }
143 }