Example usage for org.apache.http.message ParserCursor ParserCursor

List of usage examples for org.apache.http.message ParserCursor ParserCursor

Introduction

In this page you can find the example usage for org.apache.http.message ParserCursor ParserCursor.

Prototype

public ParserCursor(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  .ja  va2  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.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

/**
 * Parses elements with the given parser.
 *
 * @param value  the header value to parse
 * @param parser the parser to use, or <code>null</code> for default
 * @return array holding the header elements, never <code>null</code>
 *///ww  w.j  av a2  s . c om
public static HeaderElement[] parseElements(final String value, final HeaderValueParser parser)
        throws ParseException {
    AssertUtils.notNull(value, "Value");

    final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    final ParserCursor cursor = new ParserCursor(0, value.length());
    return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseElements(buffer, cursor);
}

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

/**
 * Parse a manually-entered HTTP request into the IHttpRequestBuilder.
 * /*from w w w.  j  av  a 2 s .  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:net.swas.explorer.httpprofile.DefaultHttpResponseParser.java

@Override
protected HttpMessage parseHead(SessionInputBuffer sessionBuffer)
        throws IOException, HttpException, ParseException {

    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);

    if (i == -1) {

        throw new ConnectionClosedException("Client closed connection");

    }/*from w w  w .j a v a2  s.  c  om*/

    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    StatusLine statusLine = this.lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusLine, null);

}

From source file:Main.java

/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed.
 *
 * @param s text to parse.//  ww w . j  a v a  2  s.c  om
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(nvp.getName(), nvp.getValue()));
        }
    }
    return list;
}

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

/**
 * Parses an element with the given parser.
 *
 * @param value  the header element to parse
 * @param parser the parser to use, or <code>null</code> for default
 * @return the parsed header element//  w  w  w . ja v a2  s .  c  o m
 */
public static HeaderElement parseHeaderElement(final String value, final HeaderValueParser parser)
        throws ParseException {
    AssertUtils.notNull(value, "Value");

    final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    final ParserCursor cursor = new ParserCursor(0, value.length());
    return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseHeaderElement(buffer, cursor);
}

From source file:net.swas.explorer.httpprofile.DefaultHttpRequestParser.java

@Override
protected HttpRequest parseHead(final SessionInputBuffer sessionBuffer)
        throws IOException, HttpException, ParseException {

    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {

        throw new ConnectionClosedException("Client closed connection");

    }//from  www  .java 2  s .c om

    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);

    return this.requestFactory.newHttpRequest(requestline);

}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public InetAddressRange parse(final String s) throws ParseException, UnknownHostException {
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);/*  w  w  w.j a va2s  . c  o m*/
    ParserCursor cursor = new ParserCursor(0, s.length());
    return parse(buffer, cursor, COMMA);
}

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

/**
 * Read and parse the response line./*from w  w  w .j  a v a  2  s. c  o m*/
 * 
 * @param parser HC LineParser.
 * @param builder HTTP response builder.
 * @param buf
 * @param bufCursor
 * @return
 */
private int parseStatusLine(final LineParser parser, final IHttpResponseBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    if (readLine(buf, bufCursor, lnBuf) < 1) {
        // no data!
        return -1;
    }
    final ParserCursor lnCursor = new ParserCursor(0, lnBuf.length());

    final StatusLine statusLine = parser.parseStatusLine(lnBuf, lnCursor);
    builder.setFromStatusLine(statusLine);

    return 0;
}

From source file:it.unimi.di.law.warc.io.AbstractWarcReader_NYU.java

private ProtocolVersion parseHead() throws IOException {
    this.line.clear();
    int read = this.buffer.readLine(this.line);
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("Protocol header '{}'.", new String(this.line.toCharArray()));
    if (read == -1)
        return null;
    ParserCursor cursor = new ParserCursor(0, this.line.length());
    try {/*from w ww  .j a  v  a  2 s. c  om*/
        return parser.parseProtocolVersion(this.line, cursor);
    } catch (ParseException e) {
        throw new WarcFormatException("Can't parse WARC version header.", e);
    }
}