/server/src/main/java/ua/papka24/server/api/Redis.java

https://github.com/Papka24/base · Java · 149 lines · 94 code · 12 blank · 43 comment · 15 complexity · 7815c9b6f2b4c332825e480c837ac1fd MD5 · raw file

  1. /*
  2. * Copyright (c) 2017. iDoc LLC
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * (1) Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * (2) Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * (3)The name of the author may not be used to
  17. * endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package ua.papka24.server.api;
  33. import com.google.gson.Gson;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import ua.papka24.server.db.redis.model.LoginRedisInfo;
  37. import ua.papka24.server.security.SecurityAttributes;
  38. import ua.papka24.server.db.dao.UserDAO;
  39. import ua.papka24.server.db.redis.RedisDAOManager;
  40. import ua.papka24.server.security.Session;
  41. import ua.papka24.server.security.SessionsPool;
  42. import javax.ws.rs.*;
  43. import javax.ws.rs.core.MediaType;
  44. import javax.ws.rs.core.Response;
  45. import java.util.List;
  46. @Path("redis")
  47. public class Redis extends REST{
  48. private static final Logger log = LoggerFactory.getLogger(Redis.class);
  49. private static final Gson gson = new Gson();
  50. @GET
  51. @Produces(MediaType.APPLICATION_JSON)
  52. public Response get(@HeaderParam("sessionId") String sessionId){
  53. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.READ_ADMIN, SecurityAttributes.WRITE_ADMIN);
  54. if (s == null) {
  55. return ERROR_FORBIDDEN;
  56. } else {
  57. List<LoginRedisInfo> sessions = RedisDAOManager.getInstance().getSessions();
  58. return Response.ok().entity(new Gson().toJson(sessions)).build();
  59. }
  60. }
  61. @GET
  62. @Path("/invalidateAll")
  63. @Produces(MediaType.APPLICATION_JSON)
  64. public Response invalidateAll(@HeaderParam("sessionid") String sessionId){
  65. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.READ_ADMIN, SecurityAttributes.WRITE_ADMIN);
  66. if (s == null) {
  67. return ERROR_FORBIDDEN;
  68. }
  69. RedisDAOManager.getInstance().invalidateAllUsersSessions();
  70. return Response.ok().build();
  71. }
  72. @GET
  73. @Path("/invalidate/{login}")
  74. @Produces(MediaType.APPLICATION_JSON)
  75. public Response invalidate(@HeaderParam("sessionid") String sessionId, @PathParam("login") String login){
  76. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.READ_ADMIN, SecurityAttributes.WRITE_ADMIN);
  77. if (s == null) {
  78. return ERROR_FORBIDDEN;
  79. }
  80. RedisDAOManager.getInstance().markChanged(login);
  81. return Response.ok().build();
  82. }
  83. @GET
  84. @Produces(MediaType.APPLICATION_JSON)
  85. @Path("/list")
  86. public Response getUsersInfo(@HeaderParam("sessionid") String sessionId){
  87. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.READ_ADMIN, SecurityAttributes.WRITE_ADMIN);
  88. if (s == null) {
  89. return ERROR_FORBIDDEN;
  90. }
  91. try {
  92. List<LoginRedisInfo> sessionsInfo = RedisDAOManager.getInstance().getSessionsInfo();
  93. return Response.ok().entity(gson.toJson(sessionsInfo)).build();
  94. }catch (Exception ex) {
  95. log.error("list error", ex);
  96. }
  97. return ERROR_SERVER;
  98. }
  99. //carefully!!
  100. // @GET
  101. // @Produces(MediaType.APPLICATION_JSON)
  102. // @Path("flushall")
  103. // public Response flushAll(@HeaderParam("sessionid") String sessionId){
  104. // Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.WRITE_ADMIN);
  105. // if (s == null) {
  106. // return ERROR_FORBIDDEN;
  107. // }
  108. // RedisDAOManager.getInstance().flushAll();
  109. // return Response.ok().build();
  110. // }
  111. @DELETE
  112. @Produces(MediaType.APPLICATION_JSON)
  113. @Path("resetall")
  114. public Response reset(@HeaderParam("sessionid") String sessionId){
  115. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.WRITE_ADMIN);
  116. if (s == null) {
  117. return ERROR_FORBIDDEN;
  118. }
  119. UserDAO.getInstance().list().forEach(RedisDAOManager.getInstance()::markChanged);
  120. return Response.ok().build();
  121. }
  122. @GET
  123. @Produces(MediaType.APPLICATION_JSON)
  124. @Path("emails/clean")
  125. public Response cleanEmailList(@HeaderParam("sessionid") String sessionId){
  126. Session s = SessionsPool.findWithRight(sessionId, SecurityAttributes.READ_ADMIN, SecurityAttributes.WRITE_ADMIN);
  127. if (s == null) {
  128. return ERROR_FORBIDDEN;
  129. }
  130. Long aLong = RedisDAOManager.getInstance().cleanEmailList();
  131. if(aLong == -1){
  132. return ERROR_SERVER;
  133. }else {
  134. return Response.ok().build();
  135. }
  136. }
  137. }