Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package networking;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- import edu.vt.middleware.password.CharacterCharacteristicsRule;
- import edu.vt.middleware.password.DigitCharacterRule;
- import edu.vt.middleware.password.LengthRule;
- import edu.vt.middleware.password.LowercaseCharacterRule;
- import edu.vt.middleware.password.NonAlphanumericCharacterRule;
- import edu.vt.middleware.password.Password;
- import edu.vt.middleware.password.PasswordData;
- import edu.vt.middleware.password.PasswordValidator;
- import edu.vt.middleware.password.Rule;
- import edu.vt.middleware.password.RuleResult;
- import edu.vt.middleware.password.UppercaseCharacterRule;
- import edu.vt.middleware.password.WhitespaceRule;
- import other.Job;
- import users.Employer;
- import users.Professor;
- import users.Student;
- public class Server implements Runnable {
- private Vector<ServerThread> serverThreads;
- ServerSocket ss = null;
- private int threadID;
- Connection conn = null;
- Statement st = null;
- ResultSet rs = null;
- private Vector<Job> allJobs;
- private Vector<Notification> pendingNotifications;
- public static void main(String[] args) {
- Server temp = new Server(6789);
- Thread up = new Thread(temp);
- up.start();
- }
- public Server(int port) {
- threadID = 0;
- try {
- ss = new ServerSocket(port);
- serverThreads = new Vector<ServerThread>();
- getPendingNotifications();
- } catch (IOException ioe) {
- System.out.println("Ioe: " + ioe.getMessage());
- }
- }
- public void end() {
- try {
- if (ss != null) {
- ss.close();
- }
- } catch (IOException ioe) {
- }
- }
- public void sendMessageToAllClients(Message cm) {
- if (cm != null) {
- System.out.println(cm.getType() + ": " + cm.getMessage());
- if (cm.getType().equals("login")) {
- if (login(cm.getUsername(), cm.getPassword(), cm.getLoginType(), cm)) {
- cm.setMessage("true");
- } else {
- cm.setMessage("false");
- }
- System.out.println("Login success: " + cm.getMessage());
- } else if (cm.getType().equals("create")) {
- int result = create(cm.getUsername(), cm.getPassword(), cm.getLoginType());
- cm.setMessage(Integer.toString(result));
- } else if (cm.getType().equals("updateStudent")) {
- Student temp = cm.getStudent();
- studentRegistration(temp.getFirstName(), temp.getLastName(), temp.getSchool(), temp.getEmail(),
- temp.getUsername(), temp.getBio(), temp.getResume(), temp.getProfilePic());
- cm.setNeedsReturn(false);
- } else if (cm.getType().equals("updateProfessor")) {
- Professor temp = cm.getProfessor();
- // Testing
- professorRegistration(temp.getFirstName(), temp.getLastName(), temp.getSchool(), temp.getEmail(),
- temp.getUsername(), temp.getBio(), temp.getProfilePic());
- cm.setNeedsReturn(false);
- } else if (cm.getType().equals("updateEmployer")) {
- Employer temp = cm.getEmployer();
- employerRegistration(temp.getCompany(), temp.getPosition(), temp.getPhone(), temp.getUsername(),
- temp.getDesc(), temp.getEmail(), temp.getProfilePic());
- cm.setNeedsReturn(false);
- } else if (cm.getType().equals("addNotification")) {
- Notification temp = cm.getNotification();
- addNotifToDatabase(temp);
- }
- synchronized (serverThreads) {
- if (cm.isNeedsReturn()) {
- for (ServerThread st : serverThreads) {
- st.sendMessage(cm);
- }
- }
- }
- }
- }
- public void addNotifToDatabase(Notification notification) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String updateinfo = "INSERT INTO `notification`(`username`, `message`) VALUES(?,?)";
- try {
- PreparedStatement ps = conn.prepareStatement(updateinfo);
- ps.setString(1, notification.getUsername());
- ps.setString(2, notification.getMessage());
- while (rs.next()) {
- String user = rs.getString("username");
- String message = rs.getString("message");
- Notification temp = new Notification(user, message);
- pendingNotifications.add(temp);
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- }
- }
- @Override
- public void run() {
- while (true) {
- System.out.println("waiting for connection...");
- Socket s;
- try {
- s = ss.accept();
- System.out.println("Connection from " + s.getInetAddress());
- ServerThread st = new ServerThread(s, this, threadID);
- ++threadID;
- serverThreads.add(st);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public boolean login(String Username, String Password, String type, Message cm) {
- boolean found = false;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
- try {
- PreparedStatement ps = conn.prepareStatement(selectName);
- ps.setString(1, Username);
- rs = ps.executeQuery();
- while (rs.next()) {
- if (rs.getString(2).equals(Password)) {
- found = true;
- // If the type is employer
- if (type.equals("employer")) {
- String Company = rs.getString(3);
- String Email = rs.getString(4);
- String Pos = rs.getString(5);
- String Path = rs.getString(6);
- String Bio = rs.getString(7);
- Employer e = new Employer(Username);
- e.setCompany(Company);
- e.setEmail(Email);
- e.setPosition(Pos);
- e.setLogo(Path);
- e.setDesc(Bio);
- cm.setEmployer(e);
- } else if (type.equals("professor")) {
- Professor temp = new Professor(Username);
- temp.setSchool(rs.getString(1));
- temp.setFirstName(rs.getString(2));
- temp.setLastName(rs.getString(3));
- temp.setEmail(rs.getString(4));
- temp.setProfilePic(rs.getString(5));
- temp.setBio(rs.getString(6));
- cm.setProfessor(temp);
- } else if (type.equals("student")) {
- Student temp = new Student(Username);
- temp.setSchool(rs.getString(3));
- temp.setFirstName(rs.getString(4));
- temp.setLastName(rs.getString(5));
- temp.setEmail(rs.getString(6));
- temp.setProfilePic(rs.getString(7));
- temp.setBio(rs.getString(9));
- temp.setSchool(rs.getString(10));
- temp.setMajor(rs.getString(11));
- temp.setGPA(rs.getString(12));
- cm.setStudent(temp);
- }
- }
- }
- } catch (SQLException sql) {
- System.out.println("sqle: line 131 " + sql.getMessage());
- sql.printStackTrace();
- }
- } catch (SQLException sql) {
- System.out.println("sqle: line 136" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sqle) {
- System.out.println("sql problem");
- }
- }
- return found;
- }
- public int create(String Username, String Password, String type) {
- // 0 = success, 1 = invalid password requirements, 2 = result already
- // exists
- int success = -1;
- LengthRule lengthRule = new LengthRule(8, 16);
- WhitespaceRule whitespaceRule = new WhitespaceRule();
- CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
- charRule.getRules().add(new DigitCharacterRule(1));
- charRule.getRules().add(new NonAlphanumericCharacterRule(1));
- charRule.getRules().add(new UppercaseCharacterRule(1));
- charRule.getRules().add(new LowercaseCharacterRule(1));
- List<Rule> ruleList = new ArrayList<Rule>();
- ruleList.add(lengthRule);
- ruleList.add(whitespaceRule);
- ruleList.add(charRule);
- PasswordValidator validator = new PasswordValidator(ruleList);
- PasswordData passwordData = new PasswordData(new Password(Password));
- RuleResult result = validator.validate(passwordData);
- if (result.isValid()) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
- String addinfo = "INSERT INTO `" + type + "`(`username`, `password`,`first`, `last`) VALUES(?,?,?,?)";
- try {
- PreparedStatement ps = conn.prepareStatement(selectName);
- PreparedStatement ps2 = conn.prepareStatement(addinfo);
- ps.setString(1, Username);
- rs = ps.executeQuery();
- while (rs.next()) {
- success = 2;
- }
- if (success != 2) {
- success = 0;
- ps2.setString(1, Username);
- ps2.setString(2, Password);
- ps2.setString(3, "");
- ps2.setString(4, "");
- ps2.executeUpdate();
- }
- } catch (SQLException sql) {
- System.out.println("sqle: 206 " + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle: 209 " + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sqle) {
- System.out.println("sql problem");
- }
- }
- } else {
- success = 1;
- }
- System.out.println("Server creation of " + Username + " Finished with sucess " + success);
- return success;
- }
- public void studentRegistration(String fname, String lname, String school, String email, String username,
- String bio, String resume, String profPic) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String updateinfo = "UPDATE `student` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? , `pathResume`=? ,`bio`=? WHERE `username` = ?";
- try {
- PreparedStatement ps2 = conn.prepareStatement(updateinfo);
- ps2.setString(1, fname);
- ps2.setString(2, lname);
- ps2.setString(3, school);
- ps2.setString(4, email);
- ps2.setString(5, "temp"); // profpic
- ps2.setString(6, "temp"); // resume
- ps2.setString(7, bio);
- ps2.setString(8, username);
- ps2.executeUpdate();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- }
- }
- public void professorRegistration(String fname, String lname, String school, String email, String username,
- String bio, String profPic) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String updateinfo = "UPDATE `professor` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? ,`bio`=? WHERE `username` = ?";
- try {
- PreparedStatement ps2 = conn.prepareStatement(updateinfo);
- ps2.setString(1, fname);
- ps2.setString(2, lname);
- ps2.setString(3, school);
- ps2.setString(4, email);
- ps2.setString(5, "temp"); // profpic
- ps2.setString(6, bio);
- ps2.setString(7, username);
- ps2.executeUpdate();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- }
- }
- public void employerRegistration(String companyName, String posComp, String phone, String username, String bio,
- String email, String logo) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String updateinfo = "UPDATE `employer` SET `company`=? ,`email`=? ,`bio`=? , `position`=? ,`pathLogo`=? ,`phone`=? WHERE `username` = ?";
- try {
- PreparedStatement ps2 = conn.prepareStatement(updateinfo);
- ps2.setString(1, companyName);
- ps2.setString(2, email);
- ps2.setString(3, bio);
- ps2.setString(4, posComp);
- ps2.setString(5, logo);
- ps2.setString(6, phone);
- ps2.setString(8, username);
- ps2.executeUpdate();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- }
- }
- private void getPendingNotifications() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
- st = conn.createStatement();
- String updateinfo = "SELECT * FROM `notification`";
- try {
- rs = st.executeQuery(updateinfo);
- while (rs.next()) {
- String user = rs.getString("username");
- String message = rs.getString("message");
- Notification temp = new Notification(user, message);
- pendingNotifications.add(temp);
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- // e1.printStackTrace();
- } finally {
- try {
- if (rs != null)
- rs.close();
- if (st != null)
- st.close();
- if (conn != null)
- conn.close();
- } catch (SQLException sql) {
- System.out.println("sqle:" + sql.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement