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:com.microsoft.tfs.core.httpclient.util.URIUtil.java

/**
 * Escape and encode a given string with allowed characters not to be
 * escaped and a given charset.// w w w.j ava 2  s.  c o  m
 *
 * @param unescaped
 *        a string
 * @param allowed
 *        allowed characters not to be escaped
 * @param charset
 *        the charset
 * @return the escaped string
 */
public static String encode(final String unescaped, final BitSet allowed, final String charset)
        throws URIException {
    final byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(unescaped, charset));
    return EncodingUtil.getAsciiString(rawdata);
}

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

/**
 * Encodes URI string./*from   ww  w . j a va 2 s .c  om*/
 * <p/>
 * 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>
 * <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 HttpException null component or unsupported character encoding
 */

protected static char[] encode(String original, BitSet allowed, String charset) throws HttpException {
    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, EncodingUtils.getBytes(original, charset));
    return EncodingUtils.getAsciiString(rawdata).toCharArray();
}

From source file:bin.httpclient3.uri.URI.java

/**
 * Encodes URI string.//  w  w  w. ja  v  a  2s .  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, EncodingUtil.getBytes(original, charset));
    return EncodingUtil.getAsciiString(rawdata).toCharArray();
}

From source file:com.microsoft.tfs.core.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-&gt;octet sequence-&gt;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(final String original, final BitSet allowed, final 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");
    }
    final byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtil.getBytes(original, charset));
    return EncodingUtil.getAsciiString(rawdata).toCharArray();
}

From source file:org.apache.amber.signature.AbstractMethod.java

/**
 * Applies the percent encoding algorithm to the input text.
 *
 * @param text the text has to be encoded.
 * @return the encoded string./*from  w  ww . ja v  a 2  s . com*/
 */
protected static String percentEncode(String text) {
    return new String(URLCodec.encodeUrl(UNRESERVED_CHARS, toUTF8Bytes(text)), UTF_8);
}

From source file:org.apache.clerezza.utils.Uri.java

/**
 * Encodes URI string./*from  ww w .  j  a  v a2 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, EncodingUtils.getBytes(original, charset));
    return EncodingUtils.getAsciiString(rawdata).toCharArray();
}

From source file:org.apache.clerezza.utils.UriUtil.java

/**
 * Escape and encode a given string with allowed characters not to be
 * escaped and a given charset./*from  www  . j  a  v  a 2s .  co  m*/
 *
 * @param unescaped a string
 * @param allowed allowed characters not to be escaped
 * @param charset the charset
 * @return the escaped string
 */
public static String encode(String unescaped, BitSet allowed, String charset) throws UriException {
    byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtils.getBytes(unescaped, charset));
    return EncodingUtils.getAsciiString(rawdata);
}

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

/**
 * Encodes a URI from raw components. Any component may be null, but
 * resulting string may not be a valid URI.
 * @param scheme The scheme./*  ww w  .  j  a  v  a  2s .  c  o  m*/
 * @param authority The authority.
 * @param path The path.
 * @param qs The query string.
 * @param fragment The fragment.
 * @return The encoded URI as a string.
 */
public static String encode(final String scheme, final String authority, final String path, final String qs,
        final String fragment) {
    StringBuilder buf = new StringBuilder();
    if (scheme != null) {
        buf.append(scheme);
        buf.append("://");
    }

    byte[] bytes;

    if (authority != null) {
        bytes = URLCodec.encodeUrl(authorityBitSet, authority.getBytes(Charsets.UTF_8));
        buf.append(new String(bytes, Charsets.US_ASCII));
    }

    if (path != null) {
        bytes = URLCodec.encodeUrl(pathBitSet, path.getBytes(Charsets.UTF_8));
        buf.append(new String(bytes, Charsets.US_ASCII));
    }

    if (qs != null) {
        buf.append('?');
        bytes = URLCodec.encodeUrl(queryBitSet, qs.getBytes(Charsets.UTF_8));
        buf.append(new String(bytes, Charsets.US_ASCII));
    }

    if (fragment != null) {
        buf.append('#');
        bytes = URLCodec.encodeUrl(queryBitSet, fragment.getBytes(Charsets.UTF_8));
        buf.append(new String(bytes, Charsets.US_ASCII));
    }

    return buf.toString();
}

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

/**
 * Encodes the path component, excluding the query string.
 * @param path The path.//from   w w w.j  a v  a2 s.c  o m
 * @return The encoded path.
 */
public static final String encodePath(final String path) {
    byte[] bytes = URLCodec.encodeUrl(pathBitSet, path.getBytes(Charsets.UTF_8));
    return new String(bytes, Charsets.US_ASCII);
}

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

/**
 * Encodes a query string component.//  w  ww .  java2 s  .  co  m
 * @param qs The query string.
 * @return The encoded path.
 */
public static final String encodeQueryString(final String qs) {
    byte[] bytes = URLCodec.encodeUrl(queryBitSet, qs.getBytes(Charsets.UTF_8));
    return new String(bytes, Charsets.US_ASCII);
}