Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. package Server;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONObject;
  5.  
  6. import java.io.*;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.Base64;
  11. import java.util.Scanner;
  12.  
  13.  
  14. public class Server {
  15.  
  16.     private static final int serverPort = 2040;
  17.     private ServerSocket s;
  18.     private BufferedReader in;
  19.     private PrintWriter out;
  20.     String userNotesOutput;
  21.  
  22.     Server() {
  23.         try {
  24.             s = new ServerSocket(serverPort);
  25.             System.out.println("Server dziala");
  26.         } catch (Exception e) {
  27.             System.out.println("Nie można utworzyć gniazda Servera");
  28.             System.exit(1);
  29.         }
  30.     }
  31.  
  32.     class ObslugaKlienta extends Thread {
  33.         Socket socket;
  34.  
  35.         public ObslugaKlienta(Socket socket) throws IOException {
  36.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  37.             out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  38.             this.socket = socket;
  39.         }
  40.  
  41.         public void run() {
  42.             try {
  43.                 String komunikat;
  44.                 while ((komunikat = in.readLine()) != null) {
  45.                     switch (komunikat) {
  46.                         case "GetNotes":
  47.                             try {
  48.                                 String userLoginGerNotes = in.readLine();
  49.  
  50.                                 Scanner scanner = new Scanner(new File("notes/" + userLoginGerNotes + ".json"));
  51.                                 userNotesOutput = scanner.useDelimiter("\\A").next();
  52.                                 scanner.close();
  53.  
  54.                                 out.println(userNotesOutput);
  55.                             } catch (Exception error) {
  56.                                 System.out.println("ERROR SERVER READ DATA" + error);
  57.                             }
  58.                             break;
  59.                         case "SetNotes":
  60.                             String userLoginGerNotes = in.readLine();
  61.                             String dataToSave = in.readLine();
  62.  
  63.                             Scanner scannerSetNotes = new Scanner(new File("notes/" + userLoginGerNotes + ".json"));
  64.                             userNotesOutput = scannerSetNotes.useDelimiter("\\A").next();
  65.                             scannerSetNotes.close();
  66.  
  67.                             JSONObject jsonObjectSetData = new JSONObject(userNotesOutput);
  68.                             JSONArray userArray = new JSONArray(dataToSave);
  69.                             jsonObjectSetData.put("data", userArray);
  70.  
  71.                             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("notes/" + userLoginGerNotes + ".json"));
  72.                             bufferedWriter.write(jsonObjectSetData.toString());
  73.                             bufferedWriter.close();
  74.                             break;
  75.                         case "Register":
  76.                             String login = in.readLine();
  77.                             String name = in.readLine();
  78.                             String surname = in.readLine();
  79.                             String password = in.readLine();
  80.                             JSONObject mainJsonObject = new JSONObject();
  81.  
  82.                             File userFile = new File("notes/" + login + ".json");
  83.                             if (userFile.exists() && !userFile.isDirectory()) {
  84.                                 mainJsonObject.put("success", false);
  85.                                 mainJsonObject.put("message", "Login jest zajety");
  86.                                 out.println(mainJsonObject);
  87.                             } else {
  88.                                 try {
  89.                                     JSONObject newJsonObject = new JSONObject();
  90.                                     newJsonObject.put("login", login);
  91.                                     newJsonObject.put("name", name);
  92.                                     newJsonObject.put("surname", surname);
  93.  
  94.                                     byte[] encodedBytes = Base64.getEncoder().encode(password.getBytes());
  95.                                     String str = new String(encodedBytes, StandardCharsets.UTF_8);
  96.  
  97.                                     newJsonObject.put("password", str);
  98.                                     JSONArray jsonArray = new JSONArray();
  99.                                     newJsonObject.put("data", jsonArray);
  100.  
  101.                                     BufferedWriter bufferedWriterNewUser = new BufferedWriter(new FileWriter("notes/" + login + ".json"));
  102.                                     bufferedWriterNewUser.write(newJsonObject.toString());
  103.                                     bufferedWriterNewUser.close();
  104.  
  105.                                     mainJsonObject.put("success", true);
  106.                                     mainJsonObject.put("message", "Rejestracja przebiegla prawidlowo");
  107.                                     out.println(mainJsonObject);
  108.  
  109.                                     System.out.println(mainJsonObject);
  110.  
  111.                                 } catch (Exception e) {
  112.                                     System.out.println("Error");
  113.                                 }
  114.                             }
  115.                             break;
  116.                         case "Login":
  117.                             String loginLogin = in.readLine();
  118.  
  119.                             System.out.println("Login pierwszy parametr " + loginLogin);
  120.  
  121.                             String passwordLogin = in.readLine();
  122.                             JSONObject loginJsonObject = new JSONObject();
  123.  
  124.                             System.out.println("haslo co przychodzi " + passwordLogin);
  125.  
  126.                             File userFileLogin = new File("notes/" + loginLogin + ".json");
  127.                             if (userFileLogin.exists() && !userFileLogin.isDirectory()) {
  128.                                 Scanner scanner = new Scanner(new File("notes/" + loginLogin + ".json"));
  129.                                 String userData = scanner.useDelimiter("\\A").next();
  130.  
  131.                                 JSONObject jsonObject = new JSONObject(userData);
  132.  
  133. //                        String loginFromJson = jsonObject.getString("login");
  134.                                 String passwordFromJson = jsonObject.getString("password");
  135.  
  136.                                 byte[] passwordLoginHash = Base64.getEncoder().encode(passwordLogin.getBytes());
  137.                                 String passwordLoginHashString = new String(passwordLoginHash, StandardCharsets.UTF_8);
  138.  
  139.  
  140.                                 if (passwordFromJson.equals(passwordLoginHashString)) {
  141.                                     loginJsonObject.put("success", true);
  142.                                     loginJsonObject.put("message", "Logowanie udalo sie.");
  143.                                     out.println(loginJsonObject);
  144.                                 } else {
  145.                                     loginJsonObject.put("success", false);
  146.                                     loginJsonObject.put("message", "Logowanie nie udalo sie.");
  147.                                     out.println(loginJsonObject);
  148.                                 }
  149.  
  150.                             } else {
  151.                                 loginJsonObject.put("success", false);
  152.                                 loginJsonObject.put("message", "Nie ma takiego uzytkownika.");
  153.                                 out.println(loginJsonObject);
  154.                             }
  155.  
  156.                             break;
  157.                         default:
  158.                             break;
  159.                     }
  160.  
  161.                 }
  162.             } catch (Exception e) {
  163.                 out.close();
  164.             }
  165.         }
  166.     }
  167.  
  168.     void dzialaj() throws Exception {
  169.         while (true) {
  170.             Socket socket = s.accept();
  171.             System.out.println("Zglosil sie nowy klient");
  172.  
  173.             try {
  174.                 ObslugaKlienta a = new ObslugaKlienta(socket);
  175.                 a.start();
  176.  
  177.             } catch (IOException e) {
  178.                 socket.close();
  179.             }
  180.         }
  181.     }
  182.  
  183.     public static void main(String args[]) throws Exception {
  184.         Server server = new Server();
  185.         server.dzialaj();
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement