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.io.IOException;
22  
23  import org.apache.geronimo.util.asn1.DERBMPString;
24  import org.apache.geronimo.util.asn1.DERIA5String;
25  import org.apache.geronimo.util.asn1.DERObject;
26  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
27  import org.apache.geronimo.util.asn1.DERPrintableString;
28  import org.apache.geronimo.util.asn1.DERUTF8String;
29  
30  /**
31   * The default converter for X509 DN entries when going from their
32   * string value to
33   */
34  public class X509DefaultEntryConverter
35      extends X509NameEntryConverter
36  {
37      /**
38       * Apply default coversion for the given value depending on the oid
39       * and the character range of the value.
40       *
41       * @param oid the object identifier for the DN entry
42       * @param value the value associated with it
43       * @return the ASN.1 equivalent for the string value.
44       */
45      public DERObject getConvertedValue(
46          DERObjectIdentifier  oid,
47          String               value)
48      {
49          if (value.length() != 0 && value.charAt(0) == '#')
50          {
51              try
52              {
53                  return convertHexEncoded(value, 1);
54              }
55              catch (IOException e)
56              {
57                  throw new RuntimeException("can't recode value for oid " + oid.getId());
58              }
59          }
60          else if (oid.equals(X509Name.EmailAddress))
61          {
62              return new DERIA5String(value);
63          }
64          else if (canBePrintable(value))
65          {
66              return new DERPrintableString(value);
67          }
68          else if (canBeUTF8(value))
69          {
70              return new DERUTF8String(value);
71          }
72  
73          return new DERBMPString(value);
74      }
75  }