Search in sources :

Example 96 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project flink by apache.

the class Router method toString.

/**
 * Returns visualized routing rules.
 */
@Override
public String toString() {
    // Step 1/2: Dump routers and anyMethodRouter in order
    int numRoutes = size();
    List<String> methods = new ArrayList<String>(numRoutes);
    List<String> patterns = new ArrayList<String>(numRoutes);
    List<String> targets = new ArrayList<String>(numRoutes);
    // For router
    for (Entry<HttpMethod, MethodlessRouter<T>> e : routers.entrySet()) {
        HttpMethod method = e.getKey();
        MethodlessRouter<T> router = e.getValue();
        aggregateRoutes(method.toString(), router.routes(), methods, patterns, targets);
    }
    // For anyMethodRouter
    aggregateRoutes("*", anyMethodRouter.routes(), methods, patterns, targets);
    // For notFound
    if (notFound != null) {
        methods.add("*");
        patterns.add("*");
        targets.add(targetToString(notFound));
    }
    // Step 2/2: Format the List into aligned columns: <method> <patterns> <target>
    int maxLengthMethod = maxLength(methods);
    int maxLengthPattern = maxLength(patterns);
    String format = "%-" + maxLengthMethod + "s  %-" + maxLengthPattern + "s  %s\n";
    int initialCapacity = (maxLengthMethod + 1 + maxLengthPattern + 1 + 20) * methods.size();
    StringBuilder b = new StringBuilder(initialCapacity);
    for (int i = 0; i < methods.size(); i++) {
        String method = methods.get(i);
        String pattern = patterns.get(i);
        String target = targets.get(i);
        b.append(String.format(format, method, pattern, target));
    }
    return b.toString();
}
Also used : ArrayList(java.util.ArrayList) HttpMethod(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod)

Example 97 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project flink by apache.

the class Router method allowedMethods.

/**
 * Returns allowed methods for a specific URI.
 *
 * <p>For {@code OPTIONS *}, use {@link #allAllowedMethods()} instead of this method.
 */
public Set<HttpMethod> allowedMethods(String uri) {
    QueryStringDecoder decoder = new QueryStringDecoder(uri);
    String[] tokens = PathPattern.removeSlashesAtBothEnds(decoder.path()).split("/");
    if (anyMethodRouter.anyMatched(tokens)) {
        return allAllowedMethods();
    }
    Set<HttpMethod> ret = new HashSet<HttpMethod>(routers.size());
    for (Map.Entry<HttpMethod, MethodlessRouter<T>> entry : routers.entrySet()) {
        MethodlessRouter<T> router = entry.getValue();
        if (router.anyMatched(tokens)) {
            HttpMethod method = entry.getKey();
            ret.add(method);
        }
    }
    return ret;
}
Also used : QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) Map(java.util.Map) HashMap(java.util.HashMap) HttpMethod(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod) HashSet(java.util.HashSet)

Example 98 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project flink by apache.

the class RouterHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
    if (HttpHeaders.is100ContinueExpected(httpRequest)) {
        channelHandlerContext.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
        return;
    }
    // Route
    HttpMethod method = httpRequest.getMethod();
    QueryStringDecoder qsd = new QueryStringDecoder(httpRequest.uri());
    RouteResult<?> routeResult = router.route(method, qsd.path(), qsd.parameters());
    if (routeResult == null) {
        respondNotFound(channelHandlerContext, httpRequest);
        return;
    }
    routed(channelHandlerContext, routeResult, httpRequest);
}
Also used : QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) HttpMethod(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod)

Example 99 with HttpMethod

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod in project crate by crate.

the class HttpBlobHandler method possibleRedirect.

private boolean possibleRedirect(HttpRequest request, String index, String digest) {
    HttpMethod method = request.method();
    if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.HEAD) || (method.equals(HttpMethod.PUT) && HttpUtil.is100ContinueExpected(request))) {
        String redirectAddress;
        try {
            redirectAddress = blobService.getRedirectAddress(index, digest);
        } catch (MissingHTTPEndpointException ex) {
            simpleResponse(request, HttpResponseStatus.BAD_GATEWAY);
            return true;
        }
        if (redirectAddress != null) {
            LOGGER.trace("redirectAddress: {}", redirectAddress);
            sendRedirect(request, activeScheme + redirectAddress);
            return true;
        }
    }
    return false;
}
Also used : MissingHTTPEndpointException(io.crate.blob.exceptions.MissingHTTPEndpointException) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)95 Test (org.junit.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)28 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)25 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)21 URI (java.net.URI)15 IOException (java.io.IOException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)11 HttpVersion (io.netty.handler.codec.http.HttpVersion)11 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)11 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)11 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)11 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Subscription (rx.Subscription)11 Action2 (rx.functions.Action2)11 ByteBuf (io.netty.buffer.ByteBuf)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)10 Map (java.util.Map)9