Example usage for io.netty.util.internal AppendableCharSequence substring

List of usage examples for io.netty.util.internal AppendableCharSequence substring

Introduction

In this page you can find the example usage for io.netty.util.internal AppendableCharSequence substring.

Prototype

public String substring(int start, int end) 

Source Link

Document

Create a new String from the given start to end.

Usage

From source file:io.maelstorm.server.RequestHandler.java

License:Open Source License

protected String getEndPoint(final AppendableCharSequence uri) {
    if (uri.length() < 1) {
        throw new DefectiveRequest("Empty query");
    }/*from  w w w . j  av  a2 s .com*/
    if (uri.charAt(0) != '/') {
        throw new DefectiveRequest("Query doesn't start with a slash: <code>" + uri + "</code>");
    }
    int questionmark = -1;
    int slash = -1;
    boolean foundAll = false;
    for (int i = 1; i < uri.length() && !foundAll; i++) {
        switch (uri.charAt(i)) {
        case '?':
            questionmark = i;
            break;
        case '/':
            slash = i;
            break;
        default:
            if (i > 1 && (questionmark > 1 || slash > 1)) {
                foundAll = true;
            }
            break;
        }
    }
    int pos = uri.length(); // Will be set to where the first path segment ends.
    if (questionmark > 0) {
        if (slash > 0) {
            pos = (questionmark < slash ? questionmark // Request: /foo?bar/quux
                    : slash); // Request: /foo/bar?quux
        } else {
            pos = questionmark; // Request: /foo?bar
        }
    } else {
        pos = (slash > 0 ? slash // Request: /foo/bar
                : uri.length()); // Request: /foo
    }
    return uri.substring(1, pos);
}

From source file:io.maelstorm.server.statistics.StatisticsEndPoint.java

License:Open Source License

public Deferred<FullHttpResponse> process(ChannelHandlerContext context, AggregatedFullHttpRequest request) {
    AppendableCharSequence uri = request.geturi();
    StringBuilder buffer = new StringBuilder();
    Deferred<FullHttpResponse> deferred = new Deferred<FullHttpResponse>();
    FullHttpResponse response;//from w  ww  .  java  2  s  .c om
    if (uri.length() == 6) {// uri = "/stats"
        for (Entry<String, ICollector> entry : statistics.entrySet()) {
            buffer.append(entry.getKey()).append("<br>").append("<table>")
                    .append(mapToBuffer(entry.getValue().getStatistics(), entry.getValue().isDisplayed()))
                    .append("</table>").append("<br>").append("<br>");
        }
        response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK,
                ResponseUtils.makePage(null, "Stats", "Stats", buffer));
    } else {
        ArrayList<Integer> splitterLocations = new ArrayList<Integer>(3);
        for (int i = 6; i < uri.length(); i++) {
            if (splitter == uri.charAt(i)) {
                splitterLocations.add(i);
            }
        }
        String key1;
        switch (splitterLocations.size()) {
        case 1:
            key1 = uri.substring(splitterLocations.get(0) + 1, uri.length());
            buffer.append(key1).append("<br>").append("<table>")
                    .append(mapToBuffer(statistics.get(key1).getStatistics(), true)).append("</table>")
                    .append("<br>").append("<br>");
            response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK,
                    ResponseUtils.makePage(null, "Stats", "Stats", buffer));
            break;
        case 2:
            key1 = uri.substring(splitterLocations.get(0) + 1, splitterLocations.get(1));
            String key2 = uri.substring(splitterLocations.get(1) + 1, uri.length());
            buffer.append(statistics.get(key1).getStatistics().get(key2));
            response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK,
                    ResponseUtils.getChannelBuffer(buffer));
            break;
        default:
            response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.NOT_FOUND);
        }
    }
    deferred.callback(response);
    return deferred;
}

From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java

License:Apache License

private static String[] splitInitialLine(AppendableCharSequence sb) {
    int aStart;/*from   w w w.ja  v  a2  s. c  om*/
    int aEnd;
    int bStart;
    int bEnd;
    int cStart;
    int cEnd;

    aStart = findNonWhitespace(sb, 0);
    aEnd = findWhitespace(sb, aStart);

    bStart = findNonWhitespace(sb, aEnd);
    bEnd = findWhitespace(sb, bStart);

    cStart = findNonWhitespace(sb, bEnd);
    cEnd = findEndOfString(sb);

    return new String[] { sb.substring(aStart, aEnd), sb.substring(bStart, bEnd),
            cStart < cEnd ? sb.substring(cStart, cEnd) : "" };
}

From source file:openbns.commons.net.codec.sts.HttpObjectDecoder.java

License:Apache License

private static String[] splitHeader(AppendableCharSequence sb) {
    final int length = sb.length();
    int nameStart;
    int nameEnd;/*from   w ww.j  ava 2s .c o m*/
    int colonEnd;
    int valueStart;
    int valueEnd;

    nameStart = findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < length; nameEnd++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }

    for (colonEnd = nameEnd; colonEnd < length; colonEnd++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd++;
            break;
        }
    }

    valueStart = findNonWhitespace(sb, colonEnd);
    if (valueStart == length) {
        return new String[] { sb.substring(nameStart, nameEnd), "" };
    }

    valueEnd = findEndOfString(sb);
    return new String[] { sb.substring(nameStart, nameEnd), sb.substring(valueStart, valueEnd) };
}