1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.openr66.protocol.http.restv2.resthandlers;
22
23 import io.netty.channel.ChannelHandlerContext;
24 import io.netty.channel.SimpleChannelInboundHandler;
25 import io.netty.handler.codec.http.FullHttpRequest;
26 import org.waarp.common.logging.WaarpLogger;
27 import org.waarp.common.logging.WaarpLoggerFactory;
28 import org.waarp.gateway.kernel.rest.RestConfiguration;
29 import org.waarp.openr66.protocol.http.rest.HttpRestR66Handler;
30
31 import static org.waarp.openr66.protocol.http.restv2.RestConstants.*;
32
33
34
35
36
37
38
39
40 public class RestVersionHandler
41 extends SimpleChannelInboundHandler<FullHttpRequest> {
42
43
44
45
46 private static final WaarpLogger logger =
47 WaarpLoggerFactory.getLogger(RestVersionHandler.class);
48
49
50
51
52 public static final String HANDLER_NAME = "version_handler";
53
54
55
56
57 private static final String V1_HANDLER = "v1_handler";
58
59
60
61
62 private final HttpRestR66Handler restV1Handler;
63
64
65
66
67
68
69
70 public RestVersionHandler(final RestConfiguration restConfiguration) {
71 super(false);
72 HttpRestR66Handler.instantiateHandlers(restConfiguration);
73 restV1Handler = new HttpRestR66Handler(restConfiguration);
74 }
75
76
77
78
79
80
81
82
83 @Override
84 protected void channelRead0(final ChannelHandlerContext ctx,
85 final FullHttpRequest request) {
86 logger.debug("{} received on {}", request.method(), request.uri());
87
88 if (request.uri().startsWith(VERSION_PREFIX)) {
89 if (ctx.pipeline().get(V1_HANDLER) != null) {
90 ctx.pipeline().remove(V1_HANDLER);
91 }
92
93 ctx.fireChannelRead(request);
94 } else {
95 if (ctx.pipeline().get(V1_HANDLER) == null) {
96 ctx.pipeline().addAfter(HANDLER_NAME, V1_HANDLER, restV1Handler);
97 }
98 ctx.fireChannelRead(request);
99 }
100 }
101 }