View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.core.command.internal;
19  
20  import io.netty.channel.ChannelFuture;
21  import io.netty.channel.ChannelFutureListener;
22  import org.waarp.common.command.ReplyCode;
23  import org.waarp.common.command.exception.Reply500Exception;
24  import org.waarp.common.command.exception.Reply501Exception;
25  import org.waarp.common.crypto.ssl.WaarpSslUtility;
26  import org.waarp.common.logging.WaarpLogger;
27  import org.waarp.common.logging.WaarpLoggerFactory;
28  import org.waarp.ftp.core.command.AbstractCommand;
29  import org.waarp.ftp.core.config.FtpConfiguration;
30  import org.waarp.ftp.core.utils.FtpChannelUtils;
31  
32  /**
33   * Internal shutdown command that will shutdown the FTP service with a password
34   * 
35   * @author Frederic Bregier
36   * 
37   */
38  public class INTERNALSHUTDOWN extends AbstractCommand {
39      /**
40       * Internal Logger
41       */
42      private static final WaarpLogger logger = WaarpLoggerFactory
43              .getLogger(INTERNALSHUTDOWN.class);
44  
45      /**
46       * 
47       * @author Frederic Bregier
48       * 
49       */
50      private static class ShutdownChannelFutureListener implements
51              ChannelFutureListener {
52  
53          private final FtpConfiguration configuration;
54  
55          protected ShutdownChannelFutureListener(FtpConfiguration configuration) {
56              this.configuration = configuration;
57          }
58  
59          public void operationComplete(ChannelFuture arg0) throws Exception {
60              WaarpSslUtility.closingSslChannel(arg0.channel());
61              FtpChannelUtils.teminateServer(configuration);
62          }
63  
64      }
65  
66      @Override
67      public void exec() throws Reply501Exception, Reply500Exception {
68          if (!getSession().getAuth().isAdmin()) {
69              // not admin
70              throw new Reply500Exception("Command Not Allowed");
71          }
72          if (!hasArg()) {
73              throw new Reply501Exception("Shutdown Need password");
74          }
75          String password = getArg();
76          if (!getConfiguration().checkPassword(password)) {
77              throw new Reply501Exception("Shutdown Need a correct password");
78          }
79          logger.warn("Shutdown...");
80          getSession().setReplyCode(
81                  ReplyCode.REPLY_221_CLOSING_CONTROL_CONNECTION,
82                  "System shutdown");
83          getSession().getNetworkHandler().writeIntermediateAnswer().addListener(
84                  new ShutdownChannelFutureListener(getConfiguration()));
85      }
86  
87  }