¿Java equivalente al encodeURIComponent de JavaScript que produce una salida idéntica?

StackOverflow https://stackoverflow.com/questions/607176

Pregunta

He estado experimentando con varios bits de código Java tratando de encontrar algo que codifique una cadena que contenga comillas, espacios y " exótico " Los caracteres Unicode y producen una salida que es idéntica a la encodeURIComponent de JavaScript.

Mi cadena de prueba de tortura es: " A " B ± "

Si ingreso la siguiente declaración de JavaScript en Firebug:

encodeURIComponent('"A" B ± "');

—Entonces me sale:

"%22A%22%20B%20%C2%B1%20%22"

Aquí está mi pequeño programa de prueba de Java:

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class EncodingTest
{
  public static void main(String[] args) throws UnsupportedEncodingException
  {
    String s = "\"A\" B ± \"";
    System.out.println("URLEncoder.encode returns "
      + URLEncoder.encode(s, "UTF-8"));

    System.out.println("getBytes returns "
      + new String(s.getBytes("UTF-8"), "ISO-8859-1"));
  }
}

: este programa produce:

URLEncoder.encode returns %22A%22+B+%C2%B1+%22
getBytes returns "A" B ± "

Cerca, pero no cigarro! ¿Cuál es la mejor manera de codificar una cadena UTF-8 utilizando Java para que produzca la misma salida que encodeURIComponent de JavaScript?

EDITAR: Estoy usando Java 1.4 para cambiarme a Java 5 en breve.

¿Fue útil?

Solución

Viendo las diferencias de implementación, veo que:

