Advertisement
jsmith1016

Untitled

Nov 2nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.03 KB | None | 0 0
  1. package com.jamessmith;
  2.  
  3. import java.io.*;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.nio.file.Files;
  7. import java.util.ArrayList;
  8.  
  9.  
  10. public class Server {
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.  
  14.         ArrayList<User> usernameAndPasswordList = null;
  15.  
  16.         try {
  17.             //load the user database
  18.             File inputFile = new File("users.txt");
  19.             //store it in an array list of users
  20.             usernameAndPasswordList = uploadUsernameAndPasswords(inputFile);
  21.         }
  22.         //if no file is input, send an error message
  23.         catch (FileNotFoundException e) {
  24.             System.out.println("ERROR:File not found. Please ensure the user.txt and root folder are located with the Server executable before running this program.");
  25.             System.exit(1);
  26.         }
  27.         //load the root folder
  28.         File rootFolder = new File("root");
  29.         //store its files
  30.         File[] rootFiles = rootFolder.listFiles();
  31.  
  32.  
  33.         //server declarations
  34.         BufferedReader inputFromClient = null;
  35.         PrintWriter outputToClient = null;
  36.         Socket socket = null;
  37.  
  38.         //start server
  39.         try (ServerSocket serverSocket = new ServerSocket(5001))
  40.         {
  41.             socket = serverSocket.accept();
  42.             inputFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  43.             outputToClient = new PrintWriter(socket.getOutputStream(), true);
  44.  
  45.             String fileName;
  46.  
  47.             System.out.println("Client Connected");
  48.  
  49.             boolean connected = true;
  50.                 do {
  51.                     String command = inputFromClient.readLine();
  52.                     switch (command) {
  53.                         case "USER":
  54.                             validateUsername(inputFromClient, outputToClient, usernameAndPasswordList);
  55.                             break;
  56.                         case "PASS":
  57.                             validatePassword(inputFromClient, outputToClient, usernameAndPasswordList);
  58.                             break;
  59.                         case "LIST":
  60.                             sendFileList(outputToClient, rootFolder);
  61.                             break;
  62.                         case "KILL":
  63.                             fileName = inputFromClient.readLine();
  64.                             rootFiles = deleteFile(outputToClient, rootFolder, rootFiles, fileName);
  65.                             break;
  66.                         case "DONE":
  67.                             connected = false;
  68.                             System.out.println("Client Disconnected");
  69.                             break;
  70.                         case "RETR":
  71.                             fileName = inputFromClient.readLine();
  72.                             retrieveFile(inputFromClient, outputToClient, rootFiles, fileName, socket);
  73.                             break;
  74.                         default:
  75.  
  76.                             break;
  77.                     }
  78.                 } while (connected);
  79.             }
  80.           catch (IOException e) {
  81.             System.out.println(e.getMessage());
  82.             e.printStackTrace();
  83.         } finally {
  84.             if (inputFromClient != null) {
  85.                 inputFromClient.close();
  86.             }
  87.             if (outputToClient != null) {
  88.                 outputToClient.close();
  89.             }
  90.             if (socket != null) {
  91.                 socket.close();
  92.             }
  93.         }
  94.     }//end main
  95.     //////////////////////////////////////////////////////////////////////////////////////////////
  96.  
  97.     private static void retrieveFile(BufferedReader inputFromClient, PrintWriter outputToClient, File[] rootFiles, String fileToRetrieve, Socket socket) throws IOException {
  98.  
  99.         String input;
  100.  
  101.         boolean found = false;
  102.  
  103.         for (File x : rootFiles) {
  104.             //convert File into String
  105.             String fileName = x.getName();
  106.             //if found, retrieve it
  107.             if (fileToRetrieve.equals(fileName)) {
  108.                 found = true;
  109.                 outputToClient.println(true);
  110.                 outputToClient.println("#" + x.length());
  111.                 input = inputFromClient.readLine();
  112.                 if (input.equals("SEND")) {
  113.                     File fileToSend = new File(fileToRetrieve);
  114.                     byte[] byteArray = new byte[(int)x.length()];
  115.                     FileInputStream fis = new FileInputStream(fileToSend);
  116.                     BufferedInputStream inputStream = new BufferedInputStream(fis);
  117.                     inputStream.read(byteArray, 0, byteArray.length);
  118.                     OutputStream outputStream = null;
  119.                     outputStream = socket.getOutputStream();
  120.                     outputStream.write(byteArray, 0, byteArray.length);
  121.                     inputStream.close();
  122.                     outputStream.close();
  123.                     outputToClient.println("+File sent");
  124.                     break;
  125.                 } else if (input.equals("STOP")) {
  126.                     outputToClient.println("+ok, RETR aborted");
  127.                     break;
  128.                 }
  129.             }
  130.         }
  131.  
  132.         if(!found){
  133.             outputToClient.println(false);
  134.         }
  135.  
  136.  
  137.     }//end retrieveFile
  138.     /////////////////////////////////////////////////////////////////////////////////////////////
  139.  
  140.     private static File[] deleteFile(PrintWriter outputToClient, File rootFolder, File[] rootFiles, String fileToDelete) {
  141.  
  142.         boolean deleted = false;
  143.         //search for the file
  144.         for(File x: rootFiles){
  145.             //convert File into String
  146.             String fileName = x.getName();
  147.             //if found, delete it
  148.             if(fileToDelete.equals(fileName)){
  149.                 deleted = x.delete();
  150.                 outputToClient.println("+" + fileToDelete + " deleted");
  151.                 break;
  152.             }
  153.         }
  154.         //if deleted == false, file wasn't found
  155.         if(!deleted){
  156.             outputToClient.println("-" + fileToDelete + " does not exist");
  157.         }
  158.  
  159.         return rootFolder.listFiles();
  160.  
  161.     }//end deleteFile
  162.     ////////////////////////////////////////////////////////////////////////////////////////////
  163.  
  164.     private static void sendFileList(PrintWriter outputToClient, File folder) {
  165.         String[] fileList = folder.list();
  166.         outputToClient.println("+root");
  167.         //send the number of file names to the client
  168.         outputToClient.println(fileList.length);
  169.         //for each file in the root directory, send to client
  170.         for(String x: fileList){
  171.             outputToClient.println(x);
  172.         }
  173.     }//end sendFileList
  174.     ///////////////////////////////////////////////////////////////////////////////////////////
  175.  
  176.  
  177.  
  178.     private static ArrayList<User> uploadUsernameAndPasswords(File file) throws IOException {
  179.         String inputString;
  180.         BufferedReader br = null;
  181.         ArrayList<User> userArr = new ArrayList<>();
  182.         try {
  183.             br = new BufferedReader(new FileReader(file));
  184.         } catch (FileNotFoundException e) {
  185.             e.printStackTrace();
  186.         }
  187.         while ((inputString = br.readLine()) != null) {
  188.             //This splits the line into username and password
  189.             //trimmedString[0] is the username, trimmedString[1] is the password.
  190.             String[] trimmedString = inputString.trim().split("\\s+");
  191.             userArr.add(new User(trimmedString[0], trimmedString[1]));
  192.             }
  193.         return userArr;
  194.     }//end uploadUserNameAndPasswords
  195.     ////////////////////////////////////////////////////////////////////////////////////////////
  196.  
  197.     private static void validateUsername(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> usernameAndPasswordList) throws IOException {
  198.         //read the username from the server
  199.         String username = inputFromServer.readLine();
  200.         //for each user
  201.         for (User list : usernameAndPasswordList) {
  202.             boolean check = list.usernameAuthentication(username);
  203.             //if the username was found, send true.
  204.             if (check) {
  205.                 outputToServer.println(true);
  206.                 return;
  207.             }
  208.         }
  209.         //if username not found, send false
  210.         outputToServer.println(false);
  211.     }
  212.  
  213.     private static void validatePassword(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> usernameAndPasswordList) throws IOException {
  214.         //read the username and password from the server
  215.         String username = inputFromServer.readLine();
  216.         String password = inputFromServer.readLine();
  217.         //for each user
  218.         for (User list : usernameAndPasswordList) {
  219.             boolean findUser = list.usernameAuthentication(username);
  220.             //find the username, check password
  221.             if (findUser) {
  222.                 boolean checkPassword = list.passwordAuthentication(password);
  223.                 //if password matches, send true
  224.                 if(checkPassword){
  225.                     outputToServer.println(true);
  226.                     return;
  227.                 }
  228.             }
  229.         }
  230.         //if password doesn't match, send false
  231.         outputToServer.println(false);
  232.     }//end validatePassword
  233.     //////////////////////////////////////////////////////////////////////////////////////////
  234.  
  235. }//end Server
  236. /////////////////////////////////////////////////////////////////////////////////////////////
  237.  
  238. //a User class to hold the username and passwords.
  239. class User {
  240.  
  241.     //fields
  242.     private String username;
  243.     private String password;
  244.  
  245.     //constructor
  246.     User(String x, String y){
  247.         this.username = x;
  248.         this.password = y;
  249.     }
  250.  
  251.     //only use boolean values to match usernames and passwords
  252.     public boolean usernameAuthentication(String x) {
  253.         return x.equals(this.username);
  254.     }
  255.  
  256.     public boolean passwordAuthentication(String y) {
  257.         return y.equals(this.password);
  258.     }
  259.  
  260. }//end User
  261. //////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement