Module Server : ajout servlet web + gestionnaire de messsage

This commit is contained in:
Notmoo 2017-07-26 16:33:31 +02:00
parent 1ab1a1656c
commit 76048e9b41
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.pqt.server.controller;
import com.pqt.core.entities.messages.Message;
//TODO écrire Javadoc
public interface IMessageHandler {
Message handleMessage(Message message);
}

View File

@ -0,0 +1,51 @@
package com.pqt.server.controller;
import com.pqt.core.entities.messages.Message;
import com.pqt.core.entities.messages.MessageType;
import com.pqt.server.module.account.AccountService;
import com.pqt.server.module.client.ClientService;
import com.pqt.server.module.sale.SaleService;
import com.pqt.server.module.state.ServerStateService;
import com.pqt.server.module.statistics.StatisticsService;
import com.pqt.server.module.stock.StockService;
import java.util.HashMap;
import java.util.Map;
public class SimpleMessageHandler implements IMessageHandler {
private AccountService accountService;
private SaleService saleService;
private StatisticsService statisticsService;
private StockService stockService;
private ClientService clientService;
private ServerStateService serverStateService;
private Map<MessageType, IMessageProcess> queryHandlers;
public SimpleMessageHandler() {
serverStateService = new ServerStateService();
accountService = new AccountService();
clientService = new ClientService();
saleService = new SaleService();
stockService = new StockService();
statisticsService = new StatisticsService(stockService, saleService);
queryHandlers = new HashMap<>();
//TODO ajouter callables à la map
}
@Override
public Message handleMessage(Message message) {
if(queryHandlers.containsKey(message.getType()))
return queryHandlers.get(message.getType()).execute(message);
return null;
}
//TODO ajouter Javadoc
private interface IMessageProcess{
Message execute(Message request);
}
}

View File

@ -0,0 +1,37 @@
package com.pqt.server.servlets;
import com.pqt.core.communication.GSonMessageToolFactory;
import com.pqt.core.communication.IMessageToolFactory;
import com.pqt.core.entities.messages.Message;
import com.pqt.server.controller.IMessageHandler;
import com.pqt.server.controller.SimpleMessageHandler;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//TODO ajouter logs
@WebServlet(name = "QueryServlet", urlPatterns = "/")
public class QueryServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeServletProcess(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
executeServletProcess(request, response);
}
private void executeServletProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IMessageToolFactory messageToolFactory = new GSonMessageToolFactory();
IMessageHandler msgHandler = new SimpleMessageHandler();
if (request.getParameter("message") != null) {
Message resp = msgHandler.handleMessage(messageToolFactory.getObjectParser(Message.class).parse(request.getParameter("message")));
response.getWriter().write(messageToolFactory.getObjectFormatter(Message.class).format(resp));
}
}
}