KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > ftp > FTPRequest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.ftp;
18
19 /**
20  * FTP Request Class
21  * <p>
22  * Contains the details of an FTP request
23  *
24  * @author GKSpencer
25  */

26 public class FTPRequest
27 {
28
29     // FTP command id
30

31     private int m_cmd;
32
33     // Command argument
34

35     private String JavaDoc m_arg;
36
37     /**
38      * Default constructor
39      */

40     public FTPRequest()
41     {
42         m_cmd = FTPCommand.InvalidCmd;
43     }
44
45     /**
46      * Class constructor
47      *
48      * @param cmd int
49      * @param arg String
50      */

51     public FTPRequest(int cmd, String JavaDoc arg)
52     {
53         m_cmd = cmd;
54         m_arg = arg;
55     }
56
57     /**
58      * Class constructor
59      *
60      * @param cmdLine String
61      */

62     public FTPRequest(String JavaDoc cmdLine)
63     {
64
65         // Parse the FTP command record
66

67         parseCommandLine(cmdLine);
68     }
69
70     /**
71      * Return the command index
72      *
73      * @return int
74      */

75     public final int isCommand()
76     {
77         return m_cmd;
78     }
79
80     /**
81      * Check if the request has an argument
82      *
83      * @return boolean
84      */

85     public final boolean hasArgument()
86     {
87         return m_arg != null ? true : false;
88     }
89
90     /**
91      * Return the request argument
92      *
93      * @return String
94      */

95     public final String JavaDoc getArgument()
96     {
97         return m_arg;
98     }
99
100     /**
101      * Set the command line for the request
102      *
103      * @param cmdLine String
104      * @return int
105      */

106     public final int setCommandLine(String JavaDoc cmdLine)
107     {
108
109         // Reset the current values
110

111         m_cmd = FTPCommand.InvalidCmd;
112         m_arg = null;
113
114         // Parse the new command line
115

116         parseCommandLine(cmdLine);
117         return isCommand();
118     }
119
120     /**
121      * Parse a command string
122      *
123      * @param cmdLine String
124      */

125     protected final void parseCommandLine(String JavaDoc cmdLine)
126     {
127
128         // Check if the command has an argument
129

130         int pos = cmdLine.indexOf(' ');
131         String JavaDoc cmd = null;
132
133         if (pos != -1)
134         {
135             cmd = cmdLine.substring(0, pos);
136             m_arg = cmdLine.substring(pos + 1);
137         }
138         else
139             cmd = cmdLine;
140
141         // Validate the FTP command
142

143         m_cmd = FTPCommand.getCommandId(cmd);
144     }
145
146     /**
147      * Update the command argument
148      *
149      * @param arg String
150      */

151     protected final void updateArgument(String JavaDoc arg)
152     {
153         m_arg = arg;
154     }
155     
156     /**
157      * Return the request as a string
158      *
159      * @return String
160      */

161     public String JavaDoc toString()
162     {
163         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
164         
165         str.append("[");
166         str.append(FTPCommand.getCommandName(m_cmd));
167         str.append(":");
168         str.append(m_arg);
169         str.append("]");
170         
171         return str.toString();
172     }
173 }
174
Popular Tags