Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class FTP1server {
  5.  
  6. /*
  7. The problem: To read in FTP commands and decide if they are valid, given a set of rules.
  8.  
  9. Approach: Using StringTokenizer, decide which command is being passed through and use the rules given for each
  10. to see if it is a valid FTP command. For USER and PASS, first strip the tokens of the whitespace, then do the
  11. checks to determine validity. For TYPE, use the rules to check for validity while also checking the tokens for
  12. A or I. SYST, QUIT, and NOOP check for validity using the rules. Used a parse method to perform all of the checks,
  13. then used BufferedReader to read in the input.
  14.  
  15. */
  16.  
  17. // Method to parse the inputed string
  18. static void parse(String input) {
  19.  
  20. // Current token
  21. String token = "";
  22. // Declaring and instantiating the StringTokenizer for the input, which is the variable input
  23. StringTokenizer tokenizedLine = new StringTokenizer(input, " \n\r", true);
  24.  
  25. // Checking to see if there are more tokens in the string
  26. if(tokenizedLine.hasMoreTokens()) {
  27. token = tokenizedLine.nextToken();
  28.  
  29. // Checking to see if the command is valid (user)
  30. if(token.equalsIgnoreCase("user")) {
  31. if(tokenizedLine.hasMoreTokens()) {
  32. token = tokenizedLine.nextToken();
  33.  
  34. if(!token.equals(" ")) {
  35. System.out.println("ERROR -- command");
  36. return;
  37. }
  38. // Stripping the white spaces after USER
  39. while(token.equals(" ")) {
  40. token = tokenizedLine.nextToken();
  41. }
  42.  
  43. // Creating a username variable to allow for spaces in the username
  44. String user = "";
  45. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  46. user += token;
  47. token = tokenizedLine.nextToken();
  48. }
  49.  
  50. // Creating a character array and looping through each to check if each is an ASCII character
  51. char[] userChar = user.toCharArray();
  52. for(int x = 0; x < userChar.length; x++) {
  53. if((int) userChar[x] <= 127 && (int) userChar[x] >= 0) {
  54. continue;
  55. } else {
  56. System.out.println("ERROR -- username");
  57. return;
  58. }
  59. }
  60.  
  61. // Checking after the username is entered
  62.  
  63.  
  64. if(token.equals("\r")) {
  65. if(tokenizedLine.hasMoreTokens()) {
  66. if(tokenizedLine.nextToken().equals("\n")) {
  67. if(user.length() > 0) {
  68. System.out.println("Command ok");
  69. return;
  70. } else {
  71. System.out.println("ERROR -- username");
  72. return;
  73. }
  74. } else {
  75. // If \r\n isn't detected, it is a CRLF error
  76. System.out.println("ERROR -- CRLF");
  77. return;
  78. }
  79. } else {
  80. // If there are no more tokens after \r, it is a CRLF error
  81. System.out.println("ERROR -- CRLF");
  82. return;
  83. }
  84. } else {
  85. // If there is no \r, that means there was never a CRLF so it gives an error.
  86. // If there is just an \n, it is a command error
  87. // if(token.equals("\n")) {
  88. // System.out.println("ERROR -- command");
  89. // return;
  90. // } else {
  91. System.out.println("ERROR -- CRLF");
  92. return;
  93. //}
  94. }
  95. } else {
  96. // If there are no tokens entered after USER, it is a command error
  97. System.out.println("ERROR -- command");
  98. return;
  99. }
  100. }
  101.  
  102. // Checking to see if the command is valid (password)
  103. else if(token.equalsIgnoreCase("pass")) {
  104. if(tokenizedLine.hasMoreTokens()) {
  105. token = tokenizedLine.nextToken();
  106.  
  107. if(!token.equals(" ")) {
  108. System.out.println("ERROR -- command");
  109. return;
  110. }
  111. // Stripping the white spaces after PASS
  112. while(token.equals(" ")) {
  113. token = tokenizedLine.nextToken();
  114. }
  115.  
  116. // Creating a password variable to allow for spaces in the password
  117. String pass = "";
  118. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  119. pass += token;
  120. token = tokenizedLine.nextToken();
  121. }
  122.  
  123. // Creating a character array and looping through each to check if each is an ASCII character
  124. char[] passChar = pass.toCharArray();
  125. for(int x = 0; x < passChar.length; x++) {
  126. if((int) passChar[x] <= 127 && (int) passChar[x] >= 0) {
  127. continue;
  128. } else {
  129. System.out.println("ERROR -- password");
  130. return;
  131. }
  132. }
  133.  
  134. // Checking after the password is entered
  135. if(token.equals("\r")) {
  136. if(tokenizedLine.hasMoreTokens()) {
  137. if(tokenizedLine.nextToken().equals("\n")) {
  138. if(pass.length() > 0) {
  139. System.out.println("Command ok");
  140. return;
  141. } else {
  142. System.out.println("ERROR -- password");
  143. return;
  144. }
  145. } else {
  146. // If \r\n isn't detected, it is a CRLF error
  147. System.out.println("ERROR -- CRLF");
  148. return;
  149. }
  150. } else {
  151. // If there are no more tokens after \r, it is a CRLF error
  152. System.out.println("ERROR -- CRLF");
  153. return;
  154. }
  155. } else {
  156. // // If there is no \r, that means there was never a CRLF so it gives an error.
  157. // // If there is just an \n, it is a command error
  158. // if(token.equals("\n")) {
  159. // System.out.println("ERROR -- command");
  160. // return;
  161. // } else {
  162. System.out.println("ERROR -- CRLF");
  163. return;
  164. //}
  165. }
  166. } else {
  167. // If there are no tokens entered after PASS, it is a command error
  168. System.out.println("ERROR -- command");
  169. return;
  170. }
  171. }
  172.  
  173. // Checking to see if the command is valid (type)
  174. else if (token.equalsIgnoreCase("type")) {
  175. token = tokenizedLine.nextToken();
  176.  
  177. if(!token.equals(" ")) {
  178. System.out.println("ERROR -- command");
  179. return;
  180. }
  181. // Trimming the spaces
  182. while (token.equals(" ")) {
  183. token = tokenizedLine.nextToken();
  184. }
  185.  
  186. if (tokenizedLine.hasMoreTokens()) {
  187. // Checking if the token is A or I
  188. if (token.equals("A") || token.equals("I")) {
  189. if (tokenizedLine.hasMoreTokens()) {
  190. if (tokenizedLine.nextToken().equals("\r")) {
  191. if (tokenizedLine.hasMoreTokens()) {
  192. if (tokenizedLine.nextToken().equals("\n")) {
  193. System.out.println("Command ok");
  194. return;
  195. }
  196. else {
  197. // If there is no \n after the \r
  198. System.out.println("ERROR -- CRLF");
  199. return;
  200. }
  201. }
  202. else {
  203. // If there is no \r
  204. System.out.println("ERROR -- CRLF");
  205. return;
  206. }
  207. }
  208. else {
  209. // If it doesn't end in \r\n
  210. System.out.println("ERROR -- CRLF");
  211. return;
  212. }
  213. }
  214. else {
  215. // If there is no CRLF
  216. System.out.println("ERROR -- command");
  217. return;
  218. }
  219. }
  220. else {
  221. // If the type after is not A or I
  222. System.out.println("ERROR -- type-code");
  223. return;
  224. }
  225. }
  226. else {
  227. // If the command is wrong
  228. System.out.println("ERROR -- command");
  229. return;
  230. }
  231. }
  232.  
  233. // Checking to see if command is valid (NOOP, QUIT, and SYST)
  234. else if(token.equalsIgnoreCase("noop") || token.equalsIgnoreCase("quit") || token.equalsIgnoreCase("syst")) {
  235. // Checking to see if the CRLF characters are after the command
  236. if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\r")) {
  237. if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\n")) {
  238. System.out.println("Command ok");
  239. return;
  240. } else {
  241. System.out.println("ERROR -- CRLF");
  242. return;
  243. }
  244. } else {
  245. System.out.println("ERROR -- CRLF");
  246. return;
  247. }
  248. } else {
  249. System.out.println("ERROR -- command");
  250. return;
  251. }
  252. }
  253. }
  254.  
  255. public static void main(String[] args) throws IOException {
  256. // Create a buffered reader to read in the lines
  257. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  258. // Store the lines in an ArrayList
  259. ArrayList<String> nexts = new ArrayList<String>();
  260. String next = "";
  261. int i = 0;
  262. i = input.read();
  263.  
  264. // While the buffer is still open
  265. while(i != -1) {
  266. // Cast the integer to a char
  267. next += (char) i;
  268. if((char) i == '\r') {
  269. // While the newline isn't encountered, add to the list, reset
  270. i = input.read();
  271. if((char) i == '\n') {
  272. next += (char) i;
  273. nexts.add(next);
  274. next = "";
  275. i = input.read();
  276. } else {
  277. nexts.add(next);
  278. next = "";
  279. }
  280.  
  281. } else if((char) i =='\n') {
  282. nexts.add(next);
  283. next = "";
  284. i = input.read();
  285. } else {
  286. i = input.read();
  287. }
  288. }
  289.  
  290. for(int j = 0; j < nexts.size(); j++) {
  291. // Parsing each
  292. next = nexts.get(j);
  293. System.out.print(next);
  294. parse(next);
  295. }
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement