Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment2;
- import java.io.*;
- import java.util.*;
- public class FTPserver2 {
- static int isValidUser = 0;
- static int isUPPairValid = 0;
- static int isPortValid = 0;
- static int fileCount = 0;
- static void parse(String input) {
- String token;
- StringTokenizer tokenizedLine = new StringTokenizer(input, " \r\n", true);
- if (tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if (token.equalsIgnoreCase("user")) {
- if (tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- String user = "";
- while (token.equals(" ")) {
- user += token;
- token = tokenizedLine.nextToken();
- }
- while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- user += token;
- token = tokenizedLine.nextToken();
- }
- char[] userChar = user.toCharArray();
- for (int i = 0; i < userChar.length; i++) {
- if ((int) userChar[i] <= 127) {
- continue;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- if (token.equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (user.equals(" ")) {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- else if (user.length() > 0 && isValidUser == 0) {
- System.out.println("331 Guest access OK, send password.");
- isValidUser = 1;
- }
- else if (user.length() > 0 && isValidUser == 1) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- return;
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- else {
- if (token.equals("\n")) {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("pass")) {
- if (tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- String pass = "";
- while (token.equals(" ")) {
- pass += token;
- token = tokenizedLine.nextToken();
- }
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- pass += token;
- token = tokenizedLine.nextToken();
- }
- char[] passChar = pass.toCharArray();
- for (int i = 0; i < passChar.length; i++) {
- if ((int) passChar[i] <= 127) {
- continue;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- if (token.equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (pass.equals(" ")) {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- else if (pass.length() > 0) {
- if (isValidUser == 1) {
- System.out.println("230 Guest login OK.");
- isUPPairValid = 1;
- }
- else {
- System.out.println("503 Bad sequence of commands.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- return;
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- else {
- if (token.equals("\n")) {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("type")) {
- token = tokenizedLine.nextToken();
- while (token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- if (tokenizedLine.hasMoreTokens()) {
- if (token.equals("A") || token.equals("I")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (isValidUser == 1 && isUPPairValid == 1) {
- System.out.println("200 Type set to " + token + ".");
- }
- else if ((isValidUser == 1 && isUPPairValid == 0)) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("530 Not logged in.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("syst")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (isValidUser == 1 && isUPPairValid == 1) {
- System.out.println("215 UNIX Type: L8.");
- }
- else if ((isValidUser == 1 && isUPPairValid == 0)) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("530 Not logged in.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("noop")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (isValidUser == 1 && isUPPairValid == 1) {
- System.out.println("200 Command OK.");
- }
- else if ((isValidUser == 1 && isUPPairValid == 0)) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("530 Not logged in.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("port")) {
- if (tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- String host = "";
- while (token.equals(" ")) {
- host += token;
- token = tokenizedLine.nextToken();
- }
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- host += token;
- token = tokenizedLine.nextToken();
- }
- char[] hostChar = host.toCharArray();
- for (int i = 0; i < hostChar.length; i++) {
- if ((int) hostChar[i] <= 255 && (int) hostChar[i] > 0) {
- continue;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- if (token.equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- String address = "";
- int portNum = 0;
- String[] split = host.split(",");
- if (split.length == 6) {
- if (isValidUser == 1 && isUPPairValid == 1) {
- for (int i = 0; i < 4; i++) {
- if (i < 4) {
- address += split[i]+ ".";
- }
- else {
- address += split[i];
- }
- }
- String pa1 = split[4];
- String pa2 = split[5];
- int port1 = Integer.parseInt(pa1);
- int port2 = Integer.parseInt(pa2);
- portNum = (port1 * 256) + port2;
- System.out.println("200 Port command successful (" + address + "," + portNum + ".");
- isPortValid = 1;
- }
- else if ((isValidUser == 1 && isUPPairValid == 0)) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("530 Not logged in.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (token.equalsIgnoreCase("retr")) {
- if (tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- // Need to change the error
- System.out.println("ERROR -- command");
- }
- // String path = "";
- while (token.equals(" ")) {
- // path += token;
- token = tokenizedLine.nextToken();
- }
- while(token.charAt(0) == '/' || token.charAt(0) == '\\') {
- token = tokenizedLine.nextToken();
- }
- String path = "";
- while (tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- path += token;
- token = tokenizedLine.nextToken();
- }
- char[] pathChar = path.toCharArray();
- for (int i = 0; i < pathChar.length; i++) {
- if ((int) pathChar[i] <= 127 && (int) pathChar[i] >= 0) {
- continue;
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- return;
- }
- }
- if (token.equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (path.length() > 0) {
- System.out.println("150 File status okay.");
- fileCount++;
- try {
- copyFile(path);
- }
- catch (IOException e) {
- e.printStackTrace();
- System.out.println("550 File not found or access denied.");
- }
- return;
- }
- else {
- System.out.println("");
- }
- }
- }
- }
- }
- }
- else if (token.equalsIgnoreCase("quit")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- if (isValidUser == 1 && isUPPairValid == 1) {
- System.out.println("200 Command OK.");
- }
- else if ((isValidUser == 1 && isUPPairValid == 0)) {
- System.out.println("503 Bad sequence of commands.");
- }
- else {
- System.out.println("530 Not logged in.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("501 Syntax error in parameter.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else if (isUPPairValid == 0) {
- System.out.println("530 Not logged in.");
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- else {
- System.out.println("500 Syntax error, command unrecognized.");
- }
- }
- private static void copyFile(String str) throws IOException {
- String file = "retr_files/file" + fileCount;
- FileInputStream in = null;
- FileOutputStream out = null;
- try {
- in = new FileInputStream(str);
- out = new FileOutputStream(file);
- int x;
- while((x = in.read()) != -1) {
- out.write(x);
- }
- if(in != null) {
- in.close();
- }
- if(out != null) {
- out.close();
- }
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws IOException {
- // Create a buffered reader to read in the lines
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- // Store the lines in an ArrayList
- ArrayList<String> nexts = new ArrayList<String>();
- String next = "";
- int i = 0;
- i = input.read();
- // While the buffer is still open
- while(i != -1) {
- // Cast the integer to a char
- next += (char) i;
- if((char) i == '\r') {
- // While the newline isn't encountered, add to the list, reset
- i = input.read();
- if((char) i == '\n') {
- next += (char) i;
- nexts.add(next);
- next = "";
- i = input.read();
- } else {
- nexts.add(next);
- next = "";
- }
- } else if((char) i =='\n') {
- nexts.add(next);
- next = "";
- i = input.read();
- } else {
- i = input.read();
- }
- }
- for(int j = 0; j < nexts.size(); j++) {
- // Parsing each
- next = nexts.get(j);
- System.out.print(next);
- parse(next);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement