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

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

Introduction

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

Prototype

public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) 

Source Link

Document

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

Usage

From source file:org.attribyte.util.URIEncoder.java

/**
 * Recodes a query string.//from  w  w w .  j a va2s  . co  m
 * @param qs The query string.
 * @return The recoded string or <tt>null</tt> if invalid.
 */
public static final String recodeQueryString(final String qs) {
    if (qs == null) {
        return null;
    }

    try {
        byte[] decoded = URLCodec.decodeUrl(qs.getBytes(Charsets.UTF_8));
        return new String(URLCodec.encodeUrl(queryBitSet, decoded), Charsets.US_ASCII);
    } catch (DecoderException de) {
        return null;
    }
}

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

/**
 * The object <code>obj</code> is compressed, encrypted and coded in Base64.
 * //from   w  w w.j  a v  a  2s .c  o m
 * @param obj Object to encrypt
 * @return Objet <code>obj</code> compressed, encrypted and coded in Base64
 * @throws HDIVException if there is an error encoding object <code>data</code>
 * @see java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream)
 * @see org.apache.commons.codec.net.URLCodec#encodeUrl(java.util.BitSet, byte[])
 * @see org.apache.commons.codec.binary.Base64#encode(byte[])
 */
public String encode64Cipher(Object obj) {

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream zos = new GZIPOutputStream(baos);
        ObjectOutputStream oos = new ObjectOutputStream(zos);
        oos.writeObject(obj);
        oos.close();
        zos.close();
        baos.close();

        byte[] cipherData = this.session.getEncryptCipher().encrypt(baos.toByteArray());

        // Encodes an array of bytes into an array of URL safe 7-bit characters.
        // Unsafe characters are escaped.
        byte[] encodedData = URLCodec.encodeUrl(null, cipherData);

        // Encodes a byte[] containing binary data, into a byte[] containing
        // characters in the Base64 alphabet.
        Base64 base64Codec = new Base64();
        return new String(base64Codec.encode(encodedData), ZIP_CHARSET);

    } catch (Exception e) {
        String errorMessage = HDIVUtil.getMessage("encode.message");
        throw new HDIVException(errorMessage, e);
    }
}

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

/**
 * The object <code>obj</code> is compressed and coded in Base64.
 * /*from   www .  j  a  v a  2s  . co  m*/
 * @param obj Object to encode
 * @return Object <code>obj</code> compressed y encoded in Base64
 * @throws HDIVException if there is an error encoding object <code>data</code>
 * @see java.util.zip.GZIPOutputStream#GZIPOutputStream(java.io.OutputStream)
 * @see org.apache.commons.codec.net.URLCodec#encodeUrl(java.util.BitSet, byte[])
 * @see org.apache.commons.codec.binary.Base64#encode(byte[])
 */
public String encode64(Object obj) {

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream zos = new GZIPOutputStream(baos);
        ObjectOutputStream oos = new ObjectOutputStream(zos);
        oos.writeObject(obj);
        oos.close();
        zos.close();
        baos.close();

        // Encodes an array of bytes into an array of URL safe 7-bit characters.
        // Unsafe characters are escaped.
        byte[] encodedData = URLCodec.encodeUrl(null, baos.toByteArray());

        // Encodes a byte[] containing binary data, into a byte[] containing
        // characters in the Base64 alphabet.
        Base64 base64Codec = new Base64();
        return new String(base64Codec.encode(encodedData), ZIP_CHARSET);

    } catch (Exception e) {
        String errorMessage = HDIVUtil.getMessage("encode.message");
        throw new HDIVException(errorMessage, e);
    }
}

From source file:org.jfrog.build.client.ArtifactoryHttpClient.java

public static String encodeUrl(String unescaped) {
    byte[] rawdata = URLCodec.encodeUrl(URI.allowed_query,
            org.apache.commons.codec.binary.StringUtils.getBytesUtf8(unescaped));
    return org.apache.commons.codec.binary.StringUtils.newStringUsAscii(rawdata);
}

From source file:org.mobicents.xcap.client.uri.encoding.UriComponentEncoder.java

private static byte[] encode(String s, BitSet allowed) throws NullPointerException {
    if (s == null) {
        throw new NullPointerException("string to encode is null");
    }//from   w  ww  .java2  s  .  c om
    if (allowed == null) {
        throw new NullPointerException("Allowed bitset may not be null");
    }
    return URLCodec.encodeUrl(allowed, s.getBytes(UTF8_CHARSET));
}

From source file:org.opencms.webdav.CmsWebdavServlet.java

/**
 * URL rewriter.<p>/*w  w w  . j a v a  2s. co  m*/
 *
 * @param path path which has to be rewritten
 * 
 * @return a string with the encoded path
 * 
 * @throws UnsupportedEncodingException if something goes wrong while encoding the url
 */
protected String rewriteUrl(String path) throws UnsupportedEncodingException {

    return new String(URLCodec.encodeUrl(URL_SAFE_CHARS, path.getBytes("ISO-8859-1")));
}

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

/**
 * Encodes URI string./*from  w  w w  .ja  va  2 s . c o  m*/
 *
 * This is a two mapping, one from original characters to octets, and
 * subsequently a second from octets to URI characters:
 * <p><blockquote><pre>
 *   original character sequence->octet sequence->URI character sequence
 * </pre></blockquote><p>
 *
 * An escaped octet is encoded as a character triplet, consisting of the
 * percent character "%" followed by the two hexadecimal digits
 * representing the octet code. For example, "%20" is the escaped
 * encoding for the US-ASCII space character.
 * <p>
 * Conversion from the local filesystem character set to UTF-8 will
 * normally involve a two step process. First convert the local character
 * set to the UCS; then convert the UCS to UTF-8.
 * The first step in the process can be performed by maintaining a mapping
 * table that includes the local character set code and the corresponding
 * UCS code.
 * The next step is to convert the UCS character code to the UTF-8 encoding.
 * <p>
 * Mapping between vendor codepages can be done in a very similar manner
 * as described above.
 * <p>
 * The only time escape encodings can allowedly be made is when a URI is
 * being created from its component parts.  The escape and validate methods
 * are internally performed within this method.
 *
 * @param original the original character sequence
 * @param allowed those characters that are allowed within a component
 * @param charset the protocol charset
 * @return URI character sequence
 * @throws URIException null component or unsupported character encoding
 */

protected static char[] encode(String original, BitSet allowed, String charset) throws URIException {
    if (original == null) {
        throw new IllegalArgumentException("Original string may not be null");
    }
    if (allowed == null) {
        throw new IllegalArgumentException("Allowed bitset may not be null");
    }
    byte[] rawdata = URLCodec.encodeUrl(allowed, getBytes(original, charset));
    return getAsciiString(rawdata).toCharArray();
}

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

/**
 * Encode the specified value./*from  w w  w.  ja  va  2 s.  co  m*/
 *
 * @param value The value to encode.
 * @return The encoded value.
 */
public static String oauthEncode(String value) {
    if (value == null) {
        return "";
    }

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

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

/**
 * Escape and encode a string regarded as the path component of an URI with
 * the default protocol charset./*from  w w w  .java 2 s. c  om*/
 * @param unescaped an unescaped string
 * @return the escaped string
 */
public static String encodePath(String unescaped) {
    byte[] rawdata = URLCodec.encodeUrl(allowed_abs_path, getBytes(unescaped, "UTF-8"));
    return getAsciiString(rawdata);
}