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

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

Introduction

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

Prototype

public static byte[] getAsciiBytes(String str) 

Source Link

Usage

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

/**
 * Decodes URI encoded string./*from w  w  w  . jav a 2  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(EncodingUtils.getAsciiBytes(component));
    } catch (DecoderException e) {
        throw new UriException(e.getMessage());
    }
    return EncodingUtils.getString(rawdata, charset);
}

From source file:org.apache.axis2.transport.nhttp.ServerHandler.java

/**
 * Handle HTTP Protocol violations with an error response
 * @param conn the connection being processed
 * @param e the exception encountered/*from   w  w w.  j  ava  2  s  . c  o  m*/
 */
public void exception(final NHttpServerConnection conn, final HttpException e) {
    HttpContext context = conn.getContext();
    HttpRequest request = conn.getHttpRequest();
    HttpVersion ver = request.getRequestLine().getHttpVersion();
    HttpResponse response = responseFactory.newHttpResponse(ver, HttpStatus.SC_BAD_REQUEST, context);
    byte[] msg = EncodingUtils.getAsciiBytes("Malformed HTTP request: " + e.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
    commitResponse(conn, response);
}

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

@Test
public void testBasicAuthentication() throws Exception {
    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");

    final Header challenge = new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\"");

    final BasicScheme authscheme = new BasicScheme();
    authscheme.processChallenge(challenge);

    final HttpRequest request = new BasicHttpRequest("GET", "/");
    final HttpContext context = new BasicHttpContext();
    final Header authResponse = authscheme.authenticate(creds, request, context);

    final String expected = "Basic " + EncodingUtils
            .getAsciiString(Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass")));
    Assert.assertEquals(AUTH.WWW_AUTH_RESP, authResponse.getName());
    Assert.assertEquals(expected, authResponse.getValue());
    Assert.assertEquals("test", authscheme.getRealm());
    Assert.assertTrue(authscheme.isComplete());
    Assert.assertFalse(authscheme.isConnectionBased());
}

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

@Test
public void testBasicProxyAuthentication() throws Exception {
    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass");

    final Header challenge = new BasicHeader(AUTH.PROXY_AUTH, "Basic realm=\"test\"");

    final BasicScheme authscheme = new BasicScheme();
    authscheme.processChallenge(challenge);

    final HttpRequest request = new BasicHttpRequest("GET", "/");
    final HttpContext context = new BasicHttpContext();
    final Header authResponse = authscheme.authenticate(creds, request, context);

    final String expected = "Basic " + EncodingUtils
            .getAsciiString(Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass")));
    Assert.assertEquals(AUTH.PROXY_AUTH_RESP, authResponse.getName());
    Assert.assertEquals(expected, authResponse.getValue());
    Assert.assertEquals("test", authscheme.getRealm());
    Assert.assertTrue(authscheme.isComplete());
    Assert.assertFalse(authscheme.isConnectionBased());
}

From source file:org.apache.http.localserver.BasicAuthTokenExtractor.java

public String extract(final HttpRequest request) throws HttpException {
    String auth = null;// ww  w  .j  a  v  a 2s .  co m

    Header h = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
    if (h != null) {
        String s = h.getValue();
        if (s != null) {
            auth = s.trim();
        }
    }

    if (auth != null) {
        int i = auth.indexOf(' ');
        if (i == -1) {
            throw new ProtocolException("Invalid Authorization header: " + auth);
        }
        String authscheme = auth.substring(0, i);
        if (authscheme.equalsIgnoreCase("basic")) {
            String s = auth.substring(i + 1).trim();
            try {
                byte[] credsRaw = EncodingUtils.getAsciiBytes(s);
                BinaryDecoder codec = new Base64();
                auth = EncodingUtils.getAsciiString(codec.decode(credsRaw));
            } catch (DecoderException ex) {
                throw new ProtocolException("Malformed BASIC credentials");
            }
        }
    }
    return auth;
}