Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class FTP1server {
- /*
- 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.
- */
- // 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 {
- // If there is no \r, that means there was never a CRLF so it gives an error.
- // If there is just an \n, it is a command error
- // if(token.equals("\n")) {
- // System.out.println("ERROR -- command");
- // 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 {
- // // If there is no \r, that means there was never a CRLF so it gives an error.
- // // If there is just an \n, it is a command error
- // if(token.equals("\n")) {
- // System.out.println("ERROR -- command");
- // 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;
- }
- }
- // 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;
- }
- }
- }
- 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