Search in sources :

Example 1 with RuleMatch

use of com.spotify.apollo.route.RuleMatch in project apollo by spotify.

the class RequestRunnableImpl method matchAndRun.

private void matchAndRun(BiConsumer<OngoingRequest, RuleMatch<Endpoint>> matchContinuation) {
    final Request request = ongoingRequest.request();
    final Optional<RuleMatch<Endpoint>> match;
    try {
        match = applicationRouter.match(request);
    } catch (InvalidUriException e) {
        LOG.debug("bad uri {} {} {}", request.method(), request.uri(), BAD_REQUEST, e);
        ongoingRequest.reply(forStatus(BAD_REQUEST));
        return;
    }
    if (!match.isPresent()) {
        Collection<String> methods = applicationRouter.getMethodsForValidRules(request);
        if (methods.isEmpty()) {
            LOG.debug("not found {} {} {}", request.method(), request.uri(), NOT_FOUND);
            ongoingRequest.reply(forStatus(NOT_FOUND));
        } else {
            StatusType statusCode;
            if ("OPTIONS".equals(request.method())) {
                statusCode = NO_CONTENT;
            } else {
                statusCode = METHOD_NOT_ALLOWED;
                LOG.debug("wrong method {} {} {}", request.method(), request.uri(), statusCode);
            }
            methods = Sets.newTreeSet(methods);
            methods.add("OPTIONS");
            ongoingRequest.reply(Response.<ByteString>forStatus(statusCode).withHeader("Allow", Joiner.on(", ").join(methods)));
        }
        return;
    }
    matchContinuation.accept(ongoingRequest, match.get());
}
Also used : RuleMatch(com.spotify.apollo.route.RuleMatch) StatusType(com.spotify.apollo.StatusType) ByteString(okio.ByteString) Request(com.spotify.apollo.Request) InvalidUriException(com.spotify.apollo.route.InvalidUriException) ByteString(okio.ByteString)

Aggregations

Request (com.spotify.apollo.Request)1 StatusType (com.spotify.apollo.StatusType)1 InvalidUriException (com.spotify.apollo.route.InvalidUriException)1 RuleMatch (com.spotify.apollo.route.RuleMatch)1 ByteString (okio.ByteString)1