Example usage for org.apache.http.util EncodingUtils getBytes

List of usage examples for org.apache.http.util EncodingUtils getBytes

Introduction

In this page you can find the example usage for org.apache.http.util EncodingUtils getBytes.

Prototype

public static byte[] getBytes(String str, String str2) 

Source Link

Usage

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

/**
 * Escape and encode a given string with allowed characters not to be
 * escaped and a given charset.//from   w  w w . ja va 2  s  . com
 *
 * @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.apache.clerezza.utils.UriUtil.java

/**
 * Escape and encode a given string with allowed characters not to be
 * escaped and a given charset./*from w ww. j ava  2s .  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(String unescaped, BitSet allowed, String charset) throws UriException {
    byte[] rawdata = URLCodec.encodeUrl(allowed, EncodingUtils.getBytes(unescaped, charset));
    return EncodingUtils.getAsciiString(rawdata);
}

From source file:android.webkit.cts.WebViewTest.java

@UiThreadTest
public void testPostUrlWithNetworkUrl() throws Exception {
    startWebServer(false);/*from w  w w.  j a v a2 s .c o  m*/
    final String networkUrl = mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL);
    final String postDataString = "username=my_username&password=my_password";
    final byte[] postData = EncodingUtils.getBytes(postDataString, "BASE64");

    mOnUiThread.postUrlAndWaitForCompletion(networkUrl, postData);

    HttpRequest request = mWebServer.getLastRequest(TestHtmlConstants.HELLO_WORLD_URL);
    // The last request should be POST
    assertEquals(request.getRequestLine().getMethod(), "POST");

    // The last request should have a request body
    assertTrue(request instanceof HttpEntityEnclosingRequest);
    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
    String entityString = EntityUtils.toString(entity);
    assertEquals(entityString, postDataString);
}

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

/**
 * Encodes URI string.//from   w  ww  .  j  a v  a  2s. c  o  m
 * <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:org.apache.clerezza.utils.Uri.java

/**
 * Encodes URI string./*from   w  w w .j  a  v a  2  s . c  om*/
 *
 * 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.http.impl.auth.BasicScheme.java

/**
 * Produces basic authorization header for the given set of {@link Credentials}.
 *
 * @param credentials The set of credentials to be used for authentication
 * @param request The request being authenticated
 * @throws org.apache.http.auth.InvalidCredentialsException if authentication
 *   credentials are not valid or not applicable for this authentication scheme
 * @throws AuthenticationException if authorization string cannot
 *   be generated due to an authentication failure
 *
 * @return a basic authorization string/*from w  ww.  java  2 s . c o  m*/
 */
@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context)
        throws AuthenticationException {

    Args.notNull(credentials, "Credentials");
    Args.notNull(request, "HTTP request");
    final StringBuilder tmp = new StringBuilder();
    tmp.append(credentials.getUserPrincipal().getName());
    tmp.append(":");
    tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());

    final byte[] base64password = base64codec
            .encode(EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request)));

    final CharArrayBuffer buffer = new CharArrayBuffer(32);
    if (isProxy()) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": Basic ");
    buffer.append(base64password, 0, base64password.length);

    return new BufferedHeader(buffer);
}

From source file:org.apache.http.impl.auth.BasicScheme.java

/**
 * Returns a basic <tt>Authorization</tt> header value for the given
 * {@link Credentials} and charset.//from   w  ww. j  a v a 2  s  .  c  o m
 *
 * @param credentials The credentials to encode.
 * @param charset The charset to use for encoding the credentials
 *
 * @return a basic authorization header
 *
 * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}.
 */
@Deprecated
public static Header authenticate(final Credentials credentials, final String charset, final boolean proxy) {
    Args.notNull(credentials, "Credentials");
    Args.notNull(charset, "charset");

    final StringBuilder tmp = new StringBuilder();
    tmp.append(credentials.getUserPrincipal().getName());
    tmp.append(":");
    tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());

    final byte[] base64password = Base64.encodeBase64(EncodingUtils.getBytes(tmp.toString(), charset), false);

    final CharArrayBuffer buffer = new CharArrayBuffer(32);
    if (proxy) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": Basic ");
    buffer.append(base64password, 0, base64password.length);

    return new BufferedHeader(buffer);
}

From source file:org.madsonic.controller.DownloadController.java

public static String UTF8(String s) {
    byte bytes[] = EncodingUtils.getBytes(s, "UTF-8");
    return new String(bytes);
}

From source file:org.madsonic.controller.DownloadController.java

public static String cp866(String s) {
    byte bytes[] = EncodingUtils.getBytes(s, "cp866");
    return new String(bytes);
}