Changeset 12244 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2017-05-24T01:56:11+02:00 (7 years ago)
Author:
Don-vip
Message:

see #11924 - simplify DNSName copy by using reflection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/DNSName.java

    r11809 r12244  
    2626
    2727import java.io.IOException;
    28 import java.util.Locale;
     28import java.lang.reflect.Field;
    2929
    30 import sun.security.util.DerOutputStream;
    31 import sun.security.x509.GeneralNameInterface;
     30import org.openstreetmap.josm.tools.Utils;
    3231
    3332/**
     
    4645 * be encoded as rfc822Name.
    4746 *
    48  * This class has been copied from OpenJDK7u repository and modified
     47 * This class has been copied from OpenJDK8u repository and modified
    4948 * in order to fix Java bug 8016345:
    5049 * https://bugs.openjdk.java.net/browse/JDK-8016345
    5150 *
    52  * It can be deleted after a migration to a Java release fixing this bug.
     51 * It can be deleted after a migration to a Java release fixing this bug:
     52 * https://bugs.openjdk.java.net/browse/JDK-8054380
    5353 * <p>
    5454 * @author Amit Kapoor
     
    5757 * @since 7347
    5858 */
    59 public final class DNSName implements GeneralNameInterface {
    60     private final String name;
     59public final class DNSName extends sun.security.x509.DNSName {
    6160
    6261    private static final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     
    7170     */
    7271    public DNSName(String name) throws IOException {
     72        super("fake");
    7373        if (name == null || name.isEmpty())
    7474            throw new IOException("DNS name must not be null");
     
    9595            }
    9696        }
    97         this.name = name;
    98     }
    99 
    100     /**
    101      * Return the type of the GeneralName.
    102      * @return the type of the GeneralName
    103      */
    104     @Override
    105     public int getType() {
    106         return GeneralNameInterface.NAME_DNS;
    107     }
    108 
    109     /**
    110      * Return the actual name value of the GeneralName.
    111      * @return the actual name value of the GeneralName
    112      */
    113     public String getName() {
    114         return name;
    115     }
    116 
    117     /**
    118      * Encode the DNS name into the DerOutputStream.
    119      *
    120      * @param out the DER stream to encode the DNSName to.
    121      * @throws IOException on encoding errors.
    122      */
    123     @Override
    124     public void encode(DerOutputStream out) throws IOException {
    125         out.putIA5String(name);
    126     }
    127 
    128     /**
    129      * Convert the name into user readable string.
    130      */
    131     @Override
    132     public String toString() {
    133         return "DNSName: " + name;
    134     }
    135 
    136     /**
    137      * Compares this name with another, for equality.
    138      *
    139      * @return true iff the names are equivalent
    140      * according to RFC2459.
    141      */
    142     @Override
    143     public boolean equals(Object obj) {
    144         if (this == obj)
    145             return true;
    146 
    147         if (!(obj instanceof DNSName))
    148             return false;
    149 
    150         DNSName other = (DNSName) obj;
    151 
    152         // RFC2459 mandates that these names are
    153         // not case-sensitive
    154         return name.equalsIgnoreCase(other.name);
    155     }
    156 
    157     /**
    158      * Returns the hash code value for this object.
    159      *
    160      * @return a hash code value for this object.
    161      */
    162     @Override
    163     public int hashCode() {
    164         return name.toUpperCase(Locale.ENGLISH).hashCode();
    165     }
    166 
    167     /**
    168      * Return type of constraint inputName places on this name:<ul>
    169      *   <li>NAME_DIFF_TYPE = -1: input name is different type from name (i.e. does not constrain).
    170      *   <li>NAME_MATCH = 0: input name matches name.
    171      *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming subtree)
    172      *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming subtree)
    173      *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but is same type.
    174      * </ul>.  These results are used in checking NameConstraints during
    175      * certification path verification.
    176      * <p>
    177      * RFC2459: DNS name restrictions are expressed as foo.bar.com. Any subdomain
    178      * satisfies the name constraint. For example, www.foo.bar.com would
    179      * satisfy the constraint but bigfoo.bar.com would not.
    180      * <p>
    181      * draft-ietf-pkix-new-part1-00.txt:  DNS name restrictions are expressed as foo.bar.com.
    182      * Any DNS name that
    183      * can be constructed by simply adding to the left hand side of the name
    184      * satisfies the name constraint. For example, www.foo.bar.com would
    185      * satisfy the constraint but foo1.bar.com would not.
    186      * <p>
    187      * RFC1034: By convention, domain names can be stored with arbitrary case, but
    188      * domain name comparisons for all present domain functions are done in a
    189      * case-insensitive manner, assuming an ASCII character set, and a high
    190      * order zero bit.
    191      * <p>
    192      * @param inputName to be checked for being constrained
    193      * @return constraint type above
    194      * @throws UnsupportedOperationException if name is not exact match, but narrowing and widening are
    195      *          not supported for this name type.
    196      */
    197     @Override
    198     public int constrains(GeneralNameInterface inputName) {
    199         int constraintType;
    200         if (inputName == null)
    201             constraintType = NAME_DIFF_TYPE;
    202         else if (inputName.getType() != NAME_DNS)
    203             constraintType = NAME_DIFF_TYPE;
    204         else {
    205             if (!(inputName instanceof DNSName)) {
    206                 throw new IllegalArgumentException("inputName: " + inputName);
    207             }
    208             String inName =
    209                 (((DNSName) inputName).getName()).toLowerCase(Locale.ENGLISH);
    210             String thisName = name.toLowerCase(Locale.ENGLISH);
    211             if (inName.equals(thisName))
    212                 constraintType = NAME_MATCH;
    213             else if (thisName.endsWith(inName)) {
    214                 int inNdx = thisName.lastIndexOf(inName);
    215                 if (thisName.charAt(inNdx-1) == '.')
    216                     constraintType = NAME_WIDENS;
    217                 else
    218                     constraintType = NAME_SAME_TYPE;
    219             } else if (inName.endsWith(thisName)) {
    220                 int ndx = inName.lastIndexOf(thisName);
    221                 if (inName.charAt(ndx-1) == '.')
    222                     constraintType = NAME_NARROWS;
    223                 else
    224                     constraintType = NAME_SAME_TYPE;
    225             } else {
    226                 constraintType = NAME_SAME_TYPE;
    227             }
     97        try {
     98            Field fName = getClass().getSuperclass().getDeclaredField("name");
     99            Utils.setObjectsAccessible(fName);
     100            fName.set(this, name);
     101        } catch (ReflectiveOperationException | SecurityException e) {
     102            throw new IOException(e);
    228103        }
    229         return constraintType;
    230     }
    231 
    232     /**
    233      * Return subtree depth of this name for purposes of determining
    234      * NameConstraints minimum and maximum bounds and for calculating
    235      * path lengths in name subtrees.
    236      *
    237      * @return distance of name from root
    238      * @throws UnsupportedOperationException if not supported for this name type
    239      */
    240     @Override
    241     public int subtreeDepth() {
    242         String subtree = name;
    243         int i = 1;
    244 
    245         /* count dots */
    246         for (; subtree.lastIndexOf('.') >= 0; i++) {
    247             subtree = subtree.substring(0, subtree.lastIndexOf('.'));
    248         }
    249 
    250         return i;
    251104    }
    252105}
Note: See TracChangeset for help on using the changeset viewer.