Advertisement
Guest User

allie

a guest
Feb 2nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.99 KB | None | 0 0
  1. package assignment2;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.nio.file.StandardCopyOption;
  7. import java.util.*;
  8.  
  9. public class FTPserver2 {
  10.  
  11. /*
  12. The problem: To read in FTP commands and decide if they are valid, given a set of rules.
  13.  
  14. Approach: Using StringTokenizer, decide which command is being passed through and use the rules given for each
  15. to see if it is a valid FTP command. For USER and PASS, first strip the tokens of the whitespace, then do the
  16. checks to determine validity. For TYPE, use the rules to check for validity while also checking the tokens for
  17. A or I. SYST, QUIT, and NOOP check for validity using the rules. Used a parse method to perform all of the checks,
  18. then used BufferedReader to read in the input.
  19.  
  20. */
  21.  
  22. static int retrCount = 0;
  23.  
  24. // Method to parse the inputed string
  25. static void parse(String input) {
  26.  
  27. // Current token
  28. String token = "";
  29. // Declaring and instantiating the StringTokenizer for the input, which is the variable input
  30. StringTokenizer tokenizedLine = new StringTokenizer(input, " \n\r", true);
  31.  
  32. // Checking to see if there are more tokens in the string
  33. if(tokenizedLine.hasMoreTokens()) {
  34. token = tokenizedLine.nextToken();
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. // Checking to see if the command is valid (user)
  45. if(token.equalsIgnoreCase("user")) {
  46. if(tokenizedLine.hasMoreTokens()) {
  47. token = tokenizedLine.nextToken();
  48.  
  49. if(!token.equals(" ")) {
  50. System.out.println("ERROR -- command");
  51. return;
  52. }
  53. // Stripping the white spaces after USER
  54. while(token.equals(" ")) {
  55. token = tokenizedLine.nextToken();
  56. }
  57.  
  58. // Creating a username variable to allow for spaces in the username
  59. String user = "";
  60. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  61. user += token;
  62. token = tokenizedLine.nextToken();
  63. }
  64.  
  65. // Creating a character array and looping through each to check if each is an ASCII character
  66. char[] userChar = user.toCharArray();
  67. for(int x = 0; x < userChar.length; x++) {
  68. if((int) userChar[x] <= 127 && (int) userChar[x] >= 0) {
  69. continue;
  70. } else {
  71. System.out.println("ERROR -- username");
  72. return;
  73. }
  74. }
  75.  
  76. // Checking after the username is entered
  77.  
  78.  
  79. if(token.equals("\r")) {
  80. if(tokenizedLine.hasMoreTokens()) {
  81. if(tokenizedLine.nextToken().equals("\n")) {
  82. if(user.length() > 0) {
  83. System.out.println("Command ok");
  84. return;
  85. } else {
  86. System.out.println("ERROR -- username");
  87. return;
  88. }
  89. } else {
  90. // If \r\n isn't detected, it is a CRLF error
  91. System.out.println("ERROR -- CRLF");
  92. return;
  93. }
  94. } else {
  95. // If there are no more tokens after \r, it is a CRLF error
  96. System.out.println("ERROR -- CRLF");
  97. return;
  98. }
  99. } else {
  100. System.out.println("ERROR -- CRLF");
  101. return;
  102. //}
  103. }
  104. } else {
  105. // If there are no tokens entered after USER, it is a command error
  106. System.out.println("ERROR -- command");
  107. return;
  108. }
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. // Checking to see if the command is valid (password)
  119. else if(token.equalsIgnoreCase("pass")) {
  120. if(tokenizedLine.hasMoreTokens()) {
  121. token = tokenizedLine.nextToken();
  122.  
  123. if(!token.equals(" ")) {
  124. System.out.println("ERROR -- command");
  125. return;
  126. }
  127. // Stripping the white spaces after PASS
  128. while(token.equals(" ")) {
  129. token = tokenizedLine.nextToken();
  130. }
  131.  
  132. // Creating a password variable to allow for spaces in the password
  133. String pass = "";
  134. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  135. pass += token;
  136. token = tokenizedLine.nextToken();
  137. }
  138.  
  139. // Creating a character array and looping through each to check if each is an ASCII character
  140. char[] passChar = pass.toCharArray();
  141. for(int x = 0; x < passChar.length; x++) {
  142. if((int) passChar[x] <= 127 && (int) passChar[x] >= 0) {
  143. continue;
  144. } else {
  145. System.out.println("ERROR -- password");
  146. return;
  147. }
  148. }
  149.  
  150. // Checking after the password is entered
  151. if(token.equals("\r")) {
  152. if(tokenizedLine.hasMoreTokens()) {
  153. if(tokenizedLine.nextToken().equals("\n")) {
  154. if(pass.length() > 0) {
  155. System.out.println("Command ok");
  156. return;
  157. } else {
  158. System.out.println("ERROR -- password");
  159. return;
  160. }
  161. } else {
  162. // If \r\n isn't detected, it is a CRLF error
  163. System.out.println("ERROR -- CRLF");
  164. return;
  165. }
  166. } else {
  167. // If there are no more tokens after \r, it is a CRLF error
  168. System.out.println("ERROR -- CRLF");
  169. return;
  170. }
  171. } else {
  172. System.out.println("ERROR -- CRLF");
  173. return;
  174. //}
  175. }
  176. } else {
  177. // If there are no tokens entered after PASS, it is a command error
  178. System.out.println("ERROR -- command");
  179. return;
  180. }
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. // Checking to see if the command is valid (type)
  192. else if (token.equalsIgnoreCase("type")) {
  193. token = tokenizedLine.nextToken();
  194.  
  195. if(!token.equals(" ")) {
  196. System.out.println("ERROR -- command");
  197. return;
  198. }
  199. // Trimming the spaces
  200. while (token.equals(" ")) {
  201. token = tokenizedLine.nextToken();
  202. }
  203.  
  204. if (tokenizedLine.hasMoreTokens()) {
  205. // Checking if the token is A or I
  206. if (token.equals("A") || token.equals("I")) {
  207. if (tokenizedLine.hasMoreTokens()) {
  208. if (tokenizedLine.nextToken().equals("\r")) {
  209. if (tokenizedLine.hasMoreTokens()) {
  210. if (tokenizedLine.nextToken().equals("\n")) {
  211. System.out.println("Command ok");
  212. return;
  213. }
  214. else {
  215. // If there is no \n after the \r
  216. System.out.println("ERROR -- CRLF");
  217. return;
  218. }
  219. }
  220. else {
  221. // If there is no \r
  222. System.out.println("ERROR -- CRLF");
  223. return;
  224. }
  225. }
  226. else {
  227. // If it doesn't end in \r\n
  228. System.out.println("ERROR -- CRLF");
  229. return;
  230. }
  231. }
  232. else {
  233. // If there is no CRLF
  234. System.out.println("ERROR -- command");
  235. return;
  236. }
  237. }
  238. else {
  239. // If the type after is not A or I
  240. System.out.println("ERROR -- type-code");
  241. return;
  242. }
  243. }
  244. else {
  245. // If the command is wrong
  246. System.out.println("ERROR -- command");
  247. return;
  248. }
  249. }
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. else if(token.equalsIgnoreCase("retr")) {
  261. if(tokenizedLine.hasMoreTokens()) {
  262. token = tokenizedLine.nextToken();
  263.  
  264. if(!token.equals(" ")) {
  265. System.out.println("ERROR -- command");
  266. return;
  267. }
  268. // Stripping the white spaces after USER
  269. while(token.equals(" ")) {
  270. token = tokenizedLine.nextToken();
  271. }
  272.  
  273. while(token.charAt(0) == '/' || token.charAt(0) == '\\') {
  274. token = tokenizedLine.nextToken();
  275. }
  276.  
  277. // Creating a username variable to allow for spaces in the username
  278. String pathName = "";
  279. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  280. pathName += token;
  281. token = tokenizedLine.nextToken();
  282. }
  283.  
  284. // Creating a character array and looping through each to check if each is an ASCII character
  285. char[] pathChar= pathName.toCharArray();
  286. for(int x = 0; x < pathChar.length; x++) {
  287. if((int) pathChar[x] <= 127 && (int) pathChar[x] >= 0) {
  288. continue;
  289. } else {
  290. System.out.println("ERROR -- username");
  291. return;
  292. }
  293. }
  294.  
  295. // Checking after the username is entered
  296.  
  297.  
  298. if(token.equals("\r")) {
  299. if(tokenizedLine.hasMoreTokens()) {
  300. if(tokenizedLine.nextToken().equals("\n")) {
  301. if(pathName.length() > 0) {
  302. System.out.println("Command ok");
  303. retrCount++;
  304. try {
  305. processRetr(pathName);
  306. } catch (IOException e) {
  307. e.printStackTrace();
  308. }
  309. return;
  310. } else {
  311. System.out.println("ERROR -- username");
  312. return;
  313. }
  314. } else {
  315. // If \r\n isn't detected, it is a CRLF error
  316. System.out.println("ERROR -- CRLF");
  317. return;
  318. }
  319. } else {
  320. // If there are no more tokens after \r, it is a CRLF error
  321. System.out.println("ERROR -- CRLF");
  322. return;
  323. }
  324. } else {
  325. System.out.println("ERROR -- CRLF");
  326. return;
  327. //}
  328. }
  329. } else {
  330. // If there are no tokens entered after USER, it is a command error
  331. System.out.println("ERROR -- command");
  332. return;
  333. }
  334. }
  335.  
  336.  
  337.  
  338.  
  339. else if(token.equalsIgnoreCase("port")) {
  340. if(tokenizedLine.hasMoreTokens()) {
  341. token = tokenizedLine.nextToken();
  342.  
  343. if(!token.equals(" ")) {
  344. System.out.println("ERROR -- command");
  345. return;
  346. }
  347. // Stripping the white spaces after USER
  348. while(token.equals(" ")) {
  349. token = tokenizedLine.nextToken();
  350. }
  351.  
  352. while(token.equals("/") && token.equals("\\")) {
  353. token = tokenizedLine.nextToken();
  354. }
  355.  
  356. // Creating a username variable to allow for spaces in the username
  357. String hostPort = "";
  358. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  359. hostPort += token;
  360. token = tokenizedLine.nextToken();
  361. }
  362.  
  363.  
  364.  
  365. // Creating a character array and looping through each to check if each is an ASCII character
  366. char[] hostPortChar= hostPort.toCharArray();
  367. int ha1 = 0;
  368. int ha2 = 0;
  369. int ha3 = 0;
  370. int ha4 = 0;
  371. int pa1 = 0;
  372. int pa2 = 0;
  373. String sa1 = "";
  374. String sa2 = "";
  375. String sa3 = "";
  376. String sa4 = "";
  377. int count = 0;
  378. for(int x = 0; x < hostPortChar.length; x++) {
  379. if(hostPortChar[x] == ',') {
  380. count++;
  381.  
  382. if(count == 0) {
  383. if(ha1 <= 255 && ha1 >= 0) {
  384. continue;
  385. } else {
  386. //TODO Error in host port number deal with it later
  387. }
  388. } else if(count == 1) {
  389. if(ha2 <= 255 && ha2 >= 0) {
  390. continue;
  391. } else {
  392. //TODO Error in host port number deal with it later
  393. }
  394. } else if(count == 2) {
  395. if(ha3 <= 255 && ha3 >= 0) {
  396. continue;
  397. } else {
  398. //TODO Error in host port number deal with it later
  399. }
  400. } else if(count == 3) {
  401. if(ha4 <= 255 && ha4 >= 0) {
  402. continue;
  403. } else {
  404. //TODO Error in host port number deal with it later
  405. }
  406. } else if (count == 4) {
  407. if(pa1 <= 255 && pa1 >= 0) {
  408. continue;
  409. } else {
  410. //TODO Error in host port number deal with it later
  411. }
  412. } else if(count == 5) {
  413. if(pa2 <= 255 && pa2 >= 0) {
  414. token = tokenizedLine.nextToken();
  415. if(token.equals("\r")) {
  416. if(tokenizedLine.hasMoreTokens()) {
  417. if(tokenizedLine.nextToken().equals("\n")) {
  418. processPort(sa1, sa2, sa3, sa4, pa1, pa2);
  419. //TODO print ya know
  420. return;
  421. } else {
  422. //TODO change this
  423. System.out.println("ERROR -- hostport");
  424. return;
  425. }
  426. } else {
  427. // If \r\n isn't detected, it is a CRLF error
  428. System.out.println("ERROR -- CRLF");
  429. return;
  430. }
  431. } else {
  432. // If there are no more tokens after \r, it is a CRLF error
  433. System.out.println("ERROR -- CRLF");
  434. return;
  435. }
  436.  
  437. } else {
  438. //TODO error in hostport
  439. return;
  440. }
  441. } else {
  442. //TODO Error in host port number deal with it later
  443. }
  444.  
  445.  
  446. } else {
  447. if(count == 0) {
  448. ha1 += (int) hostPortChar[x];
  449. sa1 += hostPortChar[x];
  450. } else if(count == 1) {
  451. ha2 += (int) hostPortChar[x];
  452. sa2 += hostPortChar[x];
  453. } else if(count == 2) {
  454. ha3 += (int) hostPortChar[x];
  455. sa3 += hostPortChar[x];
  456. } else if(count == 3) {
  457. ha4 += (int) hostPortChar[x];
  458. sa4 += hostPortChar[x];
  459. } else if (count == 4) {
  460. pa1 += (int) hostPortChar[x];
  461. } else if(count == 5) {
  462. pa2 += (int) hostPortChar[x];
  463. } else {
  464. System.out.println("Too many/too few variables");
  465. }
  466.  
  467. }
  468. }
  469. }
  470. }
  471.  
  472.  
  473. // Checking to see if command is valid (NOOP, QUIT, and SYST)
  474. else if(token.equalsIgnoreCase("noop") || token.equalsIgnoreCase("quit") || token.equalsIgnoreCase("syst")) {
  475. // Checking to see if the CRLF characters are after the command
  476. if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\r")) {
  477. if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\n")) {
  478. System.out.println("Command ok");
  479. return;
  480. } else {
  481. System.out.println("ERROR -- CRLF");
  482. return;
  483. }
  484. } else {
  485. System.out.println("ERROR -- CRLF");
  486. return;
  487. }
  488. } else {
  489. System.out.println("ERROR -- command");
  490. return;
  491. }
  492. }
  493. }
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502. private static void processPort(String ha1, String ha2, String ha3, String ha4, int pa1, int pa2) {
  503. String hostAddress = ha1 + "." + ha2 + "." + ha3 + "." + ha4 + ",";
  504. int portNumber = (pa1 * 265) + pa2;
  505.  
  506. }
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515. private static void processRetr(String str) throws IOException {
  516. String file = "retr_files/file" + retrCount;
  517. FileInputStream in = null;
  518. FileOutputStream out = null;
  519.  
  520. try {
  521. in = new FileInputStream(str);
  522. out = new FileOutputStream(file);
  523.  
  524. int x;
  525.  
  526. while((x = in.read()) != -1) {
  527. out.write(x);
  528. }
  529.  
  530. if(in != null) {
  531. in.close();
  532. }
  533.  
  534. if(out != null) {
  535. out.close();
  536. }
  537. } catch(IOException e) {
  538. e.printStackTrace();
  539. }
  540. }
  541.  
  542.  
  543.  
  544.  
  545.  
  546. public static void main(String[] args) throws IOException {
  547. // Create a buffered reader to read in the lines
  548. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  549. // Store the lines in an ArrayList
  550. ArrayList<String> nexts = new ArrayList<String>();
  551. String next = "";
  552. int i = 0;
  553. i = input.read();
  554.  
  555. // While the buffer is still open
  556. while(i != -1) {
  557. // Cast the integer to a char
  558. next += (char) i;
  559. if((char) i == '\r') {
  560. // While the newline isn't encountered, add to the list, reset
  561. i = input.read();
  562. if((char) i == '\n') {
  563. next += (char) i;
  564. nexts.add(next);
  565. next = "";
  566. i = input.read();
  567. } else {
  568. nexts.add(next);
  569. next = "";
  570. }
  571.  
  572. } else if((char) i =='\n') {
  573. nexts.add(next);
  574. next = "";
  575. i = input.read();
  576. } else {
  577. i = input.read();
  578. }
  579. }
  580.  
  581. for(int j = 0; j < nexts.size(); j++) {
  582. // Parsing each
  583. next = nexts.get(j);
  584. System.out.print(next);
  585. parse(next);
  586. }
  587. }
  588. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement