Example usage for org.apache.http.util CharArrayBuffer substring

List of usage examples for org.apache.http.util CharArrayBuffer substring

Introduction

In this page you can find the example usage for org.apache.http.util CharArrayBuffer substring.

Prototype

public String substring(int i, int i2) 

Source Link

Usage

From source file:com.subgraph.vega.ui.httpeditor.parser.HttpResponseParser.java

/**
 * Parse a HTTP response in a string./*from  w  w  w .  j  a  v  a 2 s . c o m*/
 * 
 * @param content HTTP response string.
 * @return HttpResponse, or null if the given HTTP response was empty. 
 * @throws UnsupportedEncodingException
 */
public void parseResponse(final String content) throws UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseStatusLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

From source file:com.subgraph.vega.ui.httpeditor.parser.HttpRequestParser.java

/**
 * Parse a manually-entered HTTP request into the IHttpRequestBuilder.
 * //from   w w  w  .ja  va  2s . c  o m
 * @param content Manually-entered HTTP request.
 */
public void parseRequest(final String content) throws URISyntaxException, UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseRequestLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

From source file:com.soundcloud.playerapi.OAuth2Scheme.java

@Override
public void processChallenge(Header header) throws MalformedChallengeException {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }/*from  ww  w. j  a va 2 s .com*/
    String authHeader = header.getName();
    if (!authHeader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
        throw new MalformedChallengeException("Unexpected header name: " + authHeader);
    }

    CharArrayBuffer buffer;
    int pos;
    if (header instanceof FormattedHeader) {
        buffer = ((FormattedHeader) header).getBuffer();
        pos = ((FormattedHeader) header).getValuePos();
    } else {
        String s = header.getValue();
        if (s == null) {
            throw new MalformedChallengeException("Header value is null");
        }
        buffer = new CharArrayBuffer(s.length());
        buffer.append(s);
        pos = 0;
    }
    while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int beginIndex = pos;
    while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int endIndex = pos;
    String s = buffer.substring(beginIndex, endIndex);
    if (!s.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid scheme identifier: " + s);
    }
    HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    ParserCursor cursor = new ParserCursor(pos, buffer.length());
    HeaderElement[] elements = parser.parseElements(buffer, cursor);
    if (elements.length == 0) {
        throw new MalformedChallengeException("Authentication challenge is empty");
    }
    for (HeaderElement element : elements) {
        this.mParams.put(element.getName(), element.getValue());
    }
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) {
    AssertUtils.notNull(buffer, "Char array buffer");
    AssertUtils.notNull(cursor, "Parser cursor");

    boolean terminated = false;

    int pos = cursor.getPos();
    final int indexFrom = cursor.getPos();
    final int indexTo = cursor.getUpperBound();

    // Find name/*w w w. j a  v  a  2  s.  c o m*/
    final String name;
    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (ch == '=') {
            break;
        }
        if (isOneOf(ch, delimiters)) {
            terminated = true;
            break;
        }
        pos++;
    }

    if (pos == indexTo) {
        terminated = true;
        name = buffer.substringTrimmed(indexFrom, indexTo);
    } else {
        name = buffer.substringTrimmed(indexFrom, pos);
        pos++;
    }

    if (terminated) {
        cursor.updatePos(pos);
        return createNameValuePair(name, null);
    }

    // Find value
    final String value;
    int i1 = pos;

    boolean qouted = false;
    boolean escaped = false;
    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (ch == '"' && !escaped) {
            qouted = !qouted;
        }
        if (!qouted && !escaped && isOneOf(ch, delimiters)) {
            terminated = true;
            break;
        }
        if (escaped) {
            escaped = false;
        } else {
            escaped = qouted && ch == '\\';
        }
        pos++;
    }

    int i2 = pos;
    // Trim leading white spaces
    while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
        i1++;
    }
    // Trim trailing white spaces
    while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
        i2--;
    }
    // Strip away quotes if necessary
    if (((i2 - i1) >= 2) && (buffer.charAt(i1) == '"') && (buffer.charAt(i2 - 1) == '"')) {
        i1++;
        i2--;
    }
    value = buffer.substring(i1, i2);
    if (terminated) {
        pos++;
    }
    cursor.updatePos(pos);
    return createNameValuePair(name, value);
}

From source file:org.apache.http.impl.client.AbstractAuthenticationHandler.java

protected Map<String, Header> parseChallenges(final Header[] headers) throws MalformedChallengeException {

    final Map<String, Header> map = new HashMap<String, Header>(headers.length);
    for (final Header header : headers) {
        final CharArrayBuffer buffer;
        int pos;/* w ww  .  ja  v a  2  s  .  c o m*/
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            pos = ((FormattedHeader) header).getValuePos();
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedChallengeException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            pos = 0;
        }
        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int beginIndex = pos;
        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int endIndex = pos;
        final String s = buffer.substring(beginIndex, endIndex);
        map.put(s.toLowerCase(Locale.US), header);
    }
    return map;
}

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public Map<String, Header> getChallenges(final HttpHost authhost, final HttpResponse response,
        final HttpContext context) throws MalformedChallengeException {
    Args.notNull(response, "HTTP response");
    final Header[] headers = response.getHeaders(this.headerName);
    final Map<String, Header> map = new HashMap<String, Header>(headers.length);
    for (final Header header : headers) {
        final CharArrayBuffer buffer;
        int pos;//from   ww w .  j a va 2  s. c o m
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            pos = ((FormattedHeader) header).getValuePos();
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedChallengeException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            pos = 0;
        }
        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int beginIndex = pos;
        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int endIndex = pos;
        final String s = buffer.substring(beginIndex, endIndex);
        map.put(s.toLowerCase(Locale.US), header);
    }
    return map;
}