Java Code Examples for org.apache.commons.codec.net.URLCodec#decodeUrl()

The following examples show how to use org.apache.commons.codec.net.URLCodec#decodeUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataUrlDecoder.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a data URL providing simple access to the information contained by the URL.
 * @param url the string representation of the URL to decode
 * @return the {@link DataUrlDecoder} holding decoded information
 * @throws UnsupportedEncodingException if the encoding specified by the data URL is invalid or not
 * available on the JVM
 * @throws DecoderException if decoding didn't success
 */
public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedEncodingException,
        DecoderException {
    if (!url.startsWith("data")) {
        throw new IllegalArgumentException("Not a data url: " + url);
    }
    final int comma = url.indexOf(',');
    String beforeData = url.substring("data:".length(), comma);

    final boolean base64 = beforeData.endsWith(";base64");
    if (base64) {
        beforeData = beforeData.substring(0, beforeData.length() - 7);
    }
    final String mediaType = extractMediaType(beforeData);
    final Charset charset = extractCharset(beforeData);

    byte[] data = url.substring(comma + 1).getBytes(charset);
    if (base64) {
        data = Base64.decodeBase64(decodeUrl(data));
    }
    else {
        data = URLCodec.decodeUrl(data);
    }

    return new DataUrlDecoder(data, mediaType, charset);
}
 
Example 2
Source File: DataUrlDecoder.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes a data URL providing simple access to the information contained by the URL.
 * @param url the string representation of the URL to decode
 * @return the {@link DataUrlDecoder} holding decoded information
 * @throws UnsupportedEncodingException if the encoding specified by the data URL is invalid or not
 * available on the JVM
 * @throws DecoderException if decoding didn't success
 */
public static DataUrlDecoder decodeDataURL(final String url) throws UnsupportedEncodingException,
        DecoderException {
    if (!url.startsWith("data")) {
        throw new IllegalArgumentException("Not a data url: " + url);
    }
    final int comma = url.indexOf(',');
    String beforeData = url.substring("data:".length(), comma);

    final boolean base64 = beforeData.endsWith(";base64");
    if (base64) {
        beforeData = beforeData.substring(0, beforeData.length() - 7);
    }
    final String mediaType = extractMediaType(beforeData);
    final Charset charset = extractCharset(beforeData);

    byte[] data = url.substring(comma + 1).getBytes(charset);
    if (base64) {
        data = Base64.decodeBase64(decodeUrl(data));
    }
    else {
        data = URLCodec.decodeUrl(data);
    }

    return new DataUrlDecoder(data, mediaType, charset);
}
 
Example 3
Source File: UrlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Unescapes and decodes the specified string.
 *
 * @param escaped the string to be unescaped and decoded
 * @return the unescaped and decoded string
 */
