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 org.waarp.common.command.ReplyCode;
21 import org.waarp.common.command.exception.Reply500Exception;
22 import org.waarp.common.command.exception.Reply501Exception;
23 import org.waarp.ftp.core.command.AbstractCommand;
24
25 /**
26 * Internal limit bandwidth command that will change the global limit bandwidth
27 *
28 * @author Frederic Bregier
29 *
30 */
31 public class LIMITBANDWIDTH extends AbstractCommand {
32 @Override
33 public void exec() throws Reply501Exception, Reply500Exception {
34 if (!getSession().getAuth().isAdmin()) {
35 // not admin
36 throw new Reply500Exception("Command Not Allowed");
37 }
38 if (!hasArg()) {
39 // reset to default
40 getConfiguration().changeNetworkLimit(0, 0);
41 getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
42 "Limit reset to default");
43 return;
44 }
45 String[] limits = getArgs();
46 long writeLimit = 0;
47 long readLimit = 0;
48 try {
49 if (limits.length == 1) {
50 writeLimit = Long.parseLong(limits[0]);
51 readLimit = writeLimit;
52 } else {
53 writeLimit = Long.parseLong(limits[0]);
54 readLimit = Long.parseLong(limits[1]);
55 }
56 } catch (NumberFormatException e) {
57 throw new Reply501Exception(getCommand() +
58 " ([write and read limits in b/s] | [write limit in b/s] [read limit in b/s]");
59 }
60 getConfiguration().changeNetworkLimit(writeLimit, readLimit);
61 getSession().setReplyCode(ReplyCode.REPLY_200_COMMAND_OKAY,
62 "Limit set to new values");
63 }
64
65 }