MDC en encodeURIComponent () :

  • caracteres literales (representación de expresiones regulares): [-a-zA-Z0-9._*~'()!?

documentación de Java 1.5.0 en URLEncoder :

  • caracteres literales (representación de expresiones regulares): [-a-zA-Z0-9._*?
  • el carácter de espacio " " se convierte en un signo más " + " .

Básicamente, para obtener el resultado deseado, use URLEncoder.encode (s, " UTF-8 ") y luego realice un procesamiento posterior:

  • reemplace todas las apariciones de " + " con "%20"
  • reemplace todas las apariciones de "% xx " que representan cualquiera de [~ '()!] de nuevo a sus contrapartes literales

Otros consejos

Esta es la clase que se me ocurrió al final:

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Utility class for JavaScript compatible UTF-8 encoding and decoding.
 * 
 * @see http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output
 * @author John Topley 
 */
public class EncodingUtil
{
  /**
   * Decodes the passed UTF-8 String using an algorithm that's compatible with
   * JavaScript's <code>decodeURIComponent</code> function. Returns
   * <code>null</code> if the String is <code>null</code>.
   *
   * @param s The UTF-8 encoded String to be decoded
   * @return the decoded String
   */
  public static String decodeURIComponent(String s)
  {
    if (s == null)
    {
      return null;
    }

    String result = null;

    try
    {
      result = URLDecoder.decode(s, "UTF-8");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
      result = s;  
    }

    return result;
  }

  /**
   * Encodes the passed String as UTF-8 using an algorithm that's compatible
   * with JavaScript's <code>encodeURIComponent</code> function. Returns
   * <code>null</code> if the String is <code>null</code>.
   * 
   * @param s The String to be encoded
   * @return the encoded String
   */
  public static String encodeURIComponent(String s)
  {
    String result = null;

    try
    {
      result = URLEncoder.encode(s, "UTF-8")
                         .replaceAll("\\+", "%20")
                         .replaceAll("\\%21", "!")
                         .replaceAll("\\%27", "'")
                         .replaceAll("\\%28", "(")
                         .replaceAll("\\%29", ")")
                         .replaceAll("\\%7E", "~");
    }

    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
      result = s;
    }

    return result;
  }  

  /**
   * Private constructor to prevent this class from being instantiated.
   */
  private EncodingUtil()
  {
    super();
  }
}

Uso del motor de JavaScript que se incluye con Java 6:


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Wow
{
    public static void main(String[] args) throws Exception
    {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval("print(encodeURIComponent('\"A\" B ± \"'))");
    }
}

Salida:% 22A% 22% 20B% 20% c2% b1% 20% 22

El caso es diferente pero está más cerca de lo que quieres.

Uso java.net.URI # getRawPath () , por ejemplo

String s = "a+b c.html";
String fixed = new URI(null, null, s, null).getRawPath();

El valor de corregido será a + b% 20c.html , que es lo que quieres.

El procesamiento posterior de la salida de URLEncoder.encode () borrará cualquier ventaja que supuestamente esté en el URI. Por ejemplo

URLEncoder.encode("a+b c.html").replaceAll("\\+", "%20");

le dará a un% 20b% 20c.html , que se interpretará como a b c.html .

Se me ocurrió mi propia versión del encodeURIComponent, porque la solución publicada tiene un problema, si había un + presente en la Cadena, que debería codificarse, se convertirá en un espacio.

Así que aquí está mi clase:

import java.io.UnsupportedEncodingException;
import java.util.BitSet;

public final class EscapeUtils
{
    /** used for the encodeURIComponent function */
    private static final BitSet dontNeedEncoding;

    static
    {
        dontNeedEncoding = new BitSet(256);

        // a-z
        for (int i = 97; i <= 122; ++i)
        {
            dontNeedEncoding.set(i);
        }
        // A-Z
        for (int i = 65; i <= 90; ++i)
        {
            dontNeedEncoding.set(i);
        }
        // 0-9
        for (int i = 48; i <= 57; ++i)
        {
            dontNeedEncoding.set(i);
        }

        // '()*
        for (int i = 39; i <= 42; ++i)
        {
            dontNeedEncoding.set(i);
        }
        dontNeedEncoding.set(33); // !
        dontNeedEncoding.set(45); // -
        dontNeedEncoding.set(46); // .
        dontNeedEncoding.set(95); // _
        dontNeedEncoding.set(126); // ~
    }

    /**
     * A Utility class should not be instantiated.
     */
    private EscapeUtils()
    {

    }

    /**
     * Escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )
     * 
     * @param input
     *            A component of a URI
     * @return the escaped URI component
     */
    public static String encodeURIComponent(String input)
    {
        if (input == null)
        {
            return input;
        }

        StringBuilder filtered = new StringBuilder(input.length());
        char c;
        for (int i = 0; i < input.length(); ++i)
        {
            c = input.charAt(i);
            if (dontNeedEncoding.get(c))
            {
                filtered.append(c);
            }
            else
            {
                final byte[] b = charToBytesUTF(c);

                for (int j = 0; j < b.length; ++j)
                {
                    filtered.append('%');
                    filtered.append("0123456789ABCDEF".charAt(b[j] >> 4 & 0xF));
                    filtered.append("0123456789ABCDEF".charAt(b[j] & 0xF));
                }
            }
        }
        return filtered.toString();
    }

    private static byte[] charToBytesUTF(char c)
    {
        try
        {
            return new String(new char[] { c }).getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            return new byte[] { (byte) c };
        }
    }
}

Se me ocurrió otra implementación documentada en http: //blog.sangupta .com / 2010/05 / encodeuricomponent-and.html . La implementación también puede manejar bytes Unicode.

Este es un ejemplo sencillo de la solución de Ravi Wallau:

public String buildSafeURL(String partialURL, String documentName)
        throws ScriptException {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager
            .getEngineByName("JavaScript");

    String urlSafeDocumentName = String.valueOf(scriptEngine
            .eval("encodeURIComponent('" + documentName + "')"));
    String safeURL = partialURL + urlSafeDocumentName;

    return safeURL;
}

public static void main(String[] args) {
    EncodeURIComponentDemo demo = new EncodeURIComponentDemo();
    String partialURL = "https://www.website.com/document/";
    String documentName = "Tom & Jerry Manuscript.pdf";

    try {
        System.out.println(demo.buildSafeURL(partialURL, documentName));
    } catch (ScriptException se) {
        se.printStackTrace();
    }
}

Salida: https://www.website.com/document/Tom%20%26%20Jerry%20Manuscript.pdf

También responde la pregunta pendiente en los comentarios de Loren Shqipognja sobre cómo pasar una variable String a encodeURIComponent () . El método scriptEngine.eval () devuelve un Object , por lo que puede convertirse a String a través de String.valueOf () entre otros métodos.

Esto es lo que estoy usando:

private static final String HEX = "0123456789ABCDEF";

public static String encodeURIComponent(String str) {
    if (str == null) return null;

    byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
    StringBuilder builder = new StringBuilder(bytes.length);

    for (byte c : bytes) {
        if (c >= 'a' ? c <= 'z' || c == '~' :
            c >= 'A' ? c <= 'Z' || c == '_' :
            c >= '0' ? c <= '9' :  c == '-' || c == '.')
            builder.append((char)c);
        else
            builder.append('%')
                   .append(HEX.charAt(c >> 4 & 0xf))
                   .append(HEX.charAt(c & 0xf));
    }

    return builder.toString();
}

Va ??más allá de Javascript al codificar en porcentaje cada carácter que no sea un carácter sin reservas de acuerdo con RFC 3986 .


Esta es la conversión opuesta:

public static String decodeURIComponent(String str) {
    if (str == null) return null;

    int length = str.length();
    byte[] bytes = new byte[length / 3];
    StringBuilder builder = new StringBuilder(length);

    for (int i = 0; i < length; ) {
        char c = str.charAt(i);
        if (c != '%') {
            builder.append(c);
            i += 1;
        } else {
            int j = 0;
            do {
                char h = str.charAt(i + 1);
                char l = str.charAt(i + 2);
                i += 3;

                h -= '0';
                if (h >= 10) {
                    h |= ' ';
                    h -= 'a' - '0';
                    if (h >= 6) throw new IllegalArgumentException();
                    h += 10;
                }

                l -= '0';
                if (l >= 10) {
                    l |= ' ';
                    l -= 'a' - '0';
                    if (l >= 6) throw new IllegalArgumentException();
                    l += 10;
                }

                bytes[j++] = (byte)(h << 4 | l);
                if (i >= length) break;
                c = str.charAt(i);
            } while (c == '%');
            builder.append(new String(bytes, 0, j, UTF_8));
        }
    }

    return builder.toString();
}

He encontrado la clase PercentEscaper de la biblioteca google-http-java-client, que se puede usar para implementar encodeURIComponent con bastante facilidad.

PercentEscaper de google-http-java-client javadoc google-http-java-client home

He utilizado con éxito la clase java.net.URI así:

public static String uriEncode(String string) {
    String result = string;
    if (null != string) {
        try {
            String scheme = null;
            String ssp = string;
            int es = string.indexOf(':');
            if (es > 0) {
                scheme = string.substring(0, es);
                ssp = string.substring(es + 1);
            }
            result = (new URI(scheme, ssp, null)).toString();
        } catch (URISyntaxException usex) {
            // ignore and use string that has syntax error
        }
    }
    return result;
}

para mí esto funcionó:

import org.apache.http.client.utils.URIBuilder;

String encodedString = new URIBuilder()
  .setParameter("i", stringToEncode)
  .build()
  .getRawQuery() // output: i=encodedString
  .substring(2);

o con un UriBuilder diferente

import javax.ws.rs.core.UriBuilder;

String encodedString = UriBuilder.fromPath("")
  .queryParam("i", stringToEncode)
  .toString()   // output: ?i=encodedString
  .substring(3);

En mi opinión, usar una biblioteca estándar es una mejor idea en lugar de postprocesar manualmente. También la respuesta de @Chris se veía bien, pero no funciona para las URL, como " http: // a + b c.html "

La biblioteca de guayabas tiene PercentEscaper:

Escaper percentEscaper = new PercentEscaper (" -_. * " ;, false);

" -_. * " son caracteres seguros

false dice PercentEscaper para escapar del espacio con '% 20', no '+'

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top