KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > command > impl > ftp > UserCommand


1 /**
2  * Command USER.
3  * See FTP spec for details on the command.
4  */

5 package com.coldcore.coloradoftp.command.impl.ftp;
6
7 import com.coldcore.coloradoftp.command.Reply;
8 import com.coldcore.coloradoftp.command.impl.AbstractCommand;
9 import com.coldcore.coloradoftp.session.LoginState;
10 import com.coldcore.coloradoftp.session.Session;
11 import com.coldcore.coloradoftp.session.SessionAttributeName;
12 import org.apache.log4j.Logger;
13
14 public class UserCommand extends AbstractCommand {
15
16   private static Logger log = Logger.getLogger(UserCommand.class);
17   private boolean anonymous;
18
19
20   public UserCommand() {
21     anonymous = true;
22   }
23
24
25   public boolean isAnonymous() {
26     return anonymous;
27   }
28
29
30   public void setAnonymous(boolean anonymous) {
31     this.anonymous = anonymous;
32   }
33
34
35   public Reply execute() {
36     Reply reply = getReply();
37
38     String JavaDoc username = getParameter();
39     username = username.toLowerCase();
40
41     Session session = getConnection().getSession();
42     LoginState loginState = (LoginState) session.getAttribute(SessionAttributeName.LOGIN_STATE);
43     if (loginState != null) {
44       log.debug("Already logged in user submits USER command again");
45       reply.setCode("503");
46       reply.setText("Already logged in.");
47       return reply;
48     }
49
50     session.removeAttribute(SessionAttributeName.USERNAME);
51
52     if (username.length() == 0) {
53       log.debug("Invalid syntax of submitted username");
54       reply.setCode("501");
55       reply.setText("Send your user name.");
56       return reply;
57     }
58
59     if (username.equals("anonymous") && !anonymous) {
60       log.debug("Anonymous login is disabled");
61       reply.setCode("332");
62       reply.setText("Anonymous login disabled, need account for login.");
63       return reply;
64     }
65
66     session.setAttribute(SessionAttributeName.USERNAME, username);
67     log.debug("Accepted username: "+username);
68
69     if (username.equals("anonymous")) {
70       reply.setCode("331");
71       reply.setText("Guest login okay, send your complete e-mail address as password.");
72       return reply;
73     }
74
75     reply.setCode("331");
76     reply.setText("User name okay, need password.");
77     return reply;
78   }
79 }
80
Popular Tags