1 /*
2 * This file is part of Waarp Project (named also Waarp or GG).
3 *
4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of
6 * individual contributors.
7 *
8 * All Waarp Project is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * Waarp . If not, see <http://www.gnu.org/licenses/>.
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 * Handles the dispatching of incoming request between version 1 and 2 of the
35 * REST API. By default, the
36 * pipeline continues with the v2 handler, but if the request URI does not match
37 * the pattern of a v2 entry
38 * point, then the pipeline will switch to the v1 handler.
39 */
40 public class RestVersionHandler
41 extends SimpleChannelInboundHandler<FullHttpRequest> {
42
43 /**
44 * Logger for all unexpected execution events.
45 */
46 private static final WaarpLogger logger =
47 WaarpLoggerFactory.getLogger(RestVersionHandler.class);
48
49 /**
50 * Name of this handler in the Netty pipeline.
51 */
52 public static final String HANDLER_NAME = "version_handler";
53
54 /**
55 * Name of the RESTv1 handler in the Netty pipeline.
56 */
57 private static final String V1_HANDLER = "v1_handler";
58
59 /**
60 * The RESTv1 handler.
61 */
62 private final HttpRestR66Handler restV1Handler;
63
64 /**
65 * Initializes the REST version splitter and handler with the given {@link
66 * RestConfiguration}
67 *
68 * @param restConfiguration the RestConfiguration object
69 */
70 public RestVersionHandler(final RestConfiguration restConfiguration) {
71 super(false);
72 HttpRestR66Handler.instantiateHandlers(restConfiguration);
73 restV1Handler = new HttpRestR66Handler(restConfiguration);
74 }
75
76 /**
77 * Dispatches the incoming request to the corresponding v1 or v2 REST
78 * handler.
79 *
80 * @param ctx the Netty pipeline context
81 * @param request the incoming request
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 }