Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. package io.everytrade.server.auth;
  2.  
  3. import io.everytrade.model.User;
  4. import io.everytrade.server.AppSettings;
  5. import io.everytrade.server.api.jpa.JPAUtil;
  6.  
  7. import javax.ejb.Stateless;
  8. import javax.inject.Inject;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.ws.rs.core.Context;
  11. import javax.ws.rs.core.Response;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.UUID;
  15.  
  16. @Stateless
  17. public class ResourceBase<X> {
  18. @Context
  19. private HttpServletRequest request;
  20.  
  21. @Inject
  22. protected JPAUtil jpa;
  23.  
  24. public User getUser() {
  25. try {
  26. return (User) request.getAttribute(AuthFilter.AUTH_ATTRIBUTE_USER);
  27. } catch (Exception e) {
  28. System.err.println("NO AUTH 1 - " + request);
  29. }
  30. return null;
  31. }
  32.  
  33. public boolean isSuperUser(){
  34. return "superuser@everytrade.io".equalsIgnoreCase(getUser().getEmail());
  35. }
  36.  
  37. public String getToken() {
  38. try {
  39. return (String) request.getAttribute(AuthFilter.AUTH_ATTRIBUTE_TOKEN);
  40. } catch (Exception e) {
  41. System.err.println("NO AUTH 2 - " + request);
  42. }
  43. return null;
  44. }
  45.  
  46. public AuthToken getAuthToken() throws Exception {
  47. return AuthServer.getInstance().getAuthTokenFromToken(getToken());
  48. }
  49.  
  50. protected static Response ok() {
  51. return ok(null);
  52. }
  53.  
  54. protected static Response ok(Object entity) {
  55. Response.ResponseBuilder resourceBuilder = entity != null ? Response.ok(entity) : Response.ok();
  56. return resourceBuilder.build();
  57. }
  58.  
  59. protected static Response error(int code) {
  60. return Response.status(code).build();
  61. }
  62.  
  63. protected static Response error(String message) {
  64. return error(new RuntimeException(message), null);
  65. }
  66.  
  67. protected Response error(Exception e) {
  68. return error(e, null);
  69. }
  70.  
  71. protected static Response error(Exception exception, String message) {
  72. Response.ResponseBuilder resourceBuilder = Response.status(400);//serverError();
  73. String errorCode = UUID.randomUUID().toString();
  74.  
  75. if (exception != null) {
  76. resourceBuilder.entity(new ErrorEntity(errorCode, exception));
  77. } else {
  78. resourceBuilder.entity(new ErrorEntity(errorCode, message));
  79. }
  80.  
  81. return resourceBuilder.build();
  82. }
  83.  
  84. private static class ErrorEntity {
  85.  
  86. private final String errorCode;
  87. private final Exception exception;
  88. private final List<String> errors;
  89.  
  90. public ErrorEntity(String errorCode, Exception exception) {
  91. this.errorCode = errorCode;
  92. this.exception = exception;
  93. this.errors = null;
  94. }
  95.  
  96. public ErrorEntity(String errorCode, String error) {
  97. this.errorCode = errorCode;
  98. this.exception = null;
  99. this.errors = new ArrayList<>();
  100. this.errors.add(error);
  101. }
  102.  
  103. @SuppressWarnings("unused")
  104. public String getErrorCode() {
  105. return errorCode;
  106. }
  107.  
  108. @SuppressWarnings("unused")
  109. public String getErrorMessage() {
  110. /*if (exception != null && ( AppSettings.isDev() || AppSettings.isStage() )) {
  111. StringBuilder builder = new StringBuilder();
  112. builder.append(exception.getClass().getName()).append(": ");
  113. builder.append(exception.getMessage());
  114. return builder.toString();
  115. } else {
  116.  
  117. }*/
  118. if(exception!=null){
  119. StringBuilder builder = new StringBuilder();
  120. //builder.append(exception.getClass().getName()).append(": ");
  121. builder.append(exception.getMessage());
  122. return builder.toString();
  123. }
  124. return "";
  125. }
  126.  
  127. @SuppressWarnings("unused")
  128. public StackTraceElement[] getStackTrace() {
  129. /*if (exception != null && ( AppSettings.isDev() || AppSettings.isStage() )) {
  130. return exception.getStackTrace();
  131. } else {
  132. return null;
  133. }*/
  134. if (exception != null){
  135. return exception.getStackTrace();
  136. }
  137. return null;
  138.  
  139. }
  140. }
  141.  
  142. /*
  143. @GET
  144. public List<T> getList() throws SQLException, NamingException {
  145. List records = getTixQuery();
  146. return records;
  147. }
  148.  
  149. @GET
  150. @Path("{id}")
  151. public List<T> getSingle(@PathParam("id") int id) throws NamingException {
  152. List records = getSingleQuery(id);
  153. return records;
  154. }
  155.  
  156. @POST
  157. public void insertTix(T t) throws NamingException, SQLException {
  158. createQuery(t);
  159. }
  160. */
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement