PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/Application.java

https://bitbucket.org/iliax/onlinechecker
Java | 212 lines | 175 code | 33 blank | 4 comment | 20 complexity | 0127939bb503174f7837ac1384e26e11 MD5 | raw file
Possible License(s): LGPL-3.0
  1. package controllers;
  2. import static util.StaticDataHolder.dbm;
  3. import static util.StaticDataHolder.lsHolder;
  4. import static util.StaticDataHolder.dbw;
  5. import static util.VkMotitorConstants.URL_BEGINING;
  6. import static util.VkMotitorConstants.URL_ENDING;
  7. import jobs.JobInvoker;
  8. import play.Logger;
  9. import play.Play;
  10. import play.libs.WS.HttpResponse;
  11. import play.libs.WS.WSRequest;
  12. import play.mvc.Controller;
  13. import util.ServerStatus;
  14. import com.google.gson.JsonArray;
  15. import com.google.gson.JsonElement;
  16. import com.google.gson.JsonParser;
  17. import com.mongodb.BasicDBObject;
  18. import com.mongodb.DBCursor;
  19. import com.mongodb.DBObject;
  20. public class Application extends Controller {
  21. public static final JobInvoker INVOKER = new JobInvoker();
  22. public static void index() {
  23. render();
  24. }
  25. public static void why() {
  26. render();
  27. }
  28. public static void aboutus() {
  29. render();
  30. }
  31. /** Хак, чтобы получить нормальный урл */
  32. public static void show1(String targetId) {
  33. show(targetId);
  34. }
  35. public static void show(String targetId) {
  36. try {
  37. String[] pieces = targetId.split("vk\\.com\\/");
  38. targetId = pieces[pieces.length-1];
  39. ConciseStats stats = getConciseStats(targetId);
  40. if (stats == null) {
  41. invalidid();
  42. }
  43. String uid = stats.getUserId();
  44. String online = stats.getStatus();
  45. if (lsHolder.getStatus(uid) == null) {
  46. addNewTarget(uid, online, targetId);
  47. }
  48. render(stats,targetId);
  49. } catch (Exception e) {
  50. invalidid();
  51. }
  52. }
  53. private static void addNewTarget(String uid, String online, String targetId) {
  54. dbw.saveNewRecord(uid, online,
  55. String.valueOf(System.currentTimeMillis()));
  56. lsHolder.addNew(uid, online);
  57. targetAdded(targetId);
  58. }
  59. public static ConciseStats getConciseStats(String name) {
  60. StringBuilder sb = new StringBuilder(URL_BEGINING);
  61. sb.append(name);
  62. sb.append(URL_ENDING);
  63. try {
  64. WSRequest request = play.libs.WS.url(sb.toString());
  65. request.timeout("10s");
  66. HttpResponse resp = request.get();
  67. if (resp.getStatus() != 200) {
  68. return null;
  69. }
  70. JsonElement json = new JsonParser().parse(resp.getString());
  71. JsonArray arr = json.getAsJsonObject().get("response")
  72. .getAsJsonArray();
  73. String online = arr.get(0).getAsJsonObject().get("online")
  74. .getAsString();
  75. String firstName = arr.get(0).getAsJsonObject().get("first_name").getAsString();
  76. String uid = arr.get(0).getAsJsonObject().get("uid").getAsString();
  77. String lastName = arr.get(0).getAsJsonObject().get("last_name").getAsString();
  78. String nickName = arr.get(0).getAsJsonObject().get("nickname").getAsString();
  79. return new ConciseStats(uid,online,firstName,lastName,nickName);
  80. } catch (Exception e) {
  81. Logger.error(e, "error while getting concise stats");
  82. return null;
  83. }
  84. }
  85. public static void targetAdded(String targetId) {
  86. render(targetId);
  87. }
  88. public static void invalidid() {
  89. render();
  90. }
  91. public static void admin_login(String admin) {
  92. if (admin == null || admin.trim().isEmpty()) {
  93. error();
  94. }
  95. render(admin);
  96. }
  97. // POST
  98. public static void admin_page(String admin, String password) {
  99. session.put("admin_login", admin);
  100. session.put("admin_passw", MD5(password));
  101. checkAdmin();
  102. render();
  103. }
  104. private static void checkAdmin() {
  105. String adminLogin = session.get("admin_login");
  106. String passw = session.get("admin_passw");
  107. if (adminLogin == null || passw == null) {
  108. error();
  109. }
  110. DBCursor dbcur = dbm.security.find(new BasicDBObject().append(
  111. "admin_login", adminLogin));
  112. if (dbcur.count() == 0) {
  113. error();
  114. }
  115. try {
  116. for (DBObject dbo : dbcur) {
  117. String passwHash = dbo.get("admin_passw").toString();
  118. if (passw.equals(passwHash)) {
  119. return;
  120. }
  121. }
  122. } finally {
  123. dbcur.close();
  124. }
  125. error();
  126. }
  127. // POST
  128. public static void _setClientMode() {
  129. checkAdmin();
  130. Logger.info("Client Mode was set from admin page");
  131. dbm.setServerStatus(ServerStatus.STOPPED,
  132. "client_mode set from admin page");
  133. INVOKER.stop();
  134. renderText("DONE!");
  135. }
  136. public static void _setServerMode() {
  137. checkAdmin();
  138. Logger.info("Server mode was set from admin page");
  139. dbm.setServerStatus(ServerStatus.STARTED,
  140. "server_mode set from admin page");
  141. INVOKER.start();
  142. renderText("DONE!");
  143. }
  144. // POST
  145. public static void _stopServer() {
  146. checkAdmin();
  147. dbm.setServerStatus(ServerStatus.STOPPED, "stopped from admin page");
  148. try {
  149. Thread.sleep(500);
  150. } catch (InterruptedException e) {
  151. e.printStackTrace();
  152. }
  153. Logger.info("Stopping Server from admin page");
  154. Play.stop();
  155. System.exit(0);
  156. }
  157. public static void _logoff() {
  158. session.clear();
  159. ok();
  160. }
  161. private static String MD5(String md5) {
  162. if (md5 == null) {
  163. return null;
  164. }
  165. try {
  166. java.security.MessageDigest md = java.security.MessageDigest
  167. .getInstance("MD5");
  168. byte[] array = md.digest(md5.getBytes());
  169. StringBuffer sb = new StringBuffer();
  170. for (int i = 0; i < array.length; ++i) {
  171. sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
  172. .substring(1, 3));
  173. }
  174. return sb.toString();
  175. } catch (java.security.NoSuchAlgorithmException e) {
  176. }
  177. return null;
  178. }
  179. }