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.client;
21  
22  import io.netty.channel.ChannelHandlerContext;
23  import org.waarp.commandexec.client.LocalExecClientHandler;
24  import org.waarp.commandexec.client.LocalExecClientInitializer;
25  import org.waarp.commandexec.utils.LocalExecDefaultResult;
26  import org.waarp.common.crypto.ssl.WaarpSslUtility;
27  import org.waarp.common.logging.WaarpLogger;
28  import org.waarp.common.logging.WaarpLoggerFactory;
29  
30  import javax.net.ssl.SSLException;
31  
32  /**
33   *
34   */
35  public class LocalExecSslClientHandler extends LocalExecClientHandler {
36    /**
37     * Internal Logger
38     */
39    private static final WaarpLogger logger =
40        WaarpLoggerFactory.getLogger(LocalExecSslClientHandler.class);
41  
42    /**
43     * @param factory
44     */
45    public LocalExecSslClientHandler(final LocalExecClientInitializer factory) {
46      super(factory);
47    }
48  
49    @Override
50    public void channelRegistered(final ChannelHandlerContext ctx)
51        throws Exception {
52      WaarpSslUtility.addSslOpenedChannel(ctx.channel());
53      super.channelRegistered(ctx);
54    }
55  
56    @Override
57    public void exceptionCaught(final ChannelHandlerContext ctx,
58                                final Throwable cause) {
59      logger.warn("Unexpected exception from Outband while get information: " +
60                  firstMessage, cause);
61      if (firstMessage) {
62        firstMessage = false;
63        result.set(LocalExecDefaultResult.BadTransmition);
64        result.setException((Exception) cause);
65        back = new StringBuilder("Error in LocalExec: ").append(
66            result.getException().getMessage()).append('\n');
67      } else {
68        if (cause instanceof SSLException) {
69          // ignore ?
70          logger.warn("Ignore exception ?", cause);
71          return;
72        }
73        back.append("\nERROR while receiving answer: ");
74        result.setException((Exception) cause);
75        back.append(result.getException().getMessage()).append('\n');
76      }
77      actionBeforeClose(ctx.channel());
78      WaarpSslUtility.closingSslChannel(ctx.channel());
79    }
80  }