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  
20  package org.apache.geronimo.util.asn1.x509;
21  
22  import org.apache.geronimo.util.asn1.ASN1Choice;
23  import org.apache.geronimo.util.asn1.ASN1Encodable;
24  import org.apache.geronimo.util.asn1.ASN1TaggedObject;
25  import org.apache.geronimo.util.asn1.DERObject;
26  import org.apache.geronimo.util.asn1.DERBMPString;
27  import org.apache.geronimo.util.asn1.DERIA5String;
28  import org.apache.geronimo.util.asn1.DERUTF8String;
29  import org.apache.geronimo.util.asn1.DERVisibleString;
30  import org.apache.geronimo.util.asn1.DERString;
31  
32  /**
33   * <code>DisplayText</code> class, used in
34   * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
35   *
36   * <p>It stores a string in a chosen encoding.
37   * <pre>
38   * DisplayText ::= CHOICE {
39   *      ia5String        IA5String      (SIZE (1..200)),
40   *      visibleString    VisibleString  (SIZE (1..200)),
41   *      bmpString        BMPString      (SIZE (1..200)),
42   *      utf8String       UTF8String     (SIZE (1..200)) }
43   * </pre>
44   * @see PolicyQualifierInfo
45   * @see PolicyInformation
46   */
47  public class DisplayText
48      extends ASN1Encodable
49      implements ASN1Choice
50  {
51     /**
52      * Constant corresponding to ia5String encoding.
53      *
54      */
55     public static final int CONTENT_TYPE_IA5STRING = 0;
56     /**
57      * Constant corresponding to bmpString encoding.
58      *
59      */
60     public static final int CONTENT_TYPE_BMPSTRING = 1;
61     /**
62      * Constant corresponding to utf8String encoding.
63      *
64      */
65     public static final int CONTENT_TYPE_UTF8STRING = 2;
66     /**
67      * Constant corresponding to visibleString encoding.
68      *
69      */
70     public static final int CONTENT_TYPE_VISIBLESTRING = 3;
71  
72     /**
73      * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
74      *
75      */
76     public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;
77  
78     int contentType;
79     DERString contents;
80  
81     /**
82      * Creates a new <code>DisplayText</code> instance.
83      *
84      * @param type the desired encoding type for the text.
85      * @param text the text to store. Strings longer than 200
86      * characters are truncated.
87      */
88     public DisplayText (int type, String text)
89     {
90        if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
91           // RFC3280 limits these strings to 200 chars
92           // truncate the string
93           text = text.substring (0, DISPLAY_TEXT_MAXIMUM_SIZE);
94        }
95  
96        contentType = type;
97        switch (type) {
98           case CONTENT_TYPE_IA5STRING:
99              contents = (DERString)new DERIA5String (text);
100             break;
101          case CONTENT_TYPE_UTF8STRING:
102             contents = (DERString)new DERUTF8String(text);
103             break;
104          case CONTENT_TYPE_VISIBLESTRING:
105             contents = (DERString)new DERVisibleString(text);
106             break;
107          case CONTENT_TYPE_BMPSTRING:
108             contents = (DERString)new DERBMPString(text);
109             break;
110          default:
111             contents = (DERString)new DERUTF8String(text);
112             break;
113       }
114    }
115 
116    /**
117     * return true if the passed in String can be represented without
118     * loss as a UTF8String, false otherwise.
119     */
120    private boolean canBeUTF8(
121        String  str)
122    {
123        for (int i = str.length() - 1; i >= 0; i--)
124        {
125            if (str.charAt(i) > 0x00ff)
126            {
127                return false;
128            }
129        }
130 
131        return true;
132    }
133 
134    /**
135     * Creates a new <code>DisplayText</code> instance.
136     *
137     * @param text the text to encapsulate. Strings longer than 200
138     * characters are truncated.
139     */
140    public DisplayText (String text)
141    {
142       // by default use UTF8String
143       if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
144          text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
145       }
146 
147       if (canBeUTF8(text))
148       {
149           contentType = CONTENT_TYPE_UTF8STRING;
150           contents = new DERUTF8String(text);
151       }
152       else
153       {
154           contentType = CONTENT_TYPE_BMPSTRING;
155           contents = new DERBMPString(text);
156       }
157    }
158 
159    /**
160     * Creates a new <code>DisplayText</code> instance.
161     * <p>Useful when reading back a <code>DisplayText</code> class
162     * from it's ASN1Encodable/DEREncodable form.
163     *
164     * @param de a <code>DEREncodable</code> instance.
165     */
166    public DisplayText(DERString de)
167    {
168       contents = de;
169    }
170 
171    public static DisplayText getInstance(Object de)
172    {
173       if (de instanceof DERString)
174       {
175           return new DisplayText((DERString)de);
176       }
177       else if (de instanceof DisplayText)
178       {
179           return (DisplayText)de;
180       }
181 
182       throw new IllegalArgumentException("illegal object in getInstance");
183    }
184 
185    public static DisplayText getInstance(
186        ASN1TaggedObject obj,
187        boolean          explicit)
188    {
189        return getInstance(obj.getObject()); // must be explicitly tagged
190    }
191 
192    public DERObject toASN1Object()
193    {
194       return (DERObject)contents;
195    }
196 
197    /**
198     * Returns the stored <code>String</code> object.
199     *
200     * @return the stored text as a <code>String</code>.
201     */
202    public String getString()
203    {
204       return contents.getString();
205    }
206 }