View Javadoc
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  package org.waarp.openr66.protocol.http.rest;
21  
22  import io.netty.channel.ChannelInitializer;
23  import io.netty.channel.ChannelPipeline;
24  import io.netty.channel.socket.SocketChannel;
25  import io.netty.handler.codec.http.HttpContentCompressor;
26  import io.netty.handler.codec.http.HttpServerCodec;
27  import io.netty.handler.ssl.SslHandler;
28  import io.netty.handler.stream.ChunkedWriteHandler;
29  import org.waarp.common.crypto.ssl.WaarpSslContextFactory;
30  import org.waarp.gateway.kernel.rest.RestConfiguration;
31  
32  /**
33   * Pipeline Factory for Rest HTTP support for R66
34   */
35  public class HttpRestR66Initializer extends ChannelInitializer<SocketChannel> {
36    private final boolean useHttpCompression;
37    private final WaarpSslContextFactory waarpSslContextFactory;
38    private final RestConfiguration restConfiguration;
39  
40    public HttpRestR66Initializer(final boolean useHttpCompression,
41                                  final WaarpSslContextFactory waarpSslContextFactory,
42                                  final RestConfiguration configuration) {
43      this.waarpSslContextFactory = waarpSslContextFactory;
44      this.useHttpCompression = useHttpCompression;
45      restConfiguration = configuration;
46    }
47  
48    @Override
49    protected void initChannel(final SocketChannel ch) {
50      // Create a default pipeline implementation.
51      final ChannelPipeline pipeline = ch.pipeline();
52  
53      // Enable HTTPS if necessary.
54      if (waarpSslContextFactory != null) {
55        final SslHandler handler =
56            waarpSslContextFactory.createHandlerServer(false, ch);
57        pipeline.addLast("ssl", handler);
58      }
59  
60      pipeline.addLast("codec", new HttpServerCodec());
61      if (useHttpCompression) {
62        pipeline.addLast("deflater", new HttpContentCompressor());
63      }
64      pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
65      final HttpRestR66Handler r66handler =
66          new HttpRestR66Handler(restConfiguration);
67      pipeline.addLast("handler", r66handler);
68    }
69  }