View Javadoc

1   /**
2    * Copyright 2009, Frederic Bregier, and individual contributors
3    * by the @author tags. See the COPYRIGHT.txt in the distribution for a
4    * full listing of individual contributors.
5    *
6    * This is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 3.0 of
9    * the License, or (at your option) any later version.
10   *
11   * This software is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this software; if not, write to the Free
18   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20   */
21  package goldengate.ftp.exec.config;
22  
23  import java.io.File;
24  
25  import goldengate.common.command.ReplyCode;
26  import goldengate.common.command.exception.CommandAbstractException;
27  import goldengate.common.command.exception.Reply500Exception;
28  import goldengate.common.command.exception.Reply501Exception;
29  import goldengate.common.logging.GgInternalLogger;
30  import goldengate.common.logging.GgInternalLoggerFactory;
31  import goldengate.ftp.core.command.AbstractCommand;
32  
33  /**
34   * AUTHTUPDATE command: implements the command that will try to update the authentications
35   * from the file given as argument or the original one if no argument is given.<br>
36   * Two optional arguments exist:<br>
37   * - PURGE: empty first the current authentications before applying the update<br>
38   * - SAVE: save the final authentications on the original name given at startup.<br>
39   *
40   * @author Frederic Bregier
41   *
42   */
43  public class AUTHUPDATE extends AbstractCommand {
44      /**
45       * Internal Logger
46       */
47      private static final GgInternalLogger logger = GgInternalLoggerFactory
48              .getLogger(AUTHUPDATE.class);
49  
50      /* (non-Javadoc)
51       * @see goldengate.common.command.CommandInterface#exec()
52       */
53      @Override
54      public void exec() throws CommandAbstractException {
55          if (!getSession().getAuth().isAdmin()) {
56              // not admin
57              throw new Reply500Exception("Command Not Allowed");
58          }
59          String filename = null;
60          boolean purge = false;
61          boolean write = false;
62          if (!hasArg()) {
63              filename = ((FileBasedConfiguration) getConfiguration()).authenticationFile;
64          } else {
65              String[] authents = getArgs();
66              for (int i = 0; i < authents.length; i++) {
67                  if (authents[i].equalsIgnoreCase("PURGE")) {
68                      purge = true;
69                  } else if (authents[i].equalsIgnoreCase("SAVE")) {
70                      write = true;
71                  } else if (filename == null) {
72                      filename = authents[i];
73                  }
74              }
75              if (filename == null) {
76                  filename = ((FileBasedConfiguration) getConfiguration()).authenticationFile;
77              }
78              File file = new File(filename);
79              if (! file.canRead()) {
80                  throw new Reply501Exception("Filename given as parameter is not found: "+filename);
81              }
82          }
83          if (! ((FileBasedConfiguration) getConfiguration()).initializeAuthent(filename, purge)) {
84              throw new Reply501Exception("Filename given as parameter is not correct");
85          }
86          if (write) {
87              if (! ((FileBasedConfiguration) getConfiguration()).
88                      saveAuthenticationFile(
89                              ((FileBasedConfiguration) getConfiguration()).authenticationFile)) {
90                  throw new Reply501Exception("Update is done but Write operation is not correct");
91              }
92          }
93          logger.warn("Authentication was updated from "+filename);
94          getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
95              "Authentication is updated");
96      }
97  
98  }