Guest User

Untitled

a guest
Mar 10th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. package com.jdriven.RoboAdvice.Controller;
  2.  
  3. import com.jdriven.RoboAdvice.DTO.CustomStrategyDTO;
  4. import com.jdriven.RoboAdvice.DTO.PortfolioDTO;
  5. import com.jdriven.RoboAdvice.DataModel.User;
  6. import com.jdriven.RoboAdvice.Model.GenericResponse;
  7. import com.jdriven.RoboAdvice.ServiceInterface.StrategyPerAssetService;
  8. import com.jdriven.RoboAdvice.ServiceInterface.ToUpdateQueueService;
  9. import com.jdriven.RoboAdvice.ServiceInterface.UserService;
  10. import com.jdriven.RoboAdvice.Utils.Constant;
  11. import com.jdriven.RoboAdvice.Utils.StrategyBuilder;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14.  
  15. import javax.crypto.SecretKeyFactory;
  16. import javax.crypto.spec.PBEKeySpec;
  17. import java.security.SecureRandom;
  18. import java.util.Base64;
  19. import java.util.NoSuchElementException;
  20.  
  21.  
  22. /**
  23. * Created by Davide on 01/03/17.
  24. */
  25.  
  26. @CrossOrigin
  27. @RestController
  28. public class SignupController {
  29. //https://www.quandl.com/api/v3/datasets/COM/WLD_RICE_05.json?api_key=xPf3zsdBERWBxoRwy6Tt
  30. @Autowired
  31. private UserService userService;
  32. @Autowired
  33. private ToUpdateQueueService toUpdateQueueService;
  34. @Autowired
  35. private StrategyPerAssetService strategyPerAssetService;
  36.  
  37. /* @PersistenceContext
  38. EntityManager em;
  39.  
  40. public List<AssetHeld> findAssetsHeldByUser(Long userID) {
  41.  
  42. return em.createNamedQuery("AssetClass.findAssetsHeldByUser").setParameter("uId",userID )
  43. .getResultList();
  44. }
  45. public List<StrategyPerAssetClass> findActiveStrategy(Long userID) {
  46.  
  47. return em.createNamedQuery("StrategyPerAssetClass.findActiveStrategy").setParameter("uId",userID )
  48. .getResultList();
  49. }*/
  50.  
  51.  
  52.  
  53. @RequestMapping(value = "/signup")
  54. public GenericResponse signUp(@RequestParam(value = "firstName") String firstName,
  55. @RequestParam(value = "lastName") String lastName,
  56. @RequestParam(value = "email") String email,
  57. @RequestParam(value = "password") String password,
  58. @RequestBody CustomStrategyDTO customStrategy) {
  59. if (firstName == null || lastName == null || email == null || password == null || userService.findByEmail(email) != null){
  60. return new GenericResponse(Constant.FAIL);
  61. }
  62. try {
  63. byte[] salt = new byte[16];
  64. SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  65. sr.nextBytes(salt);
  66. PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 512);
  67. SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  68. byte[] hash = f.generateSecret(spec).getEncoded();
  69. Base64.Encoder enc = Base64.getEncoder();
  70. User newUser = User.builder().email(email).password(enc.encodeToString(hash)).salt(enc.encodeToString(salt)).firstName(firstName.substring(0, 1).toUpperCase() + firstName.substring(1)).lastName(lastName.substring(0, 1).toUpperCase() + lastName.substring(1)).build();
  71. userService.save(newUser);
  72.  
  73. if (customStrategy == null) {
  74. return new GenericResponse(Constant.FAIL);
  75. }
  76.  
  77. toUpdateQueueService.changeStrategy(customStrategy,newUser);
  78.  
  79. return new GenericResponse(Constant.OK);
  80. }
  81. catch (Exception e) {
  82. e.printStackTrace();
  83. return new GenericResponse(Constant.FAIL);
  84. }
  85. }
  86.  
  87. /*@RequestMapping(value = "/login")
  88. public GenericResponse login( @RequestParam(value = "email") String email, @RequestParam(value = "password") String password) {
  89. try {
  90.  
  91. User u = userService.findByEmail(email);
  92.  
  93. Base64.Decoder dec = Base64.getDecoder();
  94. byte[] salt = dec.decode(u.getSalt());
  95. PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 512);
  96. SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  97. byte[] hash = f.generateSecret(spec).getEncoded();
  98. Base64.Encoder enc = Base64.getEncoder();
  99. if (u == null || !u.getEmail().equalsIgnoreCase(email) || !enc.encodeToString(hash).equals(u.getPassword())) {
  100. throw new BadCredentialsException("Username or Password are incorrect");
  101. }
  102. UserRole r = new UserRole();
  103. r.setName("USER_ROLE");
  104. List<UserRole> lr = new ArrayList<UserRole>();
  105. lr.add(r);
  106. Collection<? extends GrantedAuthority> authorities = lr;
  107. UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(u, password, authorities);
  108. return new GenericResponse("OK!",u);
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. throw new BadCredentialsException("Error during login");
  112. }
  113. }*/
  114.  
  115.  
  116. @RequestMapping(value = "/api/getPortfolio")
  117. public GenericResponse getPortfolio( ) {
  118. try {
  119.  
  120. PortfolioDTO portfolioDTO = userService.getPortfolio( userService.getCurrentUser().getEmail());
  121. return new GenericResponse(Constant.OK,portfolioDTO);
  122. }
  123.  
  124. catch (Exception e) {
  125. e.printStackTrace();
  126. throw new NoSuchElementException("Error during fetch");
  127. }
  128. }
  129.  
  130.  
  131. //TODO REMOVE THIS AFTER DEBUGGING
  132. @RequestMapping(value = "/api/getUpdates")
  133. public GenericResponse getUpdates( ) {
  134. try {
  135. return new GenericResponse(Constant.OK,toUpdateQueueService.getUpdatesDTO(userService.getCurrentUser().getIdUser()));
  136. }
  137. catch (Exception e) {
  138. e.printStackTrace();
  139. throw new NoSuchElementException("Error during fetch");
  140. }
  141. }
  142.  
  143.  
  144. @RequestMapping(value = "/api/getAssetClasses")
  145. public GenericResponse getAssetClasses( ) {
  146. try {
  147. return new GenericResponse(Constant.OK,strategyPerAssetService.getCustomStrategies());
  148. }
  149. catch (Exception e) {
  150. e.printStackTrace();
  151. throw new NoSuchElementException("Error during fetch");
  152. }
  153. }
  154.  
  155.  
  156.  
  157. }
Add Comment
Please, Sign In to add comment