Search in sources :

Example 1 with ZuulMessage

use of com.netflix.zuul.message.ZuulMessage in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsContentLengthValue.

@Test
public void getBodySizeIfKnown_returnsContentLengthValue() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    headers.add(com.netflix.zuul.message.http.HttpHeaderNames.CONTENT_LENGTH, "23450");
    ZuulMessage msg = new ZuulMessageImpl(context, headers);
    assertThat(HttpUtils.getBodySizeIfKnown(msg)).isEqualTo(Integer.valueOf(23450));
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) ZuulMessageImpl(com.netflix.zuul.message.ZuulMessageImpl) Test(org.junit.Test)

Example 2 with ZuulMessage

use of com.netflix.zuul.message.ZuulMessage in project zuul by Netflix.

the class HttpUtilsTest method getBodySizeIfKnown_returnsNull.

@Test
public void getBodySizeIfKnown_returnsNull() {
    SessionContext context = new SessionContext();
    Headers headers = new Headers();
    ZuulMessage msg = new ZuulMessageImpl(context, headers);
    assertThat(HttpUtils.getBodySizeIfKnown(msg)).isNull();
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) Headers(com.netflix.zuul.message.Headers) SessionContext(com.netflix.zuul.context.SessionContext) ZuulMessageImpl(com.netflix.zuul.message.ZuulMessageImpl) Test(org.junit.Test)

Example 3 with ZuulMessage

use of com.netflix.zuul.message.ZuulMessage in project zuul by Netflix.

the class BaseZuulFilterRunner method filter.

protected final O filter(final ZuulFilter<I, O> filter, final I inMesg) {
    final long startTime = System.nanoTime();
    final ZuulMessage snapshot = inMesg.getContext().debugRouting() ? inMesg.clone() : null;
    FilterChainResumer resumer = null;
    try (TaskCloseable ignored = traceTask(filter, f -> f.filterName() + ".filter")) {
        addPerfMarkTags(inMesg);
        ExecutionStatus filterRunStatus = null;
        if (filter.filterType() == INBOUND && inMesg.getContext().shouldSendErrorResponse()) {
            // Pass request down the pipeline, all the way to error endpoint if error response needs to be generated
            filterRunStatus = SKIPPED;
        }
        ;
        try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".shouldSkipFilter")) {
            if (shouldSkipFilter(inMesg, filter)) {
                filterRunStatus = SKIPPED;
            }
        }
        if (filter.isDisabled()) {
            filterRunStatus = DISABLED;
        }
        if (filterRunStatus != null) {
            recordFilterCompletion(filterRunStatus, filter, startTime, inMesg, snapshot);
            return filter.getDefaultOutput(inMesg);
        }
        if (!isMessageBodyReadyForFilter(filter, inMesg)) {
            setFilterAwaitingBody(inMesg, true);
            logger.debug("Filter {} waiting for body, UUID {}", filter.filterName(), inMesg.getContext().getUUID());
            // wait for whole body to be buffered
            return null;
        }
        setFilterAwaitingBody(inMesg, false);
        if (snapshot != null) {
            Debug.addRoutingDebug(inMesg.getContext(), "Filter " + filter.filterType().toString() + " " + filter.filterOrder() + " " + filter.filterName());
        }
        // run body contents accumulated so far through this filter
        inMesg.runBufferedBodyContentThroughFilter(filter);
        if (filter.getSyncType() == FilterSyncType.SYNC) {
            final SyncZuulFilter<I, O> syncFilter = (SyncZuulFilter) filter;
            final O outMesg;
            try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".apply")) {
                addPerfMarkTags(inMesg);
                outMesg = syncFilter.apply(inMesg);
            }
            recordFilterCompletion(SUCCESS, filter, startTime, inMesg, snapshot);
            return (outMesg != null) ? outMesg : filter.getDefaultOutput(inMesg);
        }
        // async filter
        try (TaskCloseable ignored2 = traceTask(filter, f -> f.filterName() + ".applyAsync")) {
            final Link nettyToSchedulerLink = linkOut();
            filter.incrementConcurrency();
            resumer = new FilterChainResumer(inMesg, filter, snapshot, startTime);
            filter.applyAsync(inMesg).doOnSubscribe(() -> {
                try (TaskCloseable ignored3 = traceTask(filter, f -> f.filterName() + ".onSubscribeAsync")) {
                    linkIn(nettyToSchedulerLink);
                }
            }).doOnNext(resumer.onNextStarted(nettyToSchedulerLink)).doOnError(resumer.onErrorStarted(nettyToSchedulerLink)).doOnCompleted(resumer.onCompletedStarted(nettyToSchedulerLink)).observeOn(Schedulers.from(getChannelHandlerContext(inMesg).executor())).doOnUnsubscribe(resumer::decrementConcurrency).subscribe(resumer);
        }
        // wait for the async filter to finish
        return null;
    } catch (Throwable t) {
        if (resumer != null) {
            resumer.decrementConcurrency();
        }
        final O outMesg = handleFilterException(inMesg, filter, t);
        outMesg.finishBufferedBodyIfIncomplete();
        recordFilterCompletion(FAILED, filter, startTime, inMesg, snapshot);
        return outMesg;
    }
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage) ExecutionStatus(com.netflix.zuul.ExecutionStatus) SyncZuulFilter(com.netflix.zuul.filters.SyncZuulFilter) Link(io.perfmark.Link) TaskCloseable(io.perfmark.TaskCloseable)

Example 4 with ZuulMessage

use of com.netflix.zuul.message.ZuulMessage in project zuul by Netflix.

the class HttpSyncEndpoint method applyAsync.

@Override
public Observable<HttpResponseMessage> applyAsync(HttpRequestMessage input) {
    if (WAIT_FOR_LASTCONTENT.get() && !input.hasCompleteBody()) {
        // received the whole request body), so that we can't potentially corrupt the clients' http state on this connection.
        return Observable.create(subscriber -> {
            ZuulMessage response = this.apply(input);
            ResponseState state = new ResponseState(response, subscriber);
            input.getContext().set(KEY_FOR_SUBSCRIBER, state);
        });
    } else {
        return Observable.just(this.apply(input));
    }
}
Also used : ZuulMessage(com.netflix.zuul.message.ZuulMessage)

Aggregations

ZuulMessage (com.netflix.zuul.message.ZuulMessage)4 SessionContext (com.netflix.zuul.context.SessionContext)2 Headers (com.netflix.zuul.message.Headers)2 ZuulMessageImpl (com.netflix.zuul.message.ZuulMessageImpl)2 Test (org.junit.Test)2 ExecutionStatus (com.netflix.zuul.ExecutionStatus)1 SyncZuulFilter (com.netflix.zuul.filters.SyncZuulFilter)1 Link (io.perfmark.Link)1 TaskCloseable (io.perfmark.TaskCloseable)1