Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package controllers;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11.  
  12.  
  13. import conexiones.BoneCPConnection;
  14. import conexiones.ConnectionInterface;
  15. import conexiones.HikaryCPConnection;
  16.  
  17. //import com.google.gson.Gson;
  18.  
  19. /**
  20. * Servlet implementation class Control
  21. */
  22. public class Control extends HttpServlet {
  23. private static final long serialVersionUID = 1L;
  24.  
  25. /**
  26. * Default constructor.
  27. */
  28. public Control() {
  29. // TODO Auto-generated constructor stub
  30. }
  31.  
  32. /**
  33. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  34. * response)
  35. */
  36.  
  37.  
  38. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  39. throws ServletException, IOException {
  40. // TODO Auto-generated method stub
  41. // response.getWriter().append("Served at: ").append(request.getContextPath());
  42. response.setContentType("application/json");
  43.  
  44. HttpSession oSession = request.getSession();
  45. String strOp = request.getParameter("op");
  46. String strJson = "";
  47. // Gson gson = new Gson();
  48. if (strOp != null) {
  49. if (strOp.equalsIgnoreCase("")) {
  50. response.setStatus(500);
  51. strJson = strJson(500, "No ha solicitado ninguna operacion.");
  52. } else {
  53. try {
  54. Class.forName("com.mysql.jdbc.Driver");
  55.  
  56. } catch (Exception ex) {
  57. strJson = strJson(500, "Driver no encontrado");
  58. }
  59. Connection conn;
  60. ConnectionInterface connectionInterface = null;
  61. if (strOp.equalsIgnoreCase("bonecp")) {
  62. try {
  63. connectionInterface = new BoneCPConnection();
  64. conn = connectionInterface.newConnection();
  65. strJson = strJson(200, "Conexion realizada");
  66. } catch (Exception e) {
  67. strJson = strJson(500, "Conexion fallada");
  68. }
  69. }
  70. if (strOp.equalsIgnoreCase("hikary")) {
  71. try {
  72. connectionInterface = new HikaryCPConnection();
  73. conn = connectionInterface.newConnection();
  74. strJson = strJson(200, "Conexion realizada");
  75. } catch (Exception e) {
  76. strJson = strJson(500, "Conexion fallada");
  77. }
  78. }
  79.  
  80. if (strOp.equalsIgnoreCase("login")) {
  81. String strUser = request.getParameter("user");
  82. String strPass = request.getParameter("pass");
  83. if (strUser != null && strPass != null) {
  84. if (strUser.equals("hec3555") && strPass.equals("babydriver")) {
  85. oSession.setAttribute("sesionvar", strUser);
  86. response.setStatus(200);
  87. strJson = strJson(200, "You are logged in.");
  88. } else {
  89. response.setStatus(401);
  90. strJson = strJson(401, "Authentication error");
  91. }
  92. } else {
  93. strJson = strJson(401, "User or pass not specified.");
  94. }
  95. }
  96. if (strOp.equalsIgnoreCase("logout")) {
  97. oSession.invalidate();
  98. response.setStatus(200);
  99. strJson = strJson(200, "Session is closed.");
  100. }
  101. if (strOp.equalsIgnoreCase("check")) {
  102. String strUserName = (String) oSession.getAttribute("sesionvar");
  103. if (strUserName == null) {
  104. response.setStatus(401);
  105. strJson = strJson(401, "You are NOT logged in.");
  106. } else {
  107. response.setStatus(200);
  108. strJson = strJson(200, "You are logged in.");
  109. }
  110. }
  111. if (strOp.equalsIgnoreCase("secret")) {
  112. String strUserName = (String) oSession.getAttribute("sesionvar");
  113. if (strUserName == null) {
  114. response.setStatus(401);
  115. strJson = strJson(401, "You are NOT allowed to show this message.");
  116. } else {
  117. response.setStatus(200);
  118. strJson = strJson(200, "El mensaje es.... NADA TIE SENTIDO FOLAGOR");
  119. }
  120. }
  121. }
  122. } else {
  123. response.setStatus(500);
  124. strJson = strJson(500, "No ha solicitado ninguna operacion.");
  125. }
  126.  
  127. response.getWriter().append(strJson);
  128.  
  129. }
  130.  
  131. public String strJson(int status, String msg) {
  132. String strJsonM = "{\"status\":" + status + ",\"msg\":\"" + msg + "\"}";
  133. return strJsonM;
  134. }
  135.  
  136. /**
  137. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  138. * response)
  139. */
  140. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  141. throws ServletException, IOException {
  142. // TODO Auto-generated method stub
  143. doGet(request, response);
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement