Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.12 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. ps.executeQuery();
  129.  
  130. } catch (SQLException sql) {
  131. System.out.println("sqle:" + sql.getMessage());
  132. }
  133. } catch (SQLException sql) {
  134. System.out.println("sqle:" + sql.getMessage());
  135. } catch (ClassNotFoundException e1) {
  136. // TODO Auto-generated catch block
  137. // e1.printStackTrace();
  138. } finally {
  139. try {
  140. if (rs != null)
  141. rs.close();
  142. if (st != null)
  143. st.close();
  144. if (conn != null)
  145. conn.close();
  146. } catch (SQLException sql) {
  147. System.out.println("sqle:" + sql.getMessage());
  148. }
  149. }
  150. }
  151.  
  152. @Override
  153. public void run() {
  154. while (true) {
  155. System.out.println("waiting for connection...");
  156. Socket s;
  157. try {
  158. s = ss.accept();
  159. System.out.println("Connection from " + s.getInetAddress());
  160. ServerThread st = new ServerThread(s, this, threadID);
  161. ++threadID;
  162. serverThreads.add(st);
  163. } catch (IOException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. }
  167.  
  168. }
  169. }
  170.  
  171. public boolean login(String Username, String Password, String type, Message cm) {
  172. boolean found = false;
  173. try {
  174. Class.forName("com.mysql.jdbc.Driver");
  175. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  176. st = conn.createStatement();
  177. String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
  178. try {
  179. PreparedStatement ps = conn.prepareStatement(selectName);
  180. ps.setString(1, Username);
  181. rs = ps.executeQuery();
  182. while (rs.next()) {
  183. if (rs.getString(2).equals(Password)) {
  184. found = true;
  185.  
  186. // If the type is employer
  187. if (type.equals("employer")) {
  188. String Company = rs.getString(3);
  189. String Email = rs.getString(4);
  190. String Pos = rs.getString(5);
  191. String Path = rs.getString(6);
  192. String Bio = rs.getString(7);
  193. Employer e = new Employer(Username);
  194. e.setCompany(Company);
  195. e.setEmail(Email);
  196. e.setPosition(Pos);
  197. e.setLogo(Path);
  198. e.setDesc(Bio);
  199. cm.setEmployer(e);
  200. } else if (type.equals("professor")) {
  201. Professor temp = new Professor(Username);
  202. temp.setSchool(rs.getString(1));
  203. temp.setFirstName(rs.getString(2));
  204. temp.setLastName(rs.getString(3));
  205. temp.setEmail(rs.getString(4));
  206. temp.setProfilePic(rs.getString(5));
  207. temp.setBio(rs.getString(6));
  208. cm.setProfessor(temp);
  209.  
  210. } else if (type.equals("student")) {
  211. Student temp = new Student(Username);
  212. temp.setSchool(rs.getString(3));
  213. temp.setFirstName(rs.getString(4));
  214. temp.setLastName(rs.getString(5));
  215. temp.setEmail(rs.getString(6));
  216. temp.setProfilePic(rs.getString(7));
  217. temp.setBio(rs.getString(9));
  218. temp.setSchool(rs.getString(10));
  219. temp.setMajor(rs.getString(11));
  220. temp.setGPA(rs.getString(12));
  221. cm.setStudent(temp);
  222. }
  223. }
  224. }
  225.  
  226. } catch (SQLException sql) {
  227. System.out.println("sqle: line 131 " + sql.getMessage());
  228. sql.printStackTrace();
  229. }
  230. } catch (SQLException sql) {
  231. System.out.println("sqle: line 136" + sql.getMessage());
  232. } catch (ClassNotFoundException e1) {
  233. // TODO Auto-generated catch block
  234. // e1.printStackTrace();
  235. } finally {
  236. try {
  237. if (rs != null)
  238. rs.close();
  239. if (st != null)
  240. st.close();
  241. if (conn != null)
  242. conn.close();
  243. } catch (SQLException sqle) {
  244. System.out.println("sql problem");
  245. }
  246. }
  247. return found;
  248.  
  249. }
  250.  
  251. public int create(String Username, String Password, String type) {
  252. // 0 = success, 1 = invalid password requirements, 2 = result already
  253. // exists
  254. int success = -1;
  255. LengthRule lengthRule = new LengthRule(8, 16);
  256. WhitespaceRule whitespaceRule = new WhitespaceRule();
  257. CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
  258. charRule.getRules().add(new DigitCharacterRule(1));
  259. charRule.getRules().add(new NonAlphanumericCharacterRule(1));
  260. charRule.getRules().add(new UppercaseCharacterRule(1));
  261. charRule.getRules().add(new LowercaseCharacterRule(1));
  262. List<Rule> ruleList = new ArrayList<Rule>();
  263.  
  264. ruleList.add(lengthRule);
  265.  
  266. ruleList.add(whitespaceRule);
  267.  
  268. ruleList.add(charRule);
  269.  
  270. PasswordValidator validator = new PasswordValidator(ruleList);
  271.  
  272. PasswordData passwordData = new PasswordData(new Password(Password));
  273.  
  274. RuleResult result = validator.validate(passwordData);
  275. if (result.isValid()) {
  276. try {
  277. Class.forName("com.mysql.jdbc.Driver");
  278. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  279. st = conn.createStatement();
  280. String selectName = "SELECT * FROM `" + type + "` WHERE `username`=?";
  281. String addinfo = "INSERT INTO `" + type + "`(`username`, `password`,`first`, `last`) VALUES(?,?,?,?)";
  282. try {
  283. PreparedStatement ps = conn.prepareStatement(selectName);
  284. PreparedStatement ps2 = conn.prepareStatement(addinfo);
  285. ps.setString(1, Username);
  286. rs = ps.executeQuery();
  287. while (rs.next()) {
  288. success = 2;
  289. }
  290. if (success != 2) {
  291. success = 0;
  292. ps2.setString(1, Username);
  293. ps2.setString(2, Password);
  294. ps2.setString(3, "");
  295. ps2.setString(4, "");
  296. ps2.executeUpdate();
  297. }
  298. } catch (SQLException sql) {
  299. System.out.println("sqle: 206 " + sql.getMessage());
  300. }
  301. } catch (SQLException sql) {
  302. System.out.println("sqle: 209 " + sql.getMessage());
  303. } catch (ClassNotFoundException e1) {
  304. // TODO Auto-generated catch block
  305. e1.printStackTrace();
  306. } finally {
  307. try {
  308. if (rs != null)
  309. rs.close();
  310. if (st != null)
  311. st.close();
  312. if (conn != null)
  313. conn.close();
  314. } catch (SQLException sqle) {
  315. System.out.println("sql problem");
  316. }
  317. }
  318. } else {
  319. success = 1;
  320. }
  321. System.out.println("Server creation of " + Username + " Finished with sucess " + success);
  322. return success;
  323. }
  324.  
  325. public void studentRegistration(String fname, String lname, String school, String email, String username,
  326. String bio, String resume, String profPic) {
  327. try {
  328. Class.forName("com.mysql.jdbc.Driver");
  329. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  330. st = conn.createStatement();
  331. String updateinfo = "UPDATE `student` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? , `pathResume`=? ,`bio`=? WHERE `username` = ?";
  332. try {
  333. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  334.  
  335. ps2.setString(1, fname);
  336. ps2.setString(2, lname);
  337. ps2.setString(3, school);
  338. ps2.setString(4, email);
  339. ps2.setString(5, "temp"); // profpic
  340. ps2.setString(6, "temp"); // resume
  341. ps2.setString(7, bio);
  342. ps2.setString(8, username);
  343. ps2.executeUpdate();
  344. } catch (SQLException sql) {
  345. System.out.println("sqle:" + sql.getMessage());
  346. }
  347. } catch (SQLException sql) {
  348. System.out.println("sqle:" + sql.getMessage());
  349. } catch (ClassNotFoundException e1) {
  350. // TODO Auto-generated catch block
  351. // e1.printStackTrace();
  352. } finally {
  353. try {
  354. if (rs != null)
  355. rs.close();
  356. if (st != null)
  357. st.close();
  358. if (conn != null)
  359. conn.close();
  360. } catch (SQLException sql) {
  361. System.out.println("sqle:" + sql.getMessage());
  362. }
  363. }
  364.  
  365. }
  366.  
  367. public void professorRegistration(String fname, String lname, String school, String email, String username,
  368. String bio, String profPic) {
  369. try {
  370. Class.forName("com.mysql.jdbc.Driver");
  371. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  372. st = conn.createStatement();
  373. String updateinfo = "UPDATE `professor` SET `first`=? ,`last`=? ,`school`=? , `email`=? ,`pathPicture`=? ,`bio`=? WHERE `username` = ?";
  374. try {
  375. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  376.  
  377. ps2.setString(1, fname);
  378. ps2.setString(2, lname);
  379. ps2.setString(3, school);
  380. ps2.setString(4, email);
  381. ps2.setString(5, "temp"); // profpic
  382. ps2.setString(6, bio);
  383. ps2.setString(7, username);
  384. ps2.executeUpdate();
  385. } catch (SQLException sql) {
  386. System.out.println("sqle:" + sql.getMessage());
  387. }
  388. } catch (SQLException sql) {
  389. System.out.println("sqle:" + sql.getMessage());
  390. } catch (ClassNotFoundException e1) {
  391. // TODO Auto-generated catch block
  392. // e1.printStackTrace();
  393. } finally {
  394. try {
  395. if (rs != null)
  396. rs.close();
  397. if (st != null)
  398. st.close();
  399. if (conn != null)
  400. conn.close();
  401. } catch (SQLException sql) {
  402. System.out.println("sqle:" + sql.getMessage());
  403. }
  404. }
  405. }
  406.  
  407. public void employerRegistration(String companyName, String posComp, String phone, String username, String bio,
  408. String email, String logo) {
  409. try {
  410. Class.forName("com.mysql.jdbc.Driver");
  411. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  412. st = conn.createStatement();
  413. String updateinfo = "UPDATE `employer` SET `company`=? ,`email`=? ,`bio`=? , `position`=? ,`pathLogo`=? ,`phone`=? WHERE `username` = ?";
  414. try {
  415. PreparedStatement ps2 = conn.prepareStatement(updateinfo);
  416.  
  417. ps2.setString(1, companyName);
  418. ps2.setString(2, email);
  419. ps2.setString(3, bio);
  420. ps2.setString(4, posComp);
  421. ps2.setString(5, logo);
  422. ps2.setString(6, phone);
  423. ps2.setString(8, username);
  424.  
  425. ps2.executeUpdate();
  426. } catch (SQLException sql) {
  427. System.out.println("sqle:" + sql.getMessage());
  428. }
  429. } catch (SQLException sql) {
  430. System.out.println("sqle:" + sql.getMessage());
  431. } catch (ClassNotFoundException e1) {
  432. // TODO Auto-generated catch block
  433. // e1.printStackTrace();
  434. } finally {
  435. try {
  436. if (rs != null)
  437. rs.close();
  438. if (st != null)
  439. st.close();
  440. if (conn != null)
  441. conn.close();
  442. } catch (SQLException sql) {
  443. System.out.println("sqle:" + sql.getMessage());
  444. }
  445. }
  446. }
  447.  
  448. private void getPendingNotifications() {
  449. try {
  450. Class.forName("com.mysql.jdbc.Driver");
  451. conn = DriverManager.getConnection("jdbc:mysql://localhost/TheInternsClub?user=root&useSSL=false");
  452. st = conn.createStatement();
  453.  
  454. String updateinfo = "SELECT * FROM `notification`";
  455. try {
  456. rs = st.executeQuery(updateinfo);
  457. while (rs.next()) {
  458. String user = rs.getString("username");
  459. String message = rs.getString("message");
  460. Notification temp = new Notification(user, message);
  461. pendingNotifications.add(temp);
  462. }
  463. } catch (SQLException sql) {
  464. System.out.println("sqle:" + sql.getMessage());
  465. }
  466. } catch (SQLException sql) {
  467. System.out.println("sqle:" + sql.getMessage());
  468. } catch (ClassNotFoundException e1) {
  469. // TODO Auto-generated catch block
  470. // e1.printStackTrace();
  471. } finally {
  472. try {
  473. if (rs != null)
  474. rs.close();
  475. if (st != null)
  476. st.close();
  477. if (conn != null)
  478. conn.close();
  479. } catch (SQLException sql) {
  480. System.out.println("sqle:" + sql.getMessage());
  481. }
  482. }
  483. }
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement