Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.31 KB | None | 0 0
  1. package networking;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Vector;
  15.  
  16. import edu.vt.middleware.password.CharacterCharacteristicsRule;
  17. import edu.vt.middleware.password.DigitCharacterRule;
  18. import edu.vt.middleware.password.LengthRule;
  19. import edu.vt.middleware.password.LowercaseCharacterRule;
  20. import edu.vt.middleware.password.NonAlphanumericCharacterRule;
  21. import edu.vt.middleware.password.Password;
  22. import edu.vt.middleware.password.PasswordData;
  23. import edu.vt.middleware.password.PasswordValidator;
  24. import edu.vt.middleware.password.Rule;
  25. import edu.vt.middleware.password.RuleResult;
  26. import edu.vt.middleware.password.UppercaseCharacterRule;
  27. import edu.vt.middleware.password.WhitespaceRule;
  28. import other.Job;
  29. import users.Employer;
  30. import users.Professor;
  31. import users.Student;
  32.  
  33. public class Server implements Runnable {
  34.  
  35. private Vector<ServerThread> serverThreads;
  36. ServerSocket ss = null;
  37. private int threadID;
  38. Connection conn = null;
  39. Statement st = null;
  40. ResultSet rs = null;
  41. private Vector<Job> allJobs;
  42.  
  43. private Vector<Notification> pendingNotifications;
  44.  
  45. public static void main(String[] args) {
  46. Server temp = new Server(6789);
  47. Thread up = new Thread(temp);
  48. up.start();
  49. }
  50.  
  51. public Server(int port) {
  52. threadID = 0;
  53. try {
  54. ss = new ServerSocket(port);
  55. serverThreads = new Vector<ServerThread>();
  56. getPendingNotifications();
  57.  
  58. } catch (IOException ioe) {
  59. System.out.println("Ioe: " + ioe.getMessage());
  60. }
  61. }
  62.  
  63. public void end() {
  64. try {
  65. if (ss != null) {
  66. ss.close();
  67. }
  68. } catch (IOException ioe) {
  69. }
  70. }
  71.  
  72. public void sendMessageToAllClients(Message cm) {
  73. if (cm != null) {
  74. System.out.println(cm.getType() + ": " + cm.getMessage());
  75. if (cm.getType().equals("login")) {
  76. if (login(cm.getUsername(), cm.getPassword(), cm.getLoginType(), cm)) {
  77. cm.setMessage("true");
  78. } else {
  79. cm.setMessage("false");
  80. }
  81. System.out.println("Login success: " + cm.getMessage());
  82. } else if (cm.getType().equals("create")) {
  83. int result = create(cm.getUsername(), cm.getPassword(), cm.getLoginType());
  84. cm.setMessage(Integer.toString(result));
  85. } else if (cm.getType().equals("updateStudent")) {
  86. Student temp = cm.getStudent();
  87. studentRegistration(temp.getFirstName(), temp.getLastName(), temp.getSchool(), temp.getEmail(),
  88. temp.getUsername(), temp.getBio(), temp.getResume(), temp.getProfilePic());
  89. cm.setNeedsReturn(false);
  90.  
  91. } else if (cm.getType().equals("updateProfessor")) {
  92. Professor temp = cm.getProfessor();
  93. // Testing
  94.  
  95. professorRegistration(temp.getFirstName(), temp.getLastName(), temp.getSchool(), temp.getEmail(),
  96. temp.getUsername(), temp.getBio(), temp.getProfilePic());
  97. cm.setNeedsReturn(false);
  98. } else if (cm.getType().equals("updateEmployer")) {
  99. Employer temp = cm.getEmployer();
  100. employerRegistration(temp.getCompany(), temp.getPosition(), temp.getPhone(), temp.getUsername(),
  101. temp.getDesc(), temp.getEmail(), temp.getProfilePic());
  102. cm.setNeedsReturn(false);
  103. } else if (cm.getType().equals("addNotification")) {
  104. Notification temp = cm.getNotification();
  105. addNotifToDatabase(temp);
  106. }
  107. synchronized (serverThreads) {
  108. if (cm.isNeedsReturn()) {
  109. for (ServerThread st : serverThreads) {
  110. st.sendMessage(cm);
  111. }
  112. }
  113. }
  114. }
  115. }
  116.  
  117. public void addNotifToDatabase(Notification notification) {
  118. try {
  119. Class.forName("com.mysql.jdbc.Driver");
  120. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  121. st = conn.createStatement();
  122.  
  123. String updateinfo = "INSERT INTO `notification`(`username`, `message`) VALUES(?,?)";
  124. try {
  125. PreparedStatement ps = conn.prepareStatement(updateinfo);
  126. ps.setString(1, notification.getUsername());
  127. ps.setString(2, notification.getMessage());
  128. while (rs.next()) {
  129. String user = rs.getString("username");
  130. String message = rs.getString("message");
  131. Notification temp = new Notification(user, message);
  132. pendingNotifications.add(temp);
  133. }
  134. } catch (SQLException sql) {
  135. System.out.println("sqle:" + sql.getMessage());
  136. }
  137. } catch (SQLException sql) {
  138. System.out.println("sqle:" + sql.getMessage());
  139. } catch (ClassNotFoundException e1) {
  140. // TODO Auto-generated catch block
  141. // e1.printStackTrace();
  142. } finally {
  143. try {
  144. if (rs != null)
  145. rs.close();
  146. if (st != null)
  147. st.close();
  148. if (conn != null)
  149. conn.close();
  150. } catch (SQLException sql) {
  151. System.out.println("sqle:" + sql.getMessage());
  152. }
  153. }
  154. }
  155.  
  156. @Override
  157. public void run() {
  158. while (true) {
  159. System.out.println("waiting for connection...");
  160. Socket s;
  161. try {
  162. s = ss.accept();
  163. System.out.println("Connection from " + s.getInetAddress());
  164. ServerThread st = new ServerThread(s, this, threadID);
  165. ++threadID;
  166. serverThreads.add(st);
  167. } catch (IOException e) {
  168. // TODO Auto-generated catch block
  169. e.printStackTrace();
  170. }
  171.  
  172. }
  173. }
  174.  
  175. public boolean login(String Username, String Password, String type, Message cm) {
  176. boolean found = false;
  177. try {
  178. Class.forName("com.mysql.jdbc.Driver");
  179. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  180. st = conn.createStatement();
  181. String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
  182. try {
  183. PreparedStatement ps = conn.prepareStatement(selectName);
  184. ps.setString(1, Username);
  185. rs = ps.executeQuery();
  186. while (rs.next()) {
  187. if (rs.getString(2).equals(Password)) {
  188. found = true;
  189.  
  190. // If the type is employer
  191. if (type.equals("employer")) {
  192. String Company = rs.getString(3);
  193. String Email = rs.getString(4);
  194. String Pos = rs.getString(5);
  195. String Path = rs.getString(6);
  196. String Bio = rs.getString(7);
  197. Employer e = new Employer(Username);
  198. e.setCompany(Company);
  199. e.setEmail(Email);
  200. e.setPosition(Pos);
  201. e.setLogo(Path);
  202. e.setDesc(Bio);
  203. cm.setEmployer(e);
  204. } else if (type.equals("professor")) {
  205. Professor temp = new Professor(Username);
  206. temp.setSchool(rs.getString(1));
  207. temp.setFirstName(rs.getString(2));
  208. temp.setLastName(rs.getString(3));
  209. temp.setEmail(rs.getString(4));
  210. temp.setProfilePic(rs.getString(5));
  211. temp.setBio(rs.getString(6));
  212. cm.setProfessor(temp);
  213.  
  214. } else if (type.equals("student")) {
  215. Student temp = new Student(Username);
  216. temp.setSchool(rs.getString(3));
  217. temp.setFirstName(rs.getString(4));
  218. temp.setLastName(rs.getString(5));
  219. temp.setEmail(rs.getString(6));
  220. temp.setProfilePic(rs.getString(7));
  221. temp.setBio(rs.getString(9));
  222. temp.setSchool(rs.getString(10));
  223. temp.setMajor(rs.getString(11));
  224. temp.setGPA(rs.getString(12));
  225. cm.setStudent(temp);
  226. }
  227. }
  228. }
  229.  
  230. } catch (SQLException sql) {
  231. System.out.println("sqle: line 131 " + sql.getMessage());
  232. sql.printStackTrace();
  233. }
  234. } catch (SQLException sql) {
  235. System.out.println("sqle: line 136" + sql.getMessage());
  236. } catch (ClassNotFoundException e1) {
  237. // TODO Auto-generated catch block
  238. // e1.printStackTrace();
  239. } finally {
  240. try {
  241. if (rs != null)
  242. rs.close();
  243. if (st != null)
  244. st.close();
  245. if (conn != null)
  246. conn.close();
  247. } catch (SQLException sqle) {
  248. System.out.println("sql problem");
  249. }
  250. }
  251. return found;
  252.  
  253. }
  254.  
  255. public int create(String Username, String Password, String type) {
  256. // 0 = success, 1 = invalid password requirements, 2 = result already
  257. // exists
  258. int success = -1;
  259. LengthRule lengthRule = new LengthRule(8, 16);
  260. WhitespaceRule whitespaceRule = new WhitespaceRule();
  261. CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
  262. charRule.getRules().add(new DigitCharacterRule(1));
  263. charRule.getRules().add(new NonAlphanumericCharacterRule(1));
  264. charRule.getRules().add(new UppercaseCharacterRule(1));
  265. charRule.getRules().add(new LowercaseCharacterRule(1));
  266. List<Rule> ruleList = new ArrayList<Rule>();
  267.  
  268. ruleList.add(lengthRule);
  269.  
  270. ruleList.add(whitespaceRule);
  271.  
  272. ruleList.add(charRule);
  273.  
  274. PasswordValidator validator = new PasswordValidator(ruleList);
  275.  
  276. PasswordData passwordData = new PasswordData(new Password(Password));
  277.  
  278. RuleResult result = validator.validate(passwordData);
  279. if (result.isValid()) {
  280. try {
  281. Class.forName("com.mysql.jdbc.Driver");
  282. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  283. st = conn.createStatement();
  284. String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
  285. String addinfo = "INSERT INTO `" + type + "`(`username`, `password`,`first`, `last`) VALUES(?,?,?,?)";
  286. try {
  287. PreparedStatement ps = conn.prepareStatement(selectName);
  288. PreparedStatement ps2 = conn.prepareStatement(addinfo);
  289. ps.setString(1, Username);
  290. rs = ps.executeQuery();
  291. while (rs.next()) {
  292. success = 2;
  293. }
  294. if (success != 2) {
  295. success = 0;
  296. ps2.setString(1, Username);
  297. ps2.setString(2, Password);
  298. ps2.setString(3, "");
  299. ps2.setString(4, "");
  300. ps2.executeUpdate();
  301. }
  302. } catch (SQLException sql) {
  303. System.out.println("sqle: 206 " + sql.getMessage());
  304. }
  305. } catch (SQLException sql) {
  306. System.out.println("sqle: 209 " + sql.getMessage());
  307. } catch (ClassNotFoundException e1) {
  308. // TODO Auto-generated catch block
  309. e1.printStackTrace();
  310. } finally {
  311. try {
  312. if (rs != null)
  313. rs.close();
  314. if (st != null)
  315. st.close();
  316. if (conn != null)
  317. conn.close();
  318. } catch (SQLException sqle) {
  319. System.out.println("sql problem");
  320. }
  321. }
  322. } else {
  323. success = 1;
  324. }
  325. System.out.println("Server creation of " + Username + " Finished with sucess " + success);
  326. return success;
  327. }
  328.  
  329. public void studentRegistration(String fname, String lname, String school, String email, String username,
  330. String bio, String resume, String profPic) {
  331. try {
  332. Class.forName("com.mysql.jdbc.Driver");
  333. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  334. st = conn.createStatement();
  335. String updateinfo = "UPDATE `student` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? , `pathResume`=? ,`bio`=? WHERE `username` = ?";
  336. try {
  337. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  338.  
  339. ps2.setString(1, fname);
  340. ps2.setString(2, lname);
  341. ps2.setString(3, school);
  342. ps2.setString(4, email);
  343. ps2.setString(5, "temp"); // profpic
  344. ps2.setString(6, "temp"); // resume
  345. ps2.setString(7, bio);
  346. ps2.setString(8, username);
  347. ps2.executeUpdate();
  348. } catch (SQLException sql) {
  349. System.out.println("sqle:" + sql.getMessage());
  350. }
  351. } catch (SQLException sql) {
  352. System.out.println("sqle:" + sql.getMessage());
  353. } catch (ClassNotFoundException e1) {
  354. // TODO Auto-generated catch block
  355. // e1.printStackTrace();
  356. } finally {
  357. try {
  358. if (rs != null)
  359. rs.close();
  360. if (st != null)
  361. st.close();
  362. if (conn != null)
  363. conn.close();
  364. } catch (SQLException sql) {
  365. System.out.println("sqle:" + sql.getMessage());
  366. }
  367. }
  368.  
  369. }
  370.  
  371. public void professorRegistration(String fname, String lname, String school, String email, String username,
  372. String bio, String profPic) {
  373. try {
  374. Class.forName("com.mysql.jdbc.Driver");
  375. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  376. st = conn.createStatement();
  377. String updateinfo = "UPDATE `professor` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? ,`bio`=? WHERE `username` = ?";
  378. try {
  379. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  380.  
  381. ps2.setString(1, fname);
  382. ps2.setString(2, lname);
  383. ps2.setString(3, school);
  384. ps2.setString(4, email);
  385. ps2.setString(5, "temp"); // profpic
  386. ps2.setString(6, bio);
  387. ps2.setString(7, username);
  388. ps2.executeUpdate();
  389. } catch (SQLException sql) {
  390. System.out.println("sqle:" + sql.getMessage());
  391. }
  392. } catch (SQLException sql) {
  393. System.out.println("sqle:" + sql.getMessage());
  394. } catch (ClassNotFoundException e1) {
  395. // TODO Auto-generated catch block
  396. // e1.printStackTrace();
  397. } finally {
  398. try {
  399. if (rs != null)
  400. rs.close();
  401. if (st != null)
  402. st.close();
  403. if (conn != null)
  404. conn.close();
  405. } catch (SQLException sql) {
  406. System.out.println("sqle:" + sql.getMessage());
  407. }
  408. }
  409. }
  410.  
  411. public void employerRegistration(String companyName, String posComp, String phone, String username, String bio,
  412. String email, String logo) {
  413. try {
  414. Class.forName("com.mysql.jdbc.Driver");
  415. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  416. st = conn.createStatement();
  417. String updateinfo = "UPDATE `employer` SET `company`=? ,`email`=? ,`bio`=? , `position`=? ,`pathLogo`=? ,`phone`=? WHERE `username` = ?";
  418. try {
  419. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  420.  
  421. ps2.setString(1, companyName);
  422. ps2.setString(2, email);
  423. ps2.setString(3, bio);
  424. ps2.setString(4, posComp);
  425. ps2.setString(5, logo);
  426. ps2.setString(6, phone);
  427. ps2.setString(8, username);
  428.  
  429. ps2.executeUpdate();
  430. } catch (SQLException sql) {
  431. System.out.println("sqle:" + sql.getMessage());
  432. }
  433. } catch (SQLException sql) {
  434. System.out.println("sqle:" + sql.getMessage());
  435. } catch (ClassNotFoundException e1) {
  436. // TODO Auto-generated catch block
  437. // e1.printStackTrace();
  438. } finally {
  439. try {
  440. if (rs != null)
  441. rs.close();
  442. if (st != null)
  443. st.close();
  444. if (conn != null)
  445. conn.close();
  446. } catch (SQLException sql) {
  447. System.out.println("sqle:" + sql.getMessage());
  448. }
  449. }
  450. }
  451.  
  452. private void getPendingNotifications() {
  453. try {
  454. Class.forName("com.mysql.jdbc.Driver");
  455. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  456. st = conn.createStatement();
  457.  
  458. String updateinfo = "SELECT * FROM `notification`";
  459. try {
  460. rs = st.executeQuery(updateinfo);
  461. while (rs.next()) {
  462. String user = rs.getString("username");
  463. String message = rs.getString("message");
  464. Notification temp = new Notification(user, message);
  465. pendingNotifications.add(temp);
  466. }
  467. } catch (SQLException sql) {
  468. System.out.println("sqle:" + sql.getMessage());
  469. }
  470. } catch (SQLException sql) {
  471. System.out.println("sqle:" + sql.getMessage());
  472. } catch (ClassNotFoundException e1) {
  473. // TODO Auto-generated catch block
  474. // e1.printStackTrace();
  475. } finally {
  476. try {
  477. if (rs != null)
  478. rs.close();
  479. if (st != null)
  480. st.close();
  481. if (conn != null)
  482. conn.close();
  483. } catch (SQLException sql) {
  484. System.out.println("sqle:" + sql.getMessage());
  485. }
  486. }
  487. }
  488. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement