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:org.hdiv.util.EncodingUtil.java

/**
 * Decodes Base64 alphabet characters of the string <code>s</code>, decrypts
 * this string and finally decompresses it.
 * //w w w. j a v a2 s.  c o  m
 * @param s data to decrypt
 * @return decoded data
 * @throws HDIVException if there is an error decoding object <code>data</code>
 * @see org.apache.commons.codec.binary.Base64#decode(byte[])
 * @see org.apache.commons.codec.net.URLCodec#decode(byte[])
 * @see java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream)
 */
public Object decode64Cipher(String s) {

    try {
        Base64 base64Codec = new Base64();
        // Decodes string s containing characters in the Base64 alphabet.
        byte[] encryptedData = base64Codec.decode(s.getBytes(ZIP_CHARSET));

        // Decodes an array of URL safe 7-bit characters into an array of
        // original bytes. Escaped characters are converted back to their
        // original representation.
        byte[] encodedData = URLCodec.decodeUrl(encryptedData);

        byte[] data = this.session.getDecryptCipher().decrypt(encodedData);

        ByteArrayInputStream decodedStream = new ByteArrayInputStream(data);
        InputStream unzippedStream = new GZIPInputStream(decodedStream);
        ObjectInputStream ois = new ObjectInputStream(unzippedStream);
        Object obj = ois.readObject();

        ois.close();
        unzippedStream.close();
        decodedStream.close();
        return obj;

    } catch (Exception e) {
        throw new HDIVException(HDIVErrorCodes.HDIV_PARAMETER_INCORRECT_VALUE);
    }
}

From source file:org.hdiv.util.EncodingUtil.java

/**
 * Decodes Base64 alphabet characters of the string <code>s</code> and
 * decompresses it.//from  www . j  a v  a2  s  .c  om
 * 
 * @param s data to decode
 * @return decoded <code>s</code> string
 * @throws HDIVException if there is an error decoding object <code>data</code>
 * @see org.apache.commons.codec.binary.Base64#decode(byte[])
 * @see org.apache.commons.codec.net.URLCodec#decode(byte[])
 * @see java.util.zip.GZIPInputStream#GZIPInputStream(java.io.InputStream)
 */
public Object decode64(String s) {

    try {
        Base64 base64Codec = new Base64();
        // Decodes string s containing characters in the Base64 alphabet.
        byte[] encryptedData = base64Codec.decode(s.getBytes(ZIP_CHARSET));

        // Decodes an array of URL safe 7-bit characters into an array of
        // original bytes. Escaped characters are converted back to their
        // original representation.
        byte[] encodedData = URLCodec.decodeUrl(encryptedData);

        ByteArrayInputStream decodedStream = new ByteArrayInputStream(encodedData);
        InputStream unzippedStream = new GZIPInputStream(decodedStream);
        ObjectInputStream ois = new ObjectInputStream(unzippedStream);
        Object obj = ois.readObject();

        ois.close();
        unzippedStream.close();
        decodedStream.close();
        return obj;

    } catch (Exception e) {
        throw new HDIVException(HDIVErrorCodes.HDIV_PARAMETER_INCORRECT_VALUE);
    }
}

From source file:org.openid4java.httpclient.URI.java

/**
 * Decodes URI encoded string.//from   w  ww. j a  va2  s.c  o m
 *
 * 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(getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new URIException(e.getMessage());
    }
    return getString(rawdata, charset);
}

From source file:org.openremote.foxycart.resources.FoxyCartResource.java

private byte[] decode(String data) {
    try {//from w  ww .ja  va 2s  .co m
        return URLCodec.decodeUrl(data.getBytes());
    } catch (DecoderException e) {
        String msg = "Invalid URL encoded data.";
        log.error(msg, e);
        Response resp = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("ERROR : " + msg).build();
        throw new WebApplicationException(resp);
    }
}

From source file:org.springframework.security.oauth.common.OAuthCodec.java

/**
 * Decode the specified value.//  www  .  j a v a 2s.  c o  m
 *
 * @param value The value to decode.
 * @return The decoded value.
 */
public static String oauthDecode(String value) throws DecoderException {
    if (value == null) {
        return "";
    }

    try {
        return new String(URLCodec.decodeUrl(value.getBytes("US-ASCII")), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.texai.torrent.Tracker.java

/** Handles the scrape request.
 *
 * @param parameterDictionary the parameter dictionary
 * @return the bencoded response//from  w  w  w.j  a v  a  2 s .  c om
 * @throws DecoderException when a hash cannot be URL-decoded
 */
@SuppressWarnings("unchecked")
public Map<byte[], Map<String, Object>> gatherFilesDictionary(
        final Map<String, List<String>> parameterDictionary) throws DecoderException {
    //Preconditions
    assert parameterDictionary != null : "parameterDictionary must not be null";

    final Map<byte[], Map<String, Object>> filesDictionary = new HashMap<>();
    final List<String> requestedURLEncodedInfoHashes = new ArrayList<>();
    final Object infoHashValue = parameterDictionary.get("info_hash");
    if (infoHashValue == null) {
        // when the optional parameter is absent, then reply with all tracked info hashes
        requestedURLEncodedInfoHashes.addAll(trackedPeerInfosDictionary.keySet());
    } else if (infoHashValue instanceof List<?>) {
        requestedURLEncodedInfoHashes.addAll((List<String>) infoHashValue);
    } else {
        assert infoHashValue instanceof String;
        requestedURLEncodedInfoHashes.add((String) infoHashValue);
    }
    LOGGER.warn("reviewing tracked peers for statistics");
    for (final String requestedURLEncodedInfoHash : requestedURLEncodedInfoHashes) {
        if (trackedPeerInfosDictionary.containsKey(requestedURLEncodedInfoHash)) {
            LOGGER.warn("  requested info hash: " + requestedURLEncodedInfoHash);
            final byte[] key;
            key = URLCodec.decodeUrl(requestedURLEncodedInfoHash.getBytes());
            final Map<String, Object> peersDictionary = new HashMap<>();

            // total number of times the tracker has registered a completion ("event=complete", i.e. a client finished downloading the torrent)
            final Integer nbrDownloaded = completionsDictionary.get(requestedURLEncodedInfoHash);
            LOGGER.warn("  nbrDownloaded: " + nbrDownloaded);
            if (nbrDownloaded == null) {
                peersDictionary.put("downloaded", 0);
            } else {
                peersDictionary.put("downloaded", nbrDownloaded);
            }

            int nbrLeechers = 0;
            int nbrSeeders = 0;
            synchronized (trackedPeerInfosDictionary) {
                final Map<TrackedPeerInfo, TrackedPeerStatus> trackedPeerInfosMap = trackedPeerInfosDictionary
                        .get(requestedURLEncodedInfoHash);
                for (final TrackedPeerStatus trackedPeerStatus : trackedPeerInfosMap.values()) {
                    LOGGER.warn("  trackedPeerInfo: " + trackedPeerStatus);
                    switch (trackedPeerStatus.event) {
                    case STARTED_EVENT:
                        nbrLeechers++;
                        break;
                    case COMPLETED_EVENT:
                        nbrSeeders++;
                        break;
                    default:
                        assert false;
                        break;
                    }
                }
            }
            // number of non-seeder peers, aka "leechers"
            peersDictionary.put("incomplete", nbrLeechers);

            // number of peers with the entire file, i.e. seeders
            peersDictionary.put("complete", nbrSeeders);

            // (optional) the torrent's internal name, as specified by the "name" file in the info section of the metainfo
            final MetaInfo metaInfo = metaInfoDictionary.get(requestedURLEncodedInfoHash);
            if (metaInfo != null) {
                assert metaInfo.getName() != null;
                peersDictionary.put("name", metaInfo.getName());
            }
            filesDictionary.put(key, peersDictionary);
        }
    }
    LOGGER.warn("filesDictionary: " + filesDictionary);
    return filesDictionary;
}

From source file:org.uberfire.java.nio.EncodingUtil.java

/**
 * Unescape and decode a given string regarded as an escaped string with the
 * UTF-8 protocol charset.//  ww  w.j a va  2s .co  m
 * @param escaped a string
 * @return the unescaped string
 * @throws IllegalStateException if the escaped string is not a correct URL
 */
public static String decode(String escaped) {
    byte[] asciiData = getAsciiBytes(escaped);
    byte[] rawdata;
    try {
        rawdata = URLCodec.decodeUrl(asciiData);
    } catch (DecoderException e) {
        throw new IllegalStateException(e.getMessage());
    }
    return getString(rawdata, "UTF-8");
}