package com.rila.chat;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.rila.chat.client.Client;
import com.rila.chat.client.ClientThreadServerResponseUsers;
import com.rila.chat.form.ConnexionForm;
import com.rila.chat.service.IClientService;
import fr.cesi.chat.domain.Event;
import fr.cesi.chat.domain.Message;
import fr.cesi.chat.domain.Statut;
import fr.cesi.chat.domain.User;
import fr.cesi.chat.utils.Utils;
@Controller
@RequestMapping(value = "/client")
public class ClientController {
private static final String REDIRECT_ROOT = "redirect:/"; // Redirige vers la racine du site
private Client client;
private ClientThreadServerResponseUsers clientThread;
private String serverAddress;
private User user = new User();
private ObjectOutputStream output;
@Autowired
private IClientService clientService;
@RequestMapping(value = "/connexion", method = RequestMethod.POST, produces = "application/json")
public ModelAndView connexion(@ModelAttribute("connexionForm") ConnexionForm connexionForm) throws IOException, InterruptedException {
Boolean isNull = true;
serverAddress = connexionForm.getServerAddress();
user.setNickname(connexionForm.getNickname());
user.setStatut(connexionForm.getStatut());
ModelAndView model = new ModelAndView("client");
model.addObject("statuts", Statut.values());
model.addObject("user", user);
model.addObject("serverAddress", serverAddress);
client = new Client(serverAddress, user);
output = client.getClientThreadServerSend().getOut();
clientThread = client.getClientThreadServerResponseUsers();
// Si le nom d'utilisateur existe déjà on redirige la page vers la page
// de connexion avec un message
if (clientThread.isAuthorized() == Event.SERVER_REFUSED) {
model = new ModelAndView(REDIRECT_ROOT);
model.addObject("statuts", Statut.values());
model.getModelMap().addAttribute("error",new Error("hahahah"));
model.getModelMap().addAttribute("nameExist","* Le nom d'utilisateur existe déjà, veuillez en saisir un autre.");
}
while (isNull) {
if (output != null) {
Message message2 = null;
if (clientThread.isAuthorized() == Event.SERVER_ACCEPTED) {
message2 = new Message(Utils.getDateNow(), user, null, "", Event.CONNECTION_USER);
} else if (clientThread.isAuthorized() == Event.SERVER_ACCEPTED) {
message2 = new Message(Utils.getDateNow(), user, "", null, Event.DELETING_USERNAME);
}
isNull = false;
clientService.sendMessage(output, message2);
} else
output = client.getClientThreadServerSend().getOut();
Thread.sleep(500);
}
return model;
}
/**
* Permet d'envoyer des message via JQuery
*
* @param message
* @return
*/
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
Message post(@RequestBody final Message message) {
Message message2 = new Message(Utils.getDateNow(), user, null, message.getMessage(), Event.ALL_CLIENTS);
clientService.sendMessage(output, message2);
return message;
}
/**
* Permet de récuperer les messages pour les afficher
*
* @param res
* @return
*/
@RequestMapping(value = "/getMessages", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
Message get(HttpServletResponse res) {
List<Message> messages = client.getClientThreadServerResponseMessage().getMessages();
Message message = null;
if (!messages.isEmpty()) {
message = messages.remove(0);
}
return message;
}
/**
* Permet de mettre à jour l'utilisateur connecté
*
* @param userUpdated
* @return
*/
@RequestMapping(value = "/updateUser", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
User updateStatusUser(@RequestBody final User userUpdated) {
String newNickname = userUpdated.getNickname();
String oldNickname = user.getNickname();
String concatNickname = oldNickname + "," + newNickname;
userUpdated.setNickname(concatNickname);
Message message2 = new Message(Utils.getDateNow(), userUpdated, "", null, Event.CHANGING_USERNAME);
clientService.sendMessage(output, message2);
// On set le nouveau nickname dans la variable global user
String ip = user.getIp();
String nickanme = user.getNickname();
Statut statut = user.getStatut();
user = new User();
user.setIp(ip);
user.setNickname(newNickname);
user.setStatut(statut);
return user;
}
/**
* Permet de mettre à jour l'utilisateur connecté
*
* @param userUpdated
* @return
*/
@RequestMapping(value = "/updateStatusUser", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
User postUpdateStatusUser(@RequestBody final User userUpdated, ModelAndView model) {
Message message = new Message(Utils.getDateNow(), userUpdated, "", null, Event.CHANGING_STATUS_USERNAME);
clientService.sendMessage(output, message);
// On set le nouveau nickname dans la variable global user
user.setStatut(userUpdated.getStatut());
return new User(user.getNickname(), user.getStatut(), user.getIp());
}
/**
* Permet de récuperer les utilisateurs connectés
*
* @param res
* @return
*/
@RequestMapping(value = "/getUsers", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
List<User> getUsers(HttpServletResponse res) {
List<User> users = new ArrayList<User>();
if (clientThread != null) {
users = getUsersFromServer();
}
return users;
}
/**
* Retourne à la page d'acceuil de connexion
*
* @param model
* @return
*/
@RequestMapping(value = "/deconnexion", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ModelAndView deconnexion() {
Message message2 = new Message(Utils.getDateNow(), user, "", null, Event.DELETING_USERNAME);
clientService.sendMessage(output, message2);
// client.closeConnection(); // On ferme les connexions
ModelAndView model = new ModelAndView(REDIRECT_ROOT);
model.getModelMap().addAttribute("statuts", Statut.values());
return model;
}
// ************** Model attribut ******************
@ModelAttribute("connexionForm")
public ConnexionForm getConnexionForm() {
return new ConnexionForm();
}
public Event isAuthorized() {
return clientThread.isAuthorized();
}
public List<User> getUsersFromServer() {
return clientThread.getUsers();
}
}