diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/IMessageHandler.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/IMessageHandler.java new file mode 100644 index 00000000..ce4cec02 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/IMessageHandler.java @@ -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); +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java new file mode 100644 index 00000000..67de608e --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/controller/SimpleMessageHandler.java @@ -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 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); + } +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/servlets/QueryServlet.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/servlets/QueryServlet.java new file mode 100644 index 00000000..e8626bf6 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/servlets/QueryServlet.java @@ -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)); + } + } +}