|
| 1 | +/* |
| 2 | + * Copyright © 2022 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.http.netty; |
| 17 | + |
| 18 | +import io.servicetalk.http.api.HttpExecutionStrategy; |
| 19 | +import io.servicetalk.http.api.HttpProtocolConfig; |
| 20 | +import io.servicetalk.http.api.HttpServerBuilder; |
| 21 | +import io.servicetalk.http.api.SingleAddressHttpClientBuilder; |
| 22 | +import io.servicetalk.transport.api.ExecutionContext; |
| 23 | +import io.servicetalk.transport.api.ExecutionStrategy; |
| 24 | +import io.servicetalk.transport.api.HostAndPort; |
| 25 | +import io.servicetalk.transport.api.ServerContext; |
| 26 | + |
| 27 | +import java.net.InetSocketAddress; |
| 28 | + |
| 29 | +import static io.servicetalk.http.netty.HttpProtocol.toConfigs; |
| 30 | +import static io.servicetalk.http.utils.HttpLifecycleObservers.logging; |
| 31 | +import static io.servicetalk.logging.api.LogLevel.TRACE; |
| 32 | +import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress; |
| 33 | +import static io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort; |
| 34 | + |
| 35 | +final class BuilderUtils { |
| 36 | + |
| 37 | + private BuilderUtils() { |
| 38 | + // No instances |
| 39 | + } |
| 40 | + |
| 41 | + static HttpServerBuilder newLocalServer(ExecutionContext<? extends ExecutionStrategy> ctx, |
| 42 | + HttpProtocol... protocols) { |
| 43 | + return newLocalServerWithConfigs(ctx, toConfigs(protocols)); |
| 44 | + } |
| 45 | + |
| 46 | + static HttpServerBuilder newLocalServerWithConfigs(ExecutionContext<? extends ExecutionStrategy> ctx, |
| 47 | + HttpProtocolConfig... protocols) { |
| 48 | + HttpServerBuilder builder = HttpServers.forAddress(localAddress(0)) |
| 49 | + .ioExecutor(ctx.ioExecutor()) |
| 50 | + .executor(ctx.executor()) |
| 51 | + .bufferAllocator(ctx.bufferAllocator()) |
| 52 | + .executionStrategy(HttpExecutionStrategy.from(ctx.executionStrategy())) |
| 53 | + .enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.TRUE::booleanValue) |
| 54 | + .lifecycleObserver(logging("servicetalk-tests-lifecycle-observer-logger", TRACE)); |
| 55 | + if (protocols.length > 0) { |
| 56 | + builder.protocols(protocols); |
| 57 | + } |
| 58 | + return builder; |
| 59 | + } |
| 60 | + |
| 61 | + static SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> newClient( |
| 62 | + ServerContext serverContext, |
| 63 | + ExecutionContext<? extends ExecutionStrategy> ctx, |
| 64 | + HttpProtocol... protocols) { |
| 65 | + return newClientWithConfigs(serverContext, ctx, toConfigs(protocols)); |
| 66 | + } |
| 67 | + |
| 68 | + static SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> newClientWithConfigs( |
| 69 | + ServerContext serverContext, |
| 70 | + ExecutionContext<? extends ExecutionStrategy> ctx, |
| 71 | + HttpProtocolConfig... protocols) { |
| 72 | + |
| 73 | + SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = |
| 74 | + HttpClients.forSingleAddress(serverHostAndPort(serverContext)) |
| 75 | + .ioExecutor(ctx.ioExecutor()) |
| 76 | + .executor(ctx.executor()) |
| 77 | + .bufferAllocator(ctx.bufferAllocator()) |
| 78 | + .executionStrategy(HttpExecutionStrategy.from(ctx.executionStrategy())) |
| 79 | + .enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.TRUE::booleanValue) |
| 80 | + .appendClientFilter(new HttpLifecycleObserverRequesterFilter( |
| 81 | + logging("servicetalk-tests-lifecycle-observer-logger", TRACE))); |
| 82 | + if (protocols.length > 0) { |
| 83 | + builder.protocols(protocols); |
| 84 | + } |
| 85 | + return builder; |
| 86 | + } |
| 87 | +} |
0 commit comments