|
| 1 | +/* |
| 2 | + * Copyright © 2019, 2021 Apple Inc. and the ServiceTalk project authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.servicetalk.grpc.netty; |
| 17 | + |
| 18 | +import io.servicetalk.concurrent.api.Single; |
| 19 | +import io.servicetalk.http.api.HttpExecutionStrategies; |
| 20 | +import io.servicetalk.http.api.HttpExecutionStrategy; |
| 21 | +import io.servicetalk.http.api.HttpResponse; |
| 22 | +import io.servicetalk.http.api.HttpServiceContext; |
| 23 | +import io.servicetalk.http.api.StreamingHttpRequest; |
| 24 | +import io.servicetalk.http.api.StreamingHttpResponse; |
| 25 | +import io.servicetalk.http.api.StreamingHttpResponseFactory; |
| 26 | +import io.servicetalk.http.api.StreamingHttpService; |
| 27 | +import io.servicetalk.http.api.StreamingHttpServiceFilter; |
| 28 | +import io.servicetalk.http.api.StreamingHttpServiceFilterFactory; |
| 29 | + |
| 30 | +import static io.servicetalk.grpc.internal.GrpcContextKeys.TRAILERS_ONLY_RESPONSE; |
| 31 | + |
| 32 | +final class GrpcEnforceTrailersOnlyResponseServiceFilter implements StreamingHttpServiceFilterFactory { |
| 33 | + static final GrpcEnforceTrailersOnlyResponseServiceFilter INSTANCE = |
| 34 | + new GrpcEnforceTrailersOnlyResponseServiceFilter(); |
| 35 | + |
| 36 | + private GrpcEnforceTrailersOnlyResponseServiceFilter() { |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public StreamingHttpServiceFilter create(StreamingHttpService service) { |
| 41 | + return new StreamingHttpServiceFilter(service) { |
| 42 | + @Override |
| 43 | + public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, |
| 44 | + final StreamingHttpRequest request, |
| 45 | + final StreamingHttpResponseFactory responseFactory) { |
| 46 | + return delegate().handle(ctx, request, responseFactory).flatMap(response -> { |
| 47 | + Single<StreamingHttpResponse> mappedResponse; |
| 48 | + if (Boolean.TRUE.equals(response.context().get(TRAILERS_ONLY_RESPONSE))) { |
| 49 | + mappedResponse = response.toResponse().map(HttpResponse::toStreamingResponse); |
| 50 | + } else { |
| 51 | + mappedResponse = Single.succeeded(response); |
| 52 | + } |
| 53 | + return mappedResponse.shareContextOnSubscribe(); |
| 54 | + }); |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public HttpExecutionStrategy requiredOffloads() { |
| 61 | + return HttpExecutionStrategies.offloadNone(); |
| 62 | + } |
| 63 | +} |
0 commit comments