Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. package assignment2;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class FTPserver2 {
  7.  
  8.  
  9. static int isValidUser = 0;
  10. static int isUPPairValid = 0;
  11. static int isPortValid = 0;
  12. static int fileCount = 0;
  13.  
  14.  
  15.  
  16.  
  17. static void parse(String input) {
  18. String token;
  19.  
  20.  
  21. StringTokenizer tokenizedLine = new StringTokenizer(input, " \r\n", true);
  22.  
  23.  
  24. if (tokenizedLine.hasMoreTokens()) {
  25. token = tokenizedLine.nextToken();
  26.  
  27.  
  28. if (token.equalsIgnoreCase("user")) {
  29. if (tokenizedLine.hasMoreTokens()) {
  30. token = tokenizedLine.nextToken();
  31.  
  32. String user = "";
  33. while (token.equals(" ")) {
  34. user += token;
  35. token = tokenizedLine.nextToken();
  36. }
  37.  
  38.  
  39. while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  40. user += token;
  41. token = tokenizedLine.nextToken();
  42. }
  43.  
  44.  
  45. char[] userChar = user.toCharArray();
  46. for (int i = 0; i < userChar.length; i++) {
  47. if ((int) userChar[i] <= 127) {
  48. continue;
  49. }
  50. else {
  51. System.out.println("501 Syntax error in parameter.");
  52. return;
  53. }
  54. }
  55.  
  56.  
  57. if (token.equals("\r")) {
  58. if (tokenizedLine.hasMoreTokens()) {
  59. if (tokenizedLine.nextToken().equals("\n")) {
  60. if (user.equals(" ")) {
  61. System.out.println("501 Syntax error in parameter.");
  62. return;
  63. }
  64. else if (user.length() > 0 && isValidUser == 0) {
  65. System.out.println("331 Guest access OK, send password.");
  66. isValidUser = 1;
  67. }
  68. else if (user.length() > 0 && isValidUser == 1) {
  69. System.out.println("503 Bad sequence of commands.");
  70. }
  71. else {
  72. System.out.println("500 Syntax error, command unrecognized.");
  73. return;
  74. }
  75. }
  76.  
  77. else {
  78. System.out.println("501 Syntax error in parameter.");
  79. return;
  80. }
  81. }
  82. else {
  83. System.out.println("501 Syntax error in parameter.");
  84. return;
  85. }
  86. }
  87. else {
  88. if (token.equals("\n")) {
  89. System.out.println("501 Syntax error in parameter.");
  90. return;
  91. }
  92. else {
  93. System.out.println("501 Syntax error in parameter.");
  94. return;
  95. }
  96. }
  97. }
  98. else {
  99. System.out.println("500 Syntax error, command unrecognized.");
  100. }
  101. }
  102.  
  103. else if (token.equalsIgnoreCase("pass")) {
  104. if (tokenizedLine.hasMoreTokens()) {
  105. token = tokenizedLine.nextToken();
  106.  
  107. String pass = "";
  108. while (token.equals(" ")) {
  109. pass += token;
  110. token = tokenizedLine.nextToken();
  111. }
  112.  
  113.  
  114. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  115. pass += token;
  116. token = tokenizedLine.nextToken();
  117. }
  118.  
  119. char[] passChar = pass.toCharArray();
  120. for (int i = 0; i < passChar.length; i++) {
  121. if ((int) passChar[i] <= 127) {
  122. continue;
  123. }
  124. else {
  125. System.out.println("501 Syntax error in parameter.");
  126. return;
  127. }
  128. }
  129.  
  130. if (token.equals("\r")) {
  131. if (tokenizedLine.hasMoreTokens()) {
  132. if (tokenizedLine.nextToken().equals("\n")) {
  133. if (pass.equals(" ")) {
  134. System.out.println("501 Syntax error in parameter.");
  135. return;
  136. }
  137. else if (pass.length() > 0) {
  138. if (isValidUser == 1) {
  139. System.out.println("230 Guest login OK.");
  140. isUPPairValid = 1;
  141. }
  142. else {
  143. System.out.println("503 Bad sequence of commands.");
  144. }
  145. }
  146. else {
  147. System.out.println("500 Syntax error, command unrecognized.");
  148. return;
  149. }
  150. }
  151. else {
  152. System.out.println("501 Syntax error in parameter.");
  153. return;
  154. }
  155. }
  156. else {
  157. System.out.println("501 Syntax error in parameter.");
  158. return;
  159. }
  160. }
  161. else {
  162. if (token.equals("\n")) {
  163. System.out.println("501 Syntax error in parameter.");
  164. return;
  165. }
  166. else {
  167. System.out.println("501 Syntax error in parameter.");
  168. return;
  169. }
  170. }
  171. }
  172. else {
  173. System.out.println("500 Syntax error, command unrecognized.");
  174. }
  175. }
  176.  
  177.  
  178. else if (token.equalsIgnoreCase("type")) {
  179. token = tokenizedLine.nextToken();
  180. while (token.equals(" ")) {
  181. token = tokenizedLine.nextToken();
  182. }
  183. if (tokenizedLine.hasMoreTokens()) {
  184. if (token.equals("A") || token.equals("I")) {
  185. if (tokenizedLine.hasMoreTokens()) {
  186. if (tokenizedLine.nextToken().equals("\r")) {
  187. if (tokenizedLine.hasMoreTokens()) {
  188. if (tokenizedLine.nextToken().equals("\n")) {
  189. if (isValidUser == 1 && isUPPairValid == 1) {
  190. System.out.println("200 Type set to " + token + ".");
  191. }
  192. else if ((isValidUser == 1 && isUPPairValid == 0)) {
  193. System.out.println("503 Bad sequence of commands.");
  194. }
  195. else {
  196. System.out.println("530 Not logged in.");
  197. }
  198. }
  199. else {
  200. System.out.println("501 Syntax error in parameter.");
  201. }
  202. }
  203. else {
  204. System.out.println("501 Syntax error in parameter.");
  205. }
  206. }
  207. else {
  208. System.out.println("501 Syntax error in parameter.");
  209. }
  210. }
  211. else {
  212. System.out.println("501 Syntax error in parameter.");
  213. }
  214. }
  215. else {
  216. System.out.println("501 Syntax error in parameter.");
  217. }
  218. }
  219. else {
  220. System.out.println("500 Syntax error, command unrecognized.");
  221. }
  222. }
  223.  
  224.  
  225. else if (token.equalsIgnoreCase("syst")) {
  226. if (tokenizedLine.hasMoreTokens()) {
  227. if (tokenizedLine.nextToken().equals("\r")) {
  228. if (tokenizedLine.hasMoreTokens()) {
  229. if (tokenizedLine.nextToken().equals("\n")) {
  230. if (isValidUser == 1 && isUPPairValid == 1) {
  231. System.out.println("215 UNIX Type: L8.");
  232. }
  233. else if ((isValidUser == 1 && isUPPairValid == 0)) {
  234. System.out.println("503 Bad sequence of commands.");
  235. }
  236. else {
  237. System.out.println("530 Not logged in.");
  238. }
  239. }
  240. else {
  241. System.out.println("501 Syntax error in parameter.");
  242. }
  243. }
  244. else {
  245. System.out.println("501 Syntax error in parameter.");
  246. }
  247. }
  248. else {
  249. System.out.println("501 Syntax error in parameter.");
  250. }
  251. }
  252. else {
  253. System.out.println("500 Syntax error, command unrecognized.");
  254. }
  255.  
  256. }
  257.  
  258.  
  259. else if (token.equalsIgnoreCase("noop")) {
  260. if (tokenizedLine.hasMoreTokens()) {
  261. if (tokenizedLine.nextToken().equals("\r")) {
  262. if (tokenizedLine.hasMoreTokens()) {
  263. if (tokenizedLine.nextToken().equals("\n")) {
  264. if (isValidUser == 1 && isUPPairValid == 1) {
  265. System.out.println("200 Command OK.");
  266. }
  267. else if ((isValidUser == 1 && isUPPairValid == 0)) {
  268. System.out.println("503 Bad sequence of commands.");
  269. }
  270. else {
  271. System.out.println("530 Not logged in.");
  272. }
  273. }
  274. else {
  275. System.out.println("501 Syntax error in parameter.");
  276. }
  277. }
  278. else {
  279. System.out.println("501 Syntax error in parameter.");
  280. }
  281. }
  282. else {
  283. System.out.println("501 Syntax error in parameter.");
  284. }
  285. }
  286. else {
  287. System.out.println("500 Syntax error, command unrecognized.");
  288. }
  289. }
  290.  
  291. else if (token.equalsIgnoreCase("port")) {
  292. if (tokenizedLine.hasMoreTokens()) {
  293. token = tokenizedLine.nextToken();
  294.  
  295. String host = "";
  296. while (token.equals(" ")) {
  297. host += token;
  298. token = tokenizedLine.nextToken();
  299. }
  300.  
  301.  
  302. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  303. host += token;
  304. token = tokenizedLine.nextToken();
  305. }
  306.  
  307. char[] hostChar = host.toCharArray();
  308.  
  309. for (int i = 0; i < hostChar.length; i++) {
  310. if ((int) hostChar[i] <= 255 && (int) hostChar[i] > 0) {
  311. continue;
  312. }
  313. else {
  314. System.out.println("501 Syntax error in parameter.");
  315. return;
  316. }
  317. }
  318.  
  319. if (token.equals("\r")) {
  320. if (tokenizedLine.hasMoreTokens()) {
  321. if (tokenizedLine.nextToken().equals("\n")) {
  322. String address = "";
  323. int portNum = 0;
  324.  
  325. String[] split = host.split(",");
  326. if (split.length == 6) {
  327. if (isValidUser == 1 && isUPPairValid == 1) {
  328. for (int i = 0; i < 4; i++) {
  329. if (i < 4) {
  330. address += split[i]+ ".";
  331. }
  332. else {
  333. address += split[i];
  334. }
  335. }
  336.  
  337. String pa1 = split[4];
  338. String pa2 = split[5];
  339. int port1 = Integer.parseInt(pa1);
  340. int port2 = Integer.parseInt(pa2);
  341. portNum = (port1 * 256) + port2;
  342. System.out.println("200 Port command successful (" + address + "," + portNum + ".");
  343. isPortValid = 1;
  344. }
  345. else if ((isValidUser == 1 && isUPPairValid == 0)) {
  346. System.out.println("503 Bad sequence of commands.");
  347. }
  348. else {
  349. System.out.println("530 Not logged in.");
  350. }
  351. }
  352. else {
  353. System.out.println("501 Syntax error in parameter.");
  354. }
  355. }
  356. else {
  357. System.out.println("501 Syntax error in parameter.");
  358. }
  359. }
  360. else {
  361. System.out.println("501 Syntax error in parameter.");
  362. }
  363. }
  364. else {
  365. System.out.println("501 Syntax error in parameter.");
  366. }
  367. }
  368. else {
  369. System.out.println("500 Syntax error, command unrecognized.");
  370. }
  371. }
  372.  
  373. else if (token.equalsIgnoreCase("retr")) {
  374. if (tokenizedLine.hasMoreTokens()) {
  375. token = tokenizedLine.nextToken();
  376.  
  377.  
  378. if(!token.equals(" ")) {
  379. // Need to change the error
  380. System.out.println("ERROR -- command");
  381. }
  382. // String path = "";
  383. while (token.equals(" ")) {
  384. // path += token;
  385. token = tokenizedLine.nextToken();
  386. }
  387.  
  388. while(token.charAt(0) == '/' || token.charAt(0) == '\\') {
  389. token = tokenizedLine.nextToken();
  390. }
  391.  
  392. String path = "";
  393. while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
  394. path += token;
  395. token = tokenizedLine.nextToken();
  396. }
  397.  
  398.  
  399. char[] pathChar = path.toCharArray();
  400. for (int i = 0; i < pathChar.length; i++) {
  401. if ((int) pathChar[i] <= 127 && (int) pathChar[i] >= 0) {
  402. continue;
  403. }
  404. else {
  405. System.out.println("501 Syntax error in parameter.");
  406. return;
  407. }
  408. }
  409.  
  410. if (token.equals("\r")) {
  411. if (tokenizedLine.hasMoreTokens()) {
  412. if (tokenizedLine.nextToken().equals("\n")) {
  413. if (path.length() > 0) {
  414. System.out.println("150 File status okay.");
  415. fileCount++;
  416.  
  417. try {
  418. copyFile(path);
  419. }
  420. catch (IOException e) {
  421. e.printStackTrace();
  422. System.out.println("550 File not found or access denied.");
  423. }
  424. return;
  425. }
  426. else {
  427. System.out.println("");
  428. }
  429. }
  430.  
  431. }
  432. }
  433. }
  434. }
  435. else if (token.equalsIgnoreCase("quit")) {
  436. if (tokenizedLine.hasMoreTokens()) {
  437. if (tokenizedLine.nextToken().equals("\r")) {
  438. if (tokenizedLine.hasMoreTokens()) {
  439. if (tokenizedLine.nextToken().equals("\n")) {
  440. if (isValidUser == 1 && isUPPairValid == 1) {
  441. System.out.println("200 Command OK.");
  442. }
  443. else if ((isValidUser == 1 && isUPPairValid == 0)) {
  444. System.out.println("503 Bad sequence of commands.");
  445. }
  446. else {
  447. System.out.println("530 Not logged in.");
  448. }
  449. }
  450. else {
  451. System.out.println("501 Syntax error in parameter.");
  452. }
  453. }
  454. else {
  455. System.out.println("501 Syntax error in parameter.");
  456. }
  457. }
  458. else {
  459. System.out.println("501 Syntax error in parameter.");
  460. }
  461. }
  462. else {
  463. System.out.println("500 Syntax error, command unrecognized.");
  464. }
  465. }
  466. else if (isUPPairValid == 0) {
  467. System.out.println("530 Not logged in.");
  468. }
  469. else {
  470. System.out.println("500 Syntax error, command unrecognized.");
  471. }
  472. }
  473. else {
  474. System.out.println("500 Syntax error, command unrecognized.");
  475. }
  476. }
  477.  
  478. private static void copyFile(String str) throws IOException {
  479. String file = "retr_files/file" + fileCount;
  480. FileInputStream in = null;
  481. FileOutputStream out = null;
  482.  
  483.  
  484. try {
  485. in = new FileInputStream(str);
  486. out = new FileOutputStream(file);
  487.  
  488. int x;
  489.  
  490. while((x = in.read()) != -1) {
  491. out.write(x);
  492. }
  493.  
  494. if(in != null) {
  495. in.close();
  496. }
  497.  
  498. if(out != null) {
  499. out.close();
  500. }
  501. } catch(IOException e) {
  502. e.printStackTrace();
  503. }
  504. }
  505.  
  506.  
  507. public static void main(String[] args) throws IOException {
  508. // Create a buffered reader to read in the lines
  509. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  510. // Store the lines in an ArrayList
  511. ArrayList<String> nexts = new ArrayList<String>();
  512. String next = "";
  513. int i = 0;
  514. i = input.read();
  515.  
  516. // While the buffer is still open
  517. while(i != -1) {
  518. // Cast the integer to a char
  519. next += (char) i;
  520. if((char) i == '\r') {
  521. // While the newline isn't encountered, add to the list, reset
  522. i = input.read();
  523. if((char) i == '\n') {
  524. next += (char) i;
  525. nexts.add(next);
  526. next = "";
  527. i = input.read();
  528. } else {
  529. nexts.add(next);
  530. next = "";
  531. }
  532.  
  533. } else if((char) i =='\n') {
  534. nexts.add(next);
  535. next = "";
  536. i = input.read();
  537. } else {
  538. i = input.read();
  539. }
  540. }
  541.  
  542. for(int j = 0; j < nexts.size(); j++) {
  543. // Parsing each
  544. next = nexts.get(j);
  545. System.out.print(next);
  546. parse(next);
  547. }
  548. }
  549. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement