mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

jvergara
21.31.2007 a091e85ec925fdb878303144f7b4f5e302e7c35f
opends/src/guitools/org/opends/guitools/statuspanel/BaseDNDescriptor.java
@@ -27,7 +27,12 @@
package org.opends.guitools.statuspanel;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.opends.quicksetup.util.Utils;
import org.opends.server.util.StaticUtils;
/**
 * This class is used to represent a Base DN / Replica and is aimed to be
@@ -56,6 +61,10 @@
  private int ageOfOldestMissingChange;
  private Type type;
  private String baseDn;
  private String unescapedDn;
  private static final Logger LOG =
    Logger.getLogger(BaseDNDescriptor.class.getName());
  /**
   * Constructor for this class.
@@ -73,6 +82,15 @@
    this.type = type;
    this.ageOfOldestMissingChange = ageOfOldestMissingChange;
    this.missingChanges = missingChanges;
    try
    {
      this.unescapedDn = unescapeUtf8(baseDn);
    }
    catch (Throwable t)
    {
      this.unescapedDn = baseDn;
      LOG.log(Level.WARNING, "Error unescaping dn: "+baseDn, t);
    }
  }
  /**
@@ -85,6 +103,17 @@
  }
  /**
   * Return the String DN associated with the base DN with unescaped UTF-8
   * characters.
   * @return the String DN associated with the base DN with unescaped UTF-8
   * characters.
   */
  public String getUnescapedDn()
  {
    return unescapedDn;
  }
  /**
   * {@inheritDoc}
   */
  public boolean equals(Object v)
@@ -191,4 +220,148 @@
  {
    this.db = db;
  }
  private String unescapeUtf8(String v) throws UnsupportedEncodingException
  {
    byte[] stringBytes = v.getBytes("UTF-8");
    byte[] decodedBytes = new byte[stringBytes.length];
    int pos = 0;
    for (int i = 0; i < stringBytes.length; i++)
    {
      if ((stringBytes[i] == '\\') && (i + 2 < stringBytes.length) &&
          StaticUtils.isHexDigit(stringBytes[i+1]) &&
          StaticUtils.isHexDigit(stringBytes[i+2]))
      {
        // Convert hex-encoded UTF-8 to 16-bit chars.
        byte b;
        byte escapedByte1 = stringBytes[++i];
        switch (escapedByte1)
        {
          case '0':
            b = (byte) 0x00;
            break;
          case '1':
            b = (byte) 0x10;
            break;
          case '2':
            b = (byte) 0x20;
            break;
          case '3':
            b = (byte) 0x30;
            break;
          case '4':
            b = (byte) 0x40;
            break;
          case '5':
            b = (byte) 0x50;
            break;
          case '6':
            b = (byte) 0x60;
            break;
          case '7':
            b = (byte) 0x70;
            break;
          case '8':
            b = (byte) 0x80;
            break;
          case '9':
            b = (byte) 0x90;
            break;
          case 'a':
          case 'A':
            b = (byte) 0xA0;
            break;
          case 'b':
          case 'B':
            b = (byte) 0xB0;
            break;
          case 'c':
          case 'C':
            b = (byte) 0xC0;
            break;
          case 'd':
          case 'D':
            b = (byte) 0xD0;
            break;
          case 'e':
          case 'E':
            b = (byte) 0xE0;
            break;
          case 'f':
          case 'F':
            b = (byte) 0xF0;
            break;
          default:
            throw new IllegalStateException("Unexpected byte: "+escapedByte1);
        }
        byte escapedByte2 = stringBytes[++i];
        switch (escapedByte2)
        {
          case '0':
            break;
          case '1':
            b |= 0x01;
            break;
          case '2':
            b |= 0x02;
            break;
          case '3':
            b |= 0x03;
            break;
          case '4':
            b |= 0x04;
            break;
          case '5':
            b |= 0x05;
            break;
          case '6':
            b |= 0x06;
            break;
          case '7':
            b |= 0x07;
            break;
          case '8':
            b |= 0x08;
            break;
          case '9':
            b |= 0x09;
            break;
          case 'a':
          case 'A':
            b |= 0x0A;
            break;
          case 'b':
          case 'B':
            b |= 0x0B;
            break;
          case 'c':
          case 'C':
            b |= 0x0C;
            break;
          case 'd':
          case 'D':
            b |= 0x0D;
            break;
          case 'e':
          case 'E':
            b |= 0x0E;
            break;
          case 'f':
          case 'F':
            b |= 0x0F;
            break;
          default:
            throw new IllegalStateException("Unexpected byte: "+escapedByte2);
        }
        decodedBytes[pos++] = b;
      }
      else {
        decodedBytes[pos++] = stringBytes[i];
      }
    }
    return new String(decodedBytes, 0, pos, "UTF-8");
  }
}