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     */
017    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import java.io.IOException;
021    
022    import org.apache.geronimo.util.asn1.DERBMPString;
023    import org.apache.geronimo.util.asn1.DERIA5String;
024    import org.apache.geronimo.util.asn1.DERObject;
025    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
026    import org.apache.geronimo.util.asn1.DERPrintableString;
027    import org.apache.geronimo.util.asn1.DERUTF8String;
028    
029    /**
030     * The default converter for X509 DN entries when going from their
031     * string value to
032     */
033    public class X509DefaultEntryConverter
034        extends X509NameEntryConverter
035    {
036        /**
037         * Apply default coversion for the given value depending on the oid
038         * and the character range of the value.
039         *
040         * @param oid the object identifier for the DN entry
041         * @param value the value associated with it
042         * @return the ASN.1 equivalent for the string value.
043         */
044        public DERObject getConvertedValue(
045            DERObjectIdentifier  oid,
046            String               value)
047        {
048            if (value.length() != 0 && value.charAt(0) == '#')
049            {
050                try
051                {
052                    return convertHexEncoded(value, 1);
053                }
054                catch (IOException e)
055                {
056                    throw new RuntimeException("can't recode value for oid " + oid.getId(), e);
057                }
058            }
059            else if (oid.equals(X509Name.EmailAddress))
060            {
061                return new DERIA5String(value);
062            }
063            else if (canBePrintable(value))
064            {
065                return new DERPrintableString(value);
066            }
067            else if (canBeUTF8(value))
068            {
069                return new DERUTF8String(value);
070            }
071    
072            return new DERBMPString(value);
073        }
074    }