Commit 6d6b7a68 authored by Mamadou Diallo's avatar Mamadou Diallo
Browse files

WebSockets config added + few upgrades

parent 1005511a
......@@ -36,6 +36,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
......
package com.example.server.config;
package com.example.server.config.security;
import com.example.server.model.user.User;
import com.example.server.model.user.UserEntity;
......
package com.example.server.config.websocket;
import java.time.LocalDateTime;
public class Message {
private String from;
private String message;
private LocalDateTime timestamp;
public Message() {
// required for Jackson
}
public Message(String from, String message) {
this.from = from;
this.message = message;
}
public Message(String from, String message, LocalDateTime timestamp) {
this.from = from;
this.message = message;
this.timestamp = timestamp;
}
public String getFrom() {
return from;
}
public String getMessage() {
return message;
}
}
package com.example.server.config.websocket;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.*;
@Configuration
@EnableWebSocketMessageBroker
public class ServerWebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/websocket");
registry.addEndpoint("/sockjs")
.withSockJS();
}
}
package com.example.server.config.websocket;
import lombok.RequiredArgsConstructor;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.util.HtmlUtils;
import java.io.IOException;
import java.time.LocalTime;
@RequiredArgsConstructor
@Component
public class WebSocketHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String request = message.getPayload();
System.out.println("Server received: "+ request);
String response = String.format("response from server to '%s'", HtmlUtils.htmlEscape(request));
System.out.println("Server sends: "+response);
session.sendMessage(new TextMessage(response));
}
}
......@@ -3,9 +3,9 @@ package com.example.server.controller;
import com.example.server.model.comment.CommentEntity;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.UserEntity;
import com.example.server.services.comment.CommentServiceImplement;
import com.example.server.services.post.PostServiceImplement;
import com.example.server.services.user.UserServiceImplement;
import com.example.server.services.comment.CommentService;
import com.example.server.services.post.PostService;
import com.example.server.services.user.UserService;
import com.example.server.utils.DateGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -23,9 +23,9 @@ import java.util.List;
@RequestMapping("/api/comment")
public class CommentController {
private final CommentServiceImplement commentServiceImplement;
private final UserServiceImplement userServiceImplement;
private final PostServiceImplement postServiceImplement;
private final CommentService commentService;
private final UserService userService;
private final PostService postService;
private final ObjectMapper mapper = new ObjectMapper();
......@@ -33,8 +33,8 @@ public class CommentController {
public ResponseEntity<CommentEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String postId,
@PathVariable String userid) throws JsonProcessingException {
UserEntity author = userServiceImplement.getById(userid);
PostEntity postEntity = postServiceImplement.getById(postId);
UserEntity author = userService.getById(userid);
PostEntity postEntity = postService.getById(postId);
CommentEntity commentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
......@@ -44,9 +44,9 @@ public class CommentController {
postEntity.addComment(commentEntity);
commentServiceImplement.add(commentEntity);
commentService.add(commentEntity);
postServiceImplement.add(postEntity);
postService.add(postEntity);
return new ResponseEntity<>(commentEntity, HttpStatus.CREATED);
}
......@@ -54,46 +54,46 @@ public class CommentController {
@PutMapping("/{postId}/{commentId}")
public ResponseEntity<CommentEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String postId, @PathVariable String commentId) throws JsonProcessingException {
CommentEntity commentEntity = commentServiceImplement.getById(commentId);
PostEntity postEntity = postServiceImplement.getById(postId);
CommentEntity commentEntity = commentService.getById(commentId);
PostEntity postEntity = postService.getById(postId);
CommentEntity newCommentEntity = mapper.treeToValue(jsonNode, CommentEntity.class);
commentEntity.setMessage(newCommentEntity.getMessage());
postEntity.addComment(commentEntity);
postServiceImplement.update(postId, postEntity);
postService.update(postId, postEntity);
return new ResponseEntity<>(commentEntity, HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
public ResponseEntity<CommentEntity> getById(@PathVariable String id) {
return new ResponseEntity<>(commentServiceImplement.getById(id), HttpStatus.OK);
return new ResponseEntity<>(commentService.getById(id), HttpStatus.OK);
}
@DeleteMapping("/{postId}/{commentId}")
public ResponseEntity<String> deleteById(@PathVariable String postId, @PathVariable String commentId) {
PostEntity postEntity = postServiceImplement.getById(postId);
CommentEntity commentEntity = commentServiceImplement.getById(commentId);
PostEntity postEntity = postService.getById(postId);
CommentEntity commentEntity = commentService.getById(commentId);
postEntity.deleteComment(commentEntity);
commentServiceImplement.deleteById(commentId);
postServiceImplement.update(postId, postEntity);
commentService.deleteById(commentId);
postService.update(postId, postEntity);
return ResponseEntity.ok("Comment deleted successfully !");
}
@GetMapping("/all")
public ResponseEntity<List<CommentEntity>> getAll() {
return new ResponseEntity<>(commentServiceImplement.findAll(), HttpStatus.OK);
return new ResponseEntity<>(commentService.findAll(), HttpStatus.OK);
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
commentServiceImplement.deleteAll();
commentService.deleteAll();
return new ResponseEntity<>("Comment repository is empty", HttpStatus.NOT_FOUND);
}
}
package com.example.server.controller;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class Controller {
@GetMapping("/home")
/*
@MessageMapping("/api")
@SendTo("/home")
public String homePage(){
return "Welcome";
}
......@@ -17,4 +17,5 @@ public class Controller {
public String adminHome(){
return "Admin page";
}
*/
}
package com.example.server.controller;
import com.example.server.config.websocket.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import java.time.LocalDateTime;
@Controller
public class MessageController {
@MessageMapping("/send")
@SendTo("/api/home")
public Message send(Message message) {
LocalDateTime timestamp = LocalDateTime.now();
return new Message(message.getFrom(), message.getMessage(), timestamp);
}
}
......@@ -2,7 +2,9 @@ package com.example.server.controller;
import com.example.server.model.post.PostEntity;
import com.example.server.model.user.UserEntity;
import com.example.server.services.post.PostService;
import com.example.server.services.post.PostServiceImplement;
import com.example.server.services.user.UserService;
import com.example.server.services.user.UserServiceImplement;
import com.example.server.utils.DateGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
......@@ -21,21 +23,21 @@ import java.util.List;
@RequestMapping("/api/post")
public class PostController {
private final PostServiceImplement postServiceImplement;
private final UserServiceImplement userServiceImplement;
private final PostService postService;
private final UserService userService;
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping("/add/{userid}")
public ResponseEntity<PostEntity> add(@RequestBody JsonNode jsonNode, @PathVariable String userid) {
UserEntity author = userServiceImplement.getById(userid);
UserEntity author = userService.getById(userid);
PostEntity postEntity = mapper.convertValue(jsonNode, PostEntity.class);
postEntity.setAuthor(author);
postEntity.setDate(DateGenerator.generateDate());
postEntity.setCommentEntities(new ArrayList<>());
postServiceImplement.add(postEntity);
postService.add(postEntity);
return new ResponseEntity<>(postEntity, HttpStatus.CREATED);
}
......@@ -43,37 +45,37 @@ public class PostController {
@PutMapping("/{id}")
public ResponseEntity<PostEntity> update(@RequestBody JsonNode jsonNode, @PathVariable String id) throws JsonProcessingException {
PostEntity postEntity = postServiceImplement.getById(id);
PostEntity postEntity = postService.getById(id);
PostEntity newPostEntity = mapper.treeToValue(jsonNode, PostEntity.class);
UserEntity userEntity = userServiceImplement.getById(postEntity.getAuthor().getId());
UserEntity userEntity = userService.getById(postEntity.getAuthor().getId());
newPostEntity.setAuthor(userEntity);
newPostEntity.setDate(postEntity.getDate());
postServiceImplement.update(id, newPostEntity);
postService.update(id, newPostEntity);
return new ResponseEntity<>(newPostEntity, HttpStatus.ACCEPTED);
}
@GetMapping("/{id}")
public ResponseEntity<PostEntity> getById(@PathVariable String id) {
return new ResponseEntity<>(postServiceImplement.getById(id), HttpStatus.OK);
return new ResponseEntity<>(postService.getById(id), HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteById(@PathVariable String id) {
postServiceImplement.deleteById(id);
postService.deleteById(id);
return new ResponseEntity<>("Post deleted successfully", HttpStatus.ACCEPTED);
}
@DeleteMapping("/all")
public ResponseEntity<?> deleteAll(){
postServiceImplement.deleteAll();
postService.deleteAll();
return new ResponseEntity<>("Post repository is empty", HttpStatus.NOT_FOUND);
}
@GetMapping("/all")
public ResponseEntity<List<PostEntity>> getAll() {
return new ResponseEntity<>(postServiceImplement.findAll(), HttpStatus.OK);
return new ResponseEntity<>(postService.findAll(), HttpStatus.OK);
}
}
package com.example.server.controller;
import com.example.server.model.user.UserEntity;
import com.example.server.services.user.UserServiceImplement;
import com.example.server.services.user.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -18,7 +17,7 @@ import java.util.List;
@RequestMapping("api/user")
public class UserController {
private final UserServiceImplement userService;
private final UserService userService;
private final ObjectMapper mapper = new ObjectMapper();
......@@ -46,7 +45,6 @@ public class UserController {
}
@GetMapping("/all")
@PreAuthorize("hasAnyRole('ADMIN')")
public ResponseEntity<List<UserEntity>> getAll() {
return new ResponseEntity<>(userService.findAll(), HttpStatus.OK);
}
......
......@@ -2,7 +2,9 @@ package com.example.server.repository;
import com.example.server.model.comment.CommentEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommentRepository extends MongoRepository<CommentEntity, String> {
CommentEntity getCommentById(String id);
......
......@@ -2,7 +2,9 @@ package com.example.server.repository;
import com.example.server.model.post.PostEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PostRepository extends MongoRepository<PostEntity, String> {
PostEntity getPostById(String id);
......
......@@ -3,7 +3,10 @@ package com.example.server.repository;
import com.example.server.model.user.UserEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;
@Repository
public interface UserRepository extends MongoRepository<UserEntity, String> {
UserEntity getUserByUserName(String userName);
......
......@@ -3,13 +3,13 @@ package com.example.server.services.post;
import com.example.server.exceptions.PostNotFoundException;
import com.example.server.model.post.PostEntity;
import com.example.server.repository.PostRepository;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@AllArgsConstructor
@RequiredArgsConstructor
public class PostServiceImplement implements PostService{
private final PostRepository repository;
......
package com.example.server.services.user;
import com.example.server.exceptions.PostNotFoundException;
import com.example.server.exceptions.UserNotFoundException;
import com.example.server.model.user.UserEntity;
import com.example.server.repository.UserRepository;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment