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.commandexec.ssl.server;
21  
22  import io.netty.channel.ChannelPipeline;
23  import io.netty.channel.socket.SocketChannel;
24  import io.netty.handler.codec.DelimiterBasedFrameDecoder;
25  import io.netty.handler.codec.Delimiters;
26  import io.netty.handler.codec.string.StringDecoder;
27  import io.netty.handler.codec.string.StringEncoder;
28  import io.netty.handler.ssl.SslHandler;
29  import io.netty.util.concurrent.EventExecutorGroup;
30  import org.waarp.commandexec.server.LocalExecServerInitializer;
31  import org.waarp.commandexec.utils.LocalExecDefaultResult;
32  import org.waarp.common.crypto.ssl.WaarpSslContextFactory;
33  
34  /**
35   * Version with SSL support
36   */
37  public class LocalExecSslServerInitializer extends LocalExecServerInitializer {
38  
39    private final WaarpSslContextFactory waarpSslContextFactory;
40  
41    private long delay = LocalExecDefaultResult.MAXWAITPROCESS;
42  
43    /**
44     * Constructor with default delay
45     *
46     * @param waarpSslContextFactory
47     * @param eventExecutorGroup
48     */
49    public LocalExecSslServerInitializer(
50        final WaarpSslContextFactory waarpSslContextFactory,
51        final EventExecutorGroup eventExecutorGroup) {
52      // Default delay
53      super(eventExecutorGroup);
54      this.waarpSslContextFactory = waarpSslContextFactory;
55    }
56  
57    /**
58     * Constructor with a specific default delay
59     *
60     * @param waarpSslContextFactory
61     * @param newdelay
62     */
63    public LocalExecSslServerInitializer(
64        final WaarpSslContextFactory waarpSslContextFactory, final long newdelay,
65        final EventExecutorGroup eventExecutorGroup) {
66      super(eventExecutorGroup);
67      delay = newdelay;
68      this.waarpSslContextFactory = waarpSslContextFactory;
69    }
70  
71    @Override
72    public void initChannel(final SocketChannel ch) {
73      // Create a default pipeline implementation.
74      final ChannelPipeline pipeline = ch.pipeline();
75  
76      // Add SSL as first element in the pipeline
77      final SslHandler sslhandler = waarpSslContextFactory.createHandlerServer(
78          waarpSslContextFactory.needClientAuthentication(), ch);
79      pipeline.addLast("ssl", sslhandler);
80      // Add the text line codec combination first,
81      pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
82                                                                Delimiters.lineDelimiter()));
83      pipeline.addLast(eventExecutorGroup, "decoder", new StringDecoder());
84      pipeline.addLast(eventExecutorGroup, "encoder", new StringEncoder());
85  
86      // and then business logic.
87      // Could change it with a new fixed delay if necessary at construction
88      pipeline.addLast(eventExecutorGroup, "handler",
89                       new LocalExecSslServerHandler(this, delay));
90    }
91  
92  }