A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
3.9 KiB

package org.ros.chatto.controller;
import java.security.Principal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ros.chatto.model.ChatMessage;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.MessageCipher;
import org.ros.chatto.model.UserRole;
import org.ros.chatto.repository.ChatMessageRepository;
import org.ros.chatto.repository.MessageCipherRepository;
import org.ros.chatto.repository.RoleRepository;
import org.ros.chatto.repository.UserRepository;
import org.ros.chatto.repository.UserRepositoryCustom;
import org.ros.chatto.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
@RequestMapping("/api")
//@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class DemoRestController {
@Autowired
UserRepository userRepository;
@Autowired
UserRepositoryCustom userRepositoryCustom;
@Autowired
RoleRepository roleRepository;
@Autowired
UserRoleRepository userRoleRepository;
@Autowired
MessageCipherRepository messageCipherRepository;
@Autowired
ChatMessageRepository chatMessageRepository;
@GetMapping("/users")
public List<ChatUser> getAllUsers() {
return userRepository.findAll();
}
@GetMapping("/usernames")
public List<String> getUserNames() {
return userRepositoryCustom.getAllUserNames("hmm");
}
@GetMapping("/user_old")
public ChatUser getUser() {
return userRepository.findByUserName("hmm");
}
@GetMapping("/user")
public ChatUser currentUserName(Principal principal) {
ChatUser user = userRepository.findByUserName(principal.getName());
return user;
}
@GetMapping("/roles")
public List<UserRole> getAllRoles()
{
return userRoleRepository.findAll();
}
@GetMapping("/ciphers")
public List<MessageCipher> getAllCiphers()
{
return messageCipherRepository.findAll();
}
@GetMapping("/messages")
public List<ChatMessage> getAllMessages()
{
return chatMessageRepository.findAll();
}
// @RequestMapping(value = "/", method = RequestMethod.POST)
// public ResponseEntity<Car> update(@RequestBody Car car) {
//
// if (car != null) {
// car.setMiles(car.getMiles() + 100);
// }
//
// // TODO: call persistence layer to update
// return new ResponseEntity<Car>(car, HttpStatus.OK);
// }
@PostMapping(value="/post-message", consumes = {"application/json"})
public ResponseEntity<MessageCipher> postMessage(@RequestBody MessageCipher messageCipher)
{
System.out.println("Message cipher = " + messageCipher);
messageCipherRepository.save(messageCipher);
return new ResponseEntity<MessageCipher>(HttpStatus.OK);
}
@GetMapping("/logout")
public ModelAndView logoutPage()
{
ModelAndView modelAndView = new ModelAndView("restLogout");
return modelAndView;
}
@RequestMapping(value="perform_logout", method = RequestMethod.POST)
public String performLogout (HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/users";
}
}