public static String decode(final String escaped) {
    try {
        final byte[] bytes = escaped.getBytes(US_ASCII);
        final byte[] bytes2 = URLCodec.decodeUrl(bytes);
        return new String(bytes2, UTF_8);
    }
    catch (final DecoderException e) {
        // Should never happen.
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: UrlUtils.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Unescapes and decodes the specified string.
 *
 * @param escaped the string to be unescaped and decoded
 * @return the unescaped and decoded string
 */
public static String decode(final String escaped) {
    try {
        final byte[] bytes = escaped.getBytes(US_ASCII);
        final byte[] bytes2 = URLCodec.decodeUrl(bytes);
        return new String(bytes2, UTF_8);
    }
    catch (final DecoderException e) {
        // Should never happen.
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: TestURIBuilder.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void testDecodePlus() throws IOException, URISyntaxException, DecoderException {
    URI uri = new URI("https://host/encoded+plus");
    System.out.println(uri.getPath());
    System.out.println(URIUtil.decode(uri.getPath()));

    String decoded = new String(URLCodec.decodeUrl(uri.getPath().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    assertEquals(decoded, URIUtil.decode(uri.getPath()));
}
 
Example 6
Source File: TestURIBuilder.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
public void testDecodeSpecial() throws IOException, URISyntaxException, DecoderException {
    URI uri = new URI("https://host/@");
    System.out.println(uri.getPath());
    System.out.println(URIUtil.decode(uri.getPath()));

    String decoded = new String(URLCodec.decodeUrl(uri.getPath().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    assertEquals(decoded, URIUtil.decode(uri.getPath()));
}
 
Example 7
Source File: URIUtil.java    From bintray-client-java with Apache License 2.0 5 votes vote down vote up
/**
 * Unescape and decode a given string regarded as an escaped string with the
 * default protocol charset.
 *
 * @param escaped a string
 * @return the unescaped string
 * @throws HttpException if the string cannot be decoded (invalid)
 */
public static String decode(String escaped) throws HttpException {
    try {
        byte[] rawdata = URLCodec.decodeUrl(EncodingUtils.getAsciiBytes(escaped));
        return EncodingUtils.getString(rawdata, UTF8_CHARSET_NAME);
    } catch (DecoderException e) {
        throw new HttpException(e.getMessage());
    }
}
 
Example 8
Source File: URIUtil.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Unescape and decode a given string regarded as an escaped string with the
 * default protocol charset.
 *
 * @param escaped a string
 * @return the unescaped string
 * 
 * @throws URIException if the string cannot be decoded (invalid)
 * 
 * @see URI#getDefaultProtocolCharset
 */
public static String decode(String escaped) throws URIException {
    try {
        byte[] rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(escaped));
        return EncodingUtil.getString(rawdata, URI.getDefaultProtocolCharset());
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
}
 
Example 9
Source File: URI.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Decodes URI encoded string.
 *
 * This is a two mapping, one from URI characters to octets, and
 * subsequently a second from octets to original characters:
 * <p><blockquote><pre>
 *   URI character sequence->octet sequence->original character sequence
 * </pre></blockquote><p>
 *
 * A URI must be separated into its components before the escaped
 * characters within those components can be allowedly decoded.
 * <p>
 * Notice that there is a chance that URI characters that are non UTF-8
 * may be parsed as valid UTF-8.  A recent non-scientific analysis found
 * that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
 * 0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
 * false reading.
 * <p>
 * The percent "%" character always has the reserved purpose of being
 * the escape indicator, it must be escaped as "%25" in order to be used
 * as data within a URI.
 * <p>
 * The unescape method is internally performed within this method.
 *
 * @param component the URI character sequence
 * @param charset the protocol charset
 * @return original character sequence
 * @throws URIException incomplete trailing escape pattern or unsupported
 * character encoding
 * 
 * @since 3.0
 */
protected static String decode(String component, String charset) 
    throws URIException {
    if (component == null) {
        throw new IllegalArgumentException("Component array of chars may not be null");
    }
    byte[] rawdata = null;
    try { 
        rawdata = URLCodec.decodeUrl(EncodingUtil.getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
    return EncodingUtil.getString(rawdata, charset);
}
 
Example 10
Source File: CodecUtil.java    From common_gui_tools with Apache License 2.0 3 votes vote down vote up
/**
 * Decode URL.
 *
 * @param string  String
 * @param charSet CharSet
 * @return <code>String</code> string
 * @throws DecoderException             if decode error occurs
 * @throws UnsupportedEncodingException unsupported encoding exception
 */
public static String decodeURL(String string, String charSet) throws DecoderException, UnsupportedEncodingException {
    if (string == null) {
        return null;
    }
    return new String(URLCodec.decodeUrl(string.getBytes(charSet)), charSet);
}
 
Example 11
Source File: URI.java    From bintray-client-java with Apache License 2.0 3 votes vote down vote up
/**
 * Decodes URI encoded string.
 * <p/>
 * This is a two mapping, one from URI characters to octets, and
 * subsequently a second from octets to original characters:
 * <p><blockquote><pre>
 *   URI character sequence->octet sequence->original character sequence
 * </pre></blockquote><p>
 * <p/>
 * A URI must be separated into its components before the escaped
 * characters within those components can be allowedly decoded.
 * <p/>
 * Notice that there is a chance that URI characters that are non UTF-8
 * may be parsed as valid UTF-8.  A recent non-scientific analysis found
 * that EUC encoded Japanese words had a 2.7% false reading; SJIS had a
 * 0.0005% false reading; other encoding such as ASCII or KOI-8 have a 0%
 * false reading.
 * <p/>
 * The percent "%" character always has the reserved purpose of being
 * the escape indicator, it must be escaped as "%25" in order to be used
 * as data within a URI.
 * <p/>
 * The unescape method is internally performed within this method.
 *
 * @param component the URI character sequence
 * @param charset   the protocol charset
 * @return original character sequence
 * @throws HttpException incomplete trailing escape pattern or unsupported
 *                       character encoding
 * @since 3.0
 */
protected static String decode(String component, String charset)
        throws HttpException {
    if (component == null) {
        throw new IllegalArgumentException("Component array of chars may not be null");
    }
    byte[] rawdata = null;
    try {
        rawdata = URLCodec.decodeUrl(EncodingUtils.getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new HttpException(e.getMessage());
    }
    return EncodingUtils.getString(rawdata, charset);
}