daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mypack.conf;
  2.  
  3. import java.time.LocalDateTime;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8.  
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.ComponentScan;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.context.annotation.Lazy;
  15. import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
  16. import org.springframework.http.HttpCookie;
  17. import org.springframework.http.ResponseCookie;
  18. import org.springframework.web.reactive.config.ResourceHandlerRegistry;
  19. import org.springframework.web.reactive.config.WebFluxConfigurer;
  20. import org.springframework.web.server.ServerWebExchange;
  21. import org.springframework.web.server.WebFilter;
  22. import org.springframework.web.server.WebFilterChain;
  23.  
  24. import mypack.dao.PageViewRepository;
  25. import mypack.dao.UserRepository;
  26. import mypack.domain.PageView;
  27. import mypack.domain.User;
  28. import mypack.security.JwtProvider;
  29.  
  30. import reactor.core.publisher.Mono;
  31. @Configuration
  32.  
  33.  
  34. @ComponentScan(basePackages = "mypack")
  35. @EnableReactiveMongoRepositories(basePackages = "mypack")
  36. public class WebConfig implements WebFluxConfigurer {
  37.  
  38.     @Autowired
  39.     @Lazy
  40.     private UserRepository userRepository;
  41.  
  42.     @Autowired
  43.     @Lazy
  44.     private PageViewRepository pageViewRepository;
  45.  
  46.  
  47.     @Autowired
  48.     @Lazy
  49.     JwtProvider jwtProvider;
  50.  
  51.  
  52.     @Bean
  53.     public WebFilter sampleWebFilter()  {
  54.         return new WebFilter() {
  55.  
  56.             @Override
  57.             public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  58.  
  59.                 String uri = exchange.getRequest().getURI().toString();
  60.                 String path = exchange.getRequest().getPath().pathWithinApplication().value();
  61.  
  62.  
  63.                 HttpCookie  cookie = null;
  64.                 String token = "";
  65.                 Map<String, List<HttpCookie>> cookies = exchange.getRequest().getCookies();
  66.  
  67.  
  68.                 try {
  69.                     if((exchange.getRequest().getCookies().containsKey("_token") )
  70.                             &&  (exchange.getRequest().getCookies().getFirst("_token"))!=null  ) {
  71.  
  72.                         cookie = exchange.getRequest().getCookies().getFirst("_token");
  73.                         token = cookie.getValue();
  74.  
  75.  
  76.                         return userRepository.findByToken(token).map(user -> {
  77.  
  78.                                 exchange.getAttributes().put("_token", user.getToken());
  79.  
  80.  
  81.                                 PageView pg = PageView.builder().createdDate(LocalDateTime.now()).URL(uri).build();
  82.                                 pageViewRepository.save(pg).subscribe(pg1 -> {user.getPageviews().add(pg1); });
  83.  
  84.                                 userRepository.save(user).subscribe();
  85.                                     return user;
  86.                             })
  87.  
  88.  
  89.                             .flatMap(user-> chain.filter(exchange)); // ultimately this step executes regardless user exist or not
  90.  
  91.                     // handle case when brand new user first time visits website    
  92.                     } else {
  93.                         token = jwtProvider.genToken("guest", UUID.randomUUID().toString());
  94.                         User user = User.builder().createdDate(LocalDateTime.now()).token(token).emailId("guest").build();
  95.                         userRepository.save(user).subscribe();
  96.                         exchange.getResponse().getCookies().remove("_token");
  97.  
  98.                         ResponseCookie rcookie  = ResponseCookie.from("_token", token).httpOnly(true).build();
  99.                         exchange.getResponse().addCookie(rcookie);
  100.                         exchange.getAttributes().put("_token", token);
  101.  
  102.                     }
  103.  
  104.                 } catch (Exception e) {
  105.  
  106.                     e.printStackTrace();
  107.                 }
  108.  
  109.  
  110.  
  111.                 return chain.filter(exchange);
  112.             } // end of  Mono<Void> filter method
  113.         }; // end of New WebFilter (anonymous class)
  114.     }
  115.  
  116. }
  117.    
  118. @Repository
  119. public interface PageViewRepository extends   ReactiveMongoRepository<PageView, String>{
  120.  
  121.     Mono<PageView> findById(String id);
  122.  
  123. }
  124.  
  125.  
  126. @Repository
  127. public interface UserRepository extends   ReactiveMongoRepository<User, String>{
  128.  
  129.     Mono<User> findByToken(String token);
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. @Data
  138. @AllArgsConstructor
  139. @Builder
  140. @NoArgsConstructor
  141. public class User {
  142.  
  143.     @Id
  144.     private String id;
  145.     private String token;
  146.  
  147.  
  148.     @Default
  149.     private LocalDateTime createdDate = LocalDateTime.now();
  150.  
  151.     @DBRef
  152.     private List<PageView> pageviews;
  153.  
  154. }
  155.  
  156.  
  157.  
  158. Data
  159. @Document
  160. @Builder
  161. public class PageView {
  162.     @Id
  163.     private String id;
  164.  
  165.     private String URL;
  166.  
  167.     @Default
  168.     private LocalDateTime createdDate = LocalDateTime.now();
  169. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top