Package org.apache.geronimo.util.asn1.x509

Source Code of org.apache.geronimo.util.asn1.x509.DisplayText

/**
*
* Copyright 2003-2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/


package org.apache.geronimo.util.asn1.x509;

import org.apache.geronimo.util.asn1.ASN1Choice;
import org.apache.geronimo.util.asn1.ASN1Encodable;
import org.apache.geronimo.util.asn1.ASN1TaggedObject;
import org.apache.geronimo.util.asn1.DERObject;
import org.apache.geronimo.util.asn1.DERBMPString;
import org.apache.geronimo.util.asn1.DERIA5String;
import org.apache.geronimo.util.asn1.DERUTF8String;
import org.apache.geronimo.util.asn1.DERVisibleString;
import org.apache.geronimo.util.asn1.DERString;

/**
* <code>DisplayText</code> class, used in
* <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
*
* <p>It stores a string in a chosen encoding.
* <pre>
* DisplayText ::= CHOICE {
*      ia5String        IA5String      (SIZE (1..200)),
*      visibleString    VisibleString  (SIZE (1..200)),
*      bmpString        BMPString      (SIZE (1..200)),
*      utf8String       UTF8String     (SIZE (1..200)) }
* </pre>
* @see PolicyQualifierInfo
* @see PolicyInformation
*/
public class DisplayText
    extends ASN1Encodable
    implements ASN1Choice
{
   /**
    * Constant corresponding to ia5String encoding.
    *
    */
   public static final int CONTENT_TYPE_IA5STRING = 0;
   /**
    * Constant corresponding to bmpString encoding.
    *
    */
   public static final int CONTENT_TYPE_BMPSTRING = 1;
   /**
    * Constant corresponding to utf8String encoding.
    *
    */
   public static final int CONTENT_TYPE_UTF8STRING = 2;
   /**
    * Constant corresponding to visibleString encoding.
    *
    */
   public static final int CONTENT_TYPE_VISIBLESTRING = 3;

   /**
    * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
    *
    */
   public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;

   int contentType;
   DERString contents;

   /**
    * Creates a new <code>DisplayText</code> instance.
    *
    * @param type the desired encoding type for the text.
    * @param text the text to store. Strings longer than 200
    * characters are truncated.
    */
   public DisplayText (int type, String text)
   {
      if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
         // RFC3280 limits these strings to 200 chars
         // truncate the string
         text = text.substring (0, DISPLAY_TEXT_MAXIMUM_SIZE);
      }

      contentType = type;
      switch (type) {
         case CONTENT_TYPE_IA5STRING:
            contents = (DERString)new DERIA5String (text);
            break;
         case CONTENT_TYPE_UTF8STRING:
            contents = (DERString)new DERUTF8String(text);
            break;
         case CONTENT_TYPE_VISIBLESTRING:
            contents = (DERString)new DERVisibleString(text);
            break;
         case CONTENT_TYPE_BMPSTRING:
            contents = (DERString)new DERBMPString(text);
            break;
         default:
            contents = (DERString)new DERUTF8String(text);
            break;
      }
   }

   /**
    * return true if the passed in String can be represented without
    * loss as a UTF8String, false otherwise.
    */
   private boolean canBeUTF8(
       String  str)
   {
       for (int i = str.length() - 1; i >= 0; i--)
       {
           if (str.charAt(i) > 0x00ff)
           {
               return false;
           }
       }

       return true;
   }

   /**
    * Creates a new <code>DisplayText</code> instance.
    *
    * @param text the text to encapsulate. Strings longer than 200
    * characters are truncated.
    */
   public DisplayText (String text)
   {
      // by default use UTF8String
      if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
         text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
      }

      if (canBeUTF8(text))
      {
          contentType = CONTENT_TYPE_UTF8STRING;
          contents = new DERUTF8String(text);
      }
      else
      {
          contentType = CONTENT_TYPE_BMPSTRING;
          contents = new DERBMPString(text);
      }
   }

   /**
    * Creates a new <code>DisplayText</code> instance.
    * <p>Useful when reading back a <code>DisplayText</code> class
    * from it's ASN1Encodable/DEREncodable form.
    *
    * @param de a <code>DEREncodable</code> instance.
    */
   public DisplayText(DERString de)
   {
      contents = de;
   }

   public static DisplayText getInstance(Object de)
   {
      if (de instanceof DERString)
      {
          return new DisplayText((DERString)de);
      }
      else if (de instanceof DisplayText)
      {
          return (DisplayText)de;
      }

      throw new IllegalArgumentException("illegal object in getInstance");
   }

   public static DisplayText getInstance(
       ASN1TaggedObject obj,
       boolean          explicit)
   {
       return getInstance(obj.getObject()); // must be explicitly tagged
   }

   public DERObject toASN1Object()
   {
      return (DERObject)contents;
   }

   /**
    * Returns the stored <code>String</code> object.
    *
    * @return the stored text as a <code>String</code>.
    */
   public String getString()
   {
      return contents.getString();
   }
}
TOP

Related Classes of org.apache.geronimo.util.asn1.x509.DisplayText

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.