Refactor transport-http response delivery
[netconf.git] / transport / transport-http / src / main / java / org / opendaylight / netconf / transport / http / ClientRequestDispatcher.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.transport.http;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.SimpleChannelInboundHandler;
16 import io.netty.handler.codec.http.FullHttpRequest;
17 import io.netty.handler.codec.http.FullHttpResponse;
18 import org.eclipse.jdt.annotation.NonNull;
19
20 abstract class ClientRequestDispatcher extends SimpleChannelInboundHandler<FullHttpResponse>
21         implements RequestDispatcher {
22     private Channel channel = null;
23
24     @Override
25     public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
26         channel = ctx.channel();
27         super.handlerAdded(ctx);
28     }
29
30     @Override
31     public final void dispatch(final FullHttpRequest request, final FutureCallback<FullHttpResponse> callback) {
32         final var local = channel;
33         if (local != null) {
34             dispatch(local, requireNonNull(request), requireNonNull(callback));
35         } else {
36             throw new IllegalStateException("Connection is not established yet");
37         }
38     }
39
40     abstract void dispatch(@NonNull Channel channel, @NonNull FullHttpRequest request,
41         @NonNull FutureCallback<FullHttpResponse> callback);
42 }