001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1.x509;
017:
018: import org.apache.geronimo.crypto.asn1.ASN1Choice;
019: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
020: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
021: import org.apache.geronimo.crypto.asn1.DERObject;
022: import org.apache.geronimo.crypto.asn1.DERBMPString;
023: import org.apache.geronimo.crypto.asn1.DERIA5String;
024: import org.apache.geronimo.crypto.asn1.DERUTF8String;
025: import org.apache.geronimo.crypto.asn1.DERVisibleString;
026: import org.apache.geronimo.crypto.asn1.DERString;
027:
028: /**
029: * <code>DisplayText</code> class, used in
030: * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
031: *
032: * <p>It stores a string in a chosen encoding.
033: * <pre>
034: * DisplayText ::= CHOICE {
035: * ia5String IA5String (SIZE (1..200)),
036: * visibleString VisibleString (SIZE (1..200)),
037: * bmpString BMPString (SIZE (1..200)),
038: * utf8String UTF8String (SIZE (1..200)) }
039: * </pre>
040: * @see PolicyQualifierInfo
041: * @see PolicyInformation
042: */
043: public class DisplayText extends ASN1Encodable implements ASN1Choice {
044: /**
045: * Constant corresponding to ia5String encoding.
046: *
047: */
048: public static final int CONTENT_TYPE_IA5STRING = 0;
049: /**
050: * Constant corresponding to bmpString encoding.
051: *
052: */
053: public static final int CONTENT_TYPE_BMPSTRING = 1;
054: /**
055: * Constant corresponding to utf8String encoding.
056: *
057: */
058: public static final int CONTENT_TYPE_UTF8STRING = 2;
059: /**
060: * Constant corresponding to visibleString encoding.
061: *
062: */
063: public static final int CONTENT_TYPE_VISIBLESTRING = 3;
064:
065: /**
066: * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
067: *
068: */
069: public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;
070:
071: int contentType;
072: DERString contents;
073:
074: /**
075: * Creates a new <code>DisplayText</code> instance.
076: *
077: * @param type the desired encoding type for the text.
078: * @param text the text to store. Strings longer than 200
079: * characters are truncated.
080: */
081: public DisplayText(int type, String text) {
082: if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
083: // RFC3280 limits these strings to 200 chars
084: // truncate the string
085: text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
086: }
087:
088: contentType = type;
089: switch (type) {
090: case CONTENT_TYPE_IA5STRING:
091: contents = (DERString) new DERIA5String(text);
092: break;
093: case CONTENT_TYPE_UTF8STRING:
094: contents = (DERString) new DERUTF8String(text);
095: break;
096: case CONTENT_TYPE_VISIBLESTRING:
097: contents = (DERString) new DERVisibleString(text);
098: break;
099: case CONTENT_TYPE_BMPSTRING:
100: contents = (DERString) new DERBMPString(text);
101: break;
102: default:
103: contents = (DERString) new DERUTF8String(text);
104: break;
105: }
106: }
107:
108: /**
109: * return true if the passed in String can be represented without
110: * loss as a UTF8String, false otherwise.
111: */
112: private boolean canBeUTF8(String str) {
113: for (int i = str.length() - 1; i >= 0; i--) {
114: if (str.charAt(i) > 0x00ff) {
115: return false;
116: }
117: }
118:
119: return true;
120: }
121:
122: /**
123: * Creates a new <code>DisplayText</code> instance.
124: *
125: * @param text the text to encapsulate. Strings longer than 200
126: * characters are truncated.
127: */
128: public DisplayText(String text) {
129: // by default use UTF8String
130: if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
131: text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
132: }
133:
134: if (canBeUTF8(text)) {
135: contentType = CONTENT_TYPE_UTF8STRING;
136: contents = new DERUTF8String(text);
137: } else {
138: contentType = CONTENT_TYPE_BMPSTRING;
139: contents = new DERBMPString(text);
140: }
141: }
142:
143: /**
144: * Creates a new <code>DisplayText</code> instance.
145: * <p>Useful when reading back a <code>DisplayText</code> class
146: * from it's ASN1Encodable/DEREncodable form.
147: *
148: * @param de a <code>DEREncodable</code> instance.
149: */
150: public DisplayText(DERString de) {
151: contents = de;
152: }
153:
154: public static DisplayText getInstance(Object de) {
155: if (de instanceof DERString) {
156: return new DisplayText((DERString) de);
157: } else if (de instanceof DisplayText) {
158: return (DisplayText) de;
159: }
160:
161: throw new IllegalArgumentException(
162: "illegal object in getInstance");
163: }
164:
165: public static DisplayText getInstance(ASN1TaggedObject obj,
166: boolean explicit) {
167: return getInstance(obj.getObject()); // must be explicitly tagged
168: }
169:
170: public DERObject toASN1Object() {
171: return (DERObject) contents;
172: }
173:
174: /**
175: * Returns the stored <code>String</code> object.
176: *
177: * @return the stored text as a <code>String</code>.
178: */
179: public String getString() {
180: return contents.getString();
181: }
182: }
|