Example usage for org.apache.commons.codec.net URLCodec decodeUrl

List of usage examples for org.apache.commons.codec.net URLCodec decodeUrl

Introduction

In this page you can find the example usage for org.apache.commons.codec.net URLCodec decodeUrl.

Prototype

public static final byte[] decodeUrl(byte[] bytes) throws DecoderException 

Source Link

Document

Decodes an array of URL safe 7-bit characters into an array of original bytes.

Usage

From source file:eu.esdihumboldt.cst.doc.functions.internal.context.ONameUtil.java

/**
 * Decode a name to its original string representation.
 * /*from   ww  w .  j  ava  2  s. c  o  m*/
 * @param name the name to decode
 * @return the decoded text
 * @throws DecoderException if decoding the string fails
 */
public static String decodeName(String name) throws DecoderException {
    if (name == null) {
        return null;
    }
    return new String(URLCodec.decodeUrl(name.replace('_', '%').getBytes(Charsets.US_ASCII)), Charsets.UTF_8);
}

From source file:com.autonomy.aci.client.transport.gss.GssEncryptionCodec.java

/**
 * This method firstly www-form-urlencoded unescapes the input string, as the response from a GSS-API secured ACI
 * server www-form-urlencoded escapes the Base64 encoded content. Once that's done it passes the resulting string
 * onto the super class for the Base64 decoding and length prefix stripping.
 * @param bytes The Base64 encoded byte array to decode.
 * @return The decoded byte array.// www  . j  a v  a  2  s .c o  m
 * @throws EncryptionCodecException If an error occurred during processing
 */
@Override
protected byte[] decodeInternal(final byte[] bytes) throws EncryptionCodecException {
    try {
        // We're not using AciURLCodec as it works on Strings, it uses URLCodec internally anyway, so it's always 
        // going to be shipped, thus we may as well use it's byte[] methods...
        return super.decodeInternal(URLCodec.decodeUrl(bytes));
    } catch (final DecoderException de) {
        throw new EncryptionCodecException("Unable to www-form-urlencoded unescape.", de);
    }
}

From source file:com.gargoylesoftware.htmlunit.protocol.data.DataUrlDecoder.java

/**
 * 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/*  ww  w. j  a  va2 s  .co m*/
 * @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(',');
    final String beforeData = url.substring("data:".length(), comma);
    final String mediaType = extractMediaType(beforeData);
    final String charset = extractCharset(beforeData);

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

    return new DataUrlDecoder(data, mediaType, charset);
}

From source file:com.autonomy.aci.client.util.AciURLCodec.java

/**
 * Decodes a URL safe string into its original form using the UTF-8 charset. Escaped characters are converted back
 * to their original representation./* ww  w  .j a  v  a  2  s. co  m*/
 * @param string URL safe string to convert into its original form
 * @return original string
 * @throws AciURLCodecException Thrown if URL decoding is unsuccessful
 */
public String decode(final String string) throws AciURLCodecException {
    // This is what we'll return...
    String returnValue = null;

    try {
        if (string != null) {
            returnValue = new String(URLCodec.decodeUrl(string.getBytes(US_ASCII)), UTF8);
        }
    } catch (final UnsupportedEncodingException | DecoderException uee) {
        // This should never ever happen as both charsets are required by the Java Spec.
        throw new AciURLCodecException(uee);
    }

    // Return the result...
    return returnValue;
}

From source file:com.jfrog.bintray.client.impl.util.URIUtil.java

/**
 * Unescape and decode a given string regarded as an escaped string with the
 * default protocol charset.//from  w w w  .  j a  v  a2  s  .  co m
 *
 * @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());
    }
}

From source file:com.eucalyptus.auth.login.Hmacv1LoginModule.java

private String makeSubjectString(final Map<String, List<String>> parameters)
        throws UnsupportedEncodingException {
    String paramString = "";
    Set<String> sortedKeys = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
    sortedKeys.addAll(parameters.keySet());
    sortedKeys.remove(SecurityParameter.Signature.parameter());
    for (final String key : sortedKeys) {
        if (parameters.get(key).isEmpty()) {
            paramString = paramString.concat(key).replaceAll("\\+", " ");
        } else/*from   w  w w . j a  va2 s .  com*/
            for (final String value : Ordering.natural().sortedCopy(parameters.get(key))) {
                paramString = paramString.concat(key).concat(Strings.nullToEmpty(value).replaceAll("\\+", " "));
            }
    }
    try {
        return new String(URLCodec.decodeUrl(paramString.getBytes()));
    } catch (DecoderException e) {
        return paramString;
    }
}

From source file:com.crispico.flower.mp.model.codesync.impl.FeatureChangeImpl.java

private byte[] decode(byte[] bytes) {
    try {//from   www .  j av  a2s  . co  m
        return URLCodec.decodeUrl(bytes);
    } catch (DecoderException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java

/**
 * Unescapes and decodes the specified string.
 *
 * @param escaped the string to be unescaped and decoded
 * @return the unescaped and decoded string
 *//*from w ww. j a  va 2s . com*/
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 UnsupportedEncodingException e) {
        // Should never happen.
        throw new RuntimeException(e);
    } catch (final DecoderException e) {
        // Should never happen.
        throw new RuntimeException(e);
    }
}

From source file:com.moz.fiji.schema.tools.ToolUtils.java

/**
 * Parses a command-line flag specifying a byte array.
 *
 * Valid specifications are:/*  w  w w.  jav  a 2  s .c o  m*/
 * <ul>
 *   <li> UTF-8 encoded strings, as in "utf8:encoded \x00 text".</li>
 *   <li> Hexadecimal sequence, with "hex:00dead88beefaa".</li>
 *   <li> URL encoded strings, as in "url:this%20is%20a%20URL".</li>
 * </ul>
 *
 * UTF-8 is the default, hence the "utf8:" prefix is optional unless there is an ambiguity with
 * other prefixes.
 *
 * @param flag Command-line flag specification for a byte array.
 * @return the decoded byte array.
 * @throws IOException on I/O error.
 */
public static byte[] parseBytesFlag(String flag) throws IOException {
    if (flag.startsWith(BYTES_SPEC_PREFIX_HEX)) {
        // Hexadecimal encoded byte array:
        return ByteArrayFormatter.parseHex(flag.substring(BYTES_SPEC_PREFIX_HEX.length()));

    } else if (flag.startsWith(BYTES_SPEC_PREFIX_URL)) {
        // URL encoded byte array:
        try {
            return URLCodec.decodeUrl(Bytes.toBytes(flag));
        } catch (DecoderException de) {
            throw new IOException(de);
        }

    } else {
        // UTF-8 encoded and escaped byte array:
        final String spec = flag.startsWith(BYTES_SPEC_PREFIX_UTF8)
                ? flag.substring(BYTES_SPEC_PREFIX_UTF8.length())
                : flag;
        return Bytes.toBytes(spec);
    }
}

From source file:com.alexa.stock.utils.URIUtils.java

/**
 * Unescape and decode a given string regarded as an escaped string with the
 * default protocol charset.//from w ww  .ja  v a  2s .c  om
 *
 * @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(EncodingUtils.getAsciiBytes(escaped));
        return EncodingUtils.getString(rawdata, "UTF-8");
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
}