Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment2;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.*;
- public class FTPserver2 {
- /*
- The problem: To read in FTP commands and decide if they are valid, given a set of rules.
- Approach: Using StringTokenizer, decide which command is being passed through and use the rules given for each
- to see if it is a valid FTP command. For USER and PASS, first strip the tokens of the whitespace, then do the
- checks to determine validity. For TYPE, use the rules to check for validity while also checking the tokens for
- A or I. SYST, QUIT, and NOOP check for validity using the rules. Used a parse method to perform all of the checks,
- then used BufferedReader to read in the input.
- */
- static int retrCount = 0;
- // Method to parse the inputed string
- static void parse(String input) {
- // Current token
- String token = "";
- // Declaring and instantiating the StringTokenizer for the input, which is the variable input
- StringTokenizer tokenizedLine = new StringTokenizer(input, " \n\r", true);
- // Checking to see if there are more tokens in the string
- if(tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- // Checking to see if the command is valid (user)
- if(token.equalsIgnoreCase("user")) {
- if(tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- System.out.println("ERROR -- command");
- return;
- }
- // Stripping the white spaces after USER
- while(token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- // Creating a username variable to allow for spaces in the username
- String user = "";
- while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- user += token;
- token = tokenizedLine.nextToken();
- }
- // Creating a character array and looping through each to check if each is an ASCII character
- char[] userChar = user.toCharArray();
- for(int x = 0; x < userChar.length; x++) {
- if((int) userChar[x] <= 127 && (int) userChar[x] >= 0) {
- continue;
- } else {
- System.out.println("ERROR -- username");
- return;
- }
- }
- // Checking after the username is entered
- if(token.equals("\r")) {
- if(tokenizedLine.hasMoreTokens()) {
- if(tokenizedLine.nextToken().equals("\n")) {
- if(user.length() > 0) {
- System.out.println("Command ok");
- return;
- } else {
- System.out.println("ERROR -- username");
- return;
- }
- } else {
- // If \r\n isn't detected, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- // If there are no more tokens after \r, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- System.out.println("ERROR -- CRLF");
- return;
- //}
- }
- } else {
- // If there are no tokens entered after USER, it is a command error
- System.out.println("ERROR -- command");
- return;
- }
- }
- // Checking to see if the command is valid (password)
- else if(token.equalsIgnoreCase("pass")) {
- if(tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- System.out.println("ERROR -- command");
- return;
- }
- // Stripping the white spaces after PASS
- while(token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- // Creating a password variable to allow for spaces in the password
- String pass = "";
- while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- pass += token;
- token = tokenizedLine.nextToken();
- }
- // Creating a character array and looping through each to check if each is an ASCII character
- char[] passChar = pass.toCharArray();
- for(int x = 0; x < passChar.length; x++) {
- if((int) passChar[x] <= 127 && (int) passChar[x] >= 0) {
- continue;
- } else {
- System.out.println("ERROR -- password");
- return;
- }
- }
- // Checking after the password is entered
- if(token.equals("\r")) {
- if(tokenizedLine.hasMoreTokens()) {
- if(tokenizedLine.nextToken().equals("\n")) {
- if(pass.length() > 0) {
- System.out.println("Command ok");
- return;
- } else {
- System.out.println("ERROR -- password");
- return;
- }
- } else {
- // If \r\n isn't detected, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- // If there are no more tokens after \r, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- System.out.println("ERROR -- CRLF");
- return;
- //}
- }
- } else {
- // If there are no tokens entered after PASS, it is a command error
- System.out.println("ERROR -- command");
- return;
- }
- }
- // Checking to see if the command is valid (type)
- else if (token.equalsIgnoreCase("type")) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- System.out.println("ERROR -- command");
- return;
- }
- // Trimming the spaces
- while (token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- if (tokenizedLine.hasMoreTokens()) {
- // Checking if the token is A or I
- if (token.equals("A") || token.equals("I")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\r")) {
- if (tokenizedLine.hasMoreTokens()) {
- if (tokenizedLine.nextToken().equals("\n")) {
- System.out.println("Command ok");
- return;
- }
- else {
- // If there is no \n after the \r
- System.out.println("ERROR -- CRLF");
- return;
- }
- }
- else {
- // If there is no \r
- System.out.println("ERROR -- CRLF");
- return;
- }
- }
- else {
- // If it doesn't end in \r\n
- System.out.println("ERROR -- CRLF");
- return;
- }
- }
- else {
- // If there is no CRLF
- System.out.println("ERROR -- command");
- return;
- }
- }
- else {
- // If the type after is not A or I
- System.out.println("ERROR -- type-code");
- return;
- }
- }
- else {
- // If the command is wrong
- System.out.println("ERROR -- command");
- return;
- }
- }
- else if(token.equalsIgnoreCase("retr")) {
- if(tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- System.out.println("ERROR -- command");
- return;
- }
- // Stripping the white spaces after USER
- while(token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- while(token.charAt(0) == '/' || token.charAt(0) == '\\') {
- token = tokenizedLine.nextToken();
- }
- // Creating a username variable to allow for spaces in the username
- String pathName = "";
- while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- pathName += token;
- token = tokenizedLine.nextToken();
- }
- // Creating a character array and looping through each to check if each is an ASCII character
- char[] pathChar= pathName.toCharArray();
- for(int x = 0; x < pathChar.length; x++) {
- if((int) pathChar[x] <= 127 && (int) pathChar[x] >= 0) {
- continue;
- } else {
- System.out.println("ERROR -- username");
- return;
- }
- }
- // Checking after the username is entered
- if(token.equals("\r")) {
- if(tokenizedLine.hasMoreTokens()) {
- if(tokenizedLine.nextToken().equals("\n")) {
- if(pathName.length() > 0) {
- System.out.println("Command ok");
- retrCount++;
- try {
- processRetr(pathName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return;
- } else {
- System.out.println("ERROR -- username");
- return;
- }
- } else {
- // If \r\n isn't detected, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- // If there are no more tokens after \r, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- System.out.println("ERROR -- CRLF");
- return;
- //}
- }
- } else {
- // If there are no tokens entered after USER, it is a command error
- System.out.println("ERROR -- command");
- return;
- }
- }
- else if(token.equalsIgnoreCase("port")) {
- if(tokenizedLine.hasMoreTokens()) {
- token = tokenizedLine.nextToken();
- if(!token.equals(" ")) {
- System.out.println("ERROR -- command");
- return;
- }
- // Stripping the white spaces after USER
- while(token.equals(" ")) {
- token = tokenizedLine.nextToken();
- }
- while(token.equals("/") && token.equals("\\")) {
- token = tokenizedLine.nextToken();
- }
- // Creating a username variable to allow for spaces in the username
- String hostPort = "";
- while(tokenizedLine.hasMoreTokens() && !token.equals("\r")) {
- hostPort += token;
- token = tokenizedLine.nextToken();
- }
- // Creating a character array and looping through each to check if each is an ASCII character
- char[] hostPortChar= hostPort.toCharArray();
- int ha1 = 0;
- int ha2 = 0;
- int ha3 = 0;
- int ha4 = 0;
- int pa1 = 0;
- int pa2 = 0;
- String sa1 = "";
- String sa2 = "";
- String sa3 = "";
- String sa4 = "";
- int count = 0;
- for(int x = 0; x < hostPortChar.length; x++) {
- if(hostPortChar[x] == ',') {
- count++;
- if(count == 0) {
- if(ha1 <= 255 && ha1 >= 0) {
- continue;
- } else {
- //TODO Error in host port number deal with it later
- }
- } else if(count == 1) {
- if(ha2 <= 255 && ha2 >= 0) {
- continue;
- } else {
- //TODO Error in host port number deal with it later
- }
- } else if(count == 2) {
- if(ha3 <= 255 && ha3 >= 0) {
- continue;
- } else {
- //TODO Error in host port number deal with it later
- }
- } else if(count == 3) {
- if(ha4 <= 255 && ha4 >= 0) {
- continue;
- } else {
- //TODO Error in host port number deal with it later
- }
- } else if (count == 4) {
- if(pa1 <= 255 && pa1 >= 0) {
- continue;
- } else {
- //TODO Error in host port number deal with it later
- }
- } else if(count == 5) {
- if(pa2 <= 255 && pa2 >= 0) {
- token = tokenizedLine.nextToken();
- if(token.equals("\r")) {
- if(tokenizedLine.hasMoreTokens()) {
- if(tokenizedLine.nextToken().equals("\n")) {
- processPort(sa1, sa2, sa3, sa4, pa1, pa2);
- //TODO print ya know
- return;
- } else {
- //TODO change this
- System.out.println("ERROR -- hostport");
- return;
- }
- } else {
- // If \r\n isn't detected, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- // If there are no more tokens after \r, it is a CRLF error
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- //TODO error in hostport
- return;
- }
- } else {
- //TODO Error in host port number deal with it later
- }
- } else {
- if(count == 0) {
- ha1 += (int) hostPortChar[x];
- sa1 += hostPortChar[x];
- } else if(count == 1) {
- ha2 += (int) hostPortChar[x];
- sa2 += hostPortChar[x];
- } else if(count == 2) {
- ha3 += (int) hostPortChar[x];
- sa3 += hostPortChar[x];
- } else if(count == 3) {
- ha4 += (int) hostPortChar[x];
- sa4 += hostPortChar[x];
- } else if (count == 4) {
- pa1 += (int) hostPortChar[x];
- } else if(count == 5) {
- pa2 += (int) hostPortChar[x];
- } else {
- System.out.println("Too many/too few variables");
- }
- }
- }
- }
- }
- // Checking to see if command is valid (NOOP, QUIT, and SYST)
- else if(token.equalsIgnoreCase("noop") || token.equalsIgnoreCase("quit") || token.equalsIgnoreCase("syst")) {
- // Checking to see if the CRLF characters are after the command
- if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\r")) {
- if(tokenizedLine.hasMoreTokens() && tokenizedLine.nextToken().equals("\n")) {
- System.out.println("Command ok");
- return;
- } else {
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- System.out.println("ERROR -- CRLF");
- return;
- }
- } else {
- System.out.println("ERROR -- command");
- return;
- }
- }
- }
- private static void processPort(String ha1, String ha2, String ha3, String ha4, int pa1, int pa2) {
- String hostAddress = ha1 + "." + ha2 + "." + ha3 + "." + ha4 + ",";
- int portNumber = (pa1 * 265) + pa2;
- }
- private static void processRetr(String str) throws IOException {
- String file = "retr_files/file" + retrCount;
- 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