[SERVEUR] #13 : Les fichiers de ressources peuvent désormais être stoqués dans les dossiers de la webapp du serveur; Ajout d'un test permettant de s'assurer que la wepapp a bien été unpack

This commit is contained in:
Notmoo-PC\Notmoo 2018-01-23 20:31:11 +01:00
parent d8f096f8d9
commit d5c3fbbdc6
9 changed files with 82 additions and 42 deletions

View File

@ -18,6 +18,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/WEB-INF/classes" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/WEB-INF/classes" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View File

@ -18,6 +18,8 @@ import com.pqt.server.module.sale.SaleService;
import com.pqt.server.module.state.ServerStateService; import com.pqt.server.module.state.ServerStateService;
import com.pqt.server.module.statistics.StatisticsService; import com.pqt.server.module.statistics.StatisticsService;
import com.pqt.server.module.stock.StockService; import com.pqt.server.module.stock.StockService;
import com.pqt.server.tools.io.ISerialFileManager;
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
import java.util.*; import java.util.*;
@ -66,12 +68,13 @@ public class SimpleMessageHandler implements IMessageHandler {
private MessageManager manager; private MessageManager manager;
public SimpleMessageHandler() { public SimpleMessageHandler(String ressourceFolderPathStr) {
serverStateService = new ServerStateService(); serverStateService = new ServerStateService();
accountService = new AccountService(); accountService = new AccountService(ressourceFolderPathStr);
//clientService = new ClientService(); //clientService = new ClientService();
stockService = new StockService(); stockService = new StockService(ressourceFolderPathStr);
saleService = new SaleService(stockService); saleService = new SaleService(stockService, ressourceFolderPathStr);
statisticsService = new StatisticsService(stockService, saleService); statisticsService = new StatisticsService(stockService, saleService);
messageToolFactory = new GSonMessageToolFactory(); messageToolFactory = new GSonMessageToolFactory();

View File

@ -5,7 +5,7 @@ import com.pqt.core.entities.user_account.AccountLevel;
import java.util.List; import java.util.List;
//TODO ajouter logs //TODO Issue #6 : ajouter logs
/** /**
* Cette classe correspond au service de gestion des comptes utilisateurs. Il permet la vérification de l'existance * Cette classe correspond au service de gestion des comptes utilisateurs. Il permet la vérification de l'existance
* d'un compte, de son état (connecté/déconnecté), de changer son état ainsi que de récupérer son niveau d'accréditation. * d'un compte, de son état (connecté/déconnecté), de changer son état ainsi que de récupérer son niveau d'accréditation.
@ -18,8 +18,8 @@ public class AccountService {
private IAccountDao dao; private IAccountDao dao;
public AccountService() { public AccountService(String ressourceFolderPathStr) {
dao = new FileAccountDao(); dao = new FileAccountDao(ressourceFolderPathStr);
} }
/** /**

View File

@ -8,10 +8,11 @@ import com.pqt.server.tools.security.IHashTool;
import com.pqt.server.tools.security.RandomString; import com.pqt.server.tools.security.RandomString;
import com.pqt.server.tools.security.SHA256HashTool; import com.pqt.server.tools.security.SHA256HashTool;
import java.io.File;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
//TODO ajouter logs //TODO Issue #6 : ajouter logs
/** /**
* Implémentation de l'interface {@link IAccountDao} utilisant un fichier contenant des objets sérialisés comme * Implémentation de l'interface {@link IAccountDao} utilisant un fichier contenant des objets sérialisés comme
@ -25,8 +26,8 @@ import java.util.stream.Collectors;
*/ */
public class FileAccountDao implements IAccountDao { public class FileAccountDao implements IAccountDao {
//TODO to modify private static final String ACCOUNT_FILE_NAME = "acc.pqt";
private static final String ACCOUNT_FILE_NAME = "G:\\temp\\acc.pqt"; private final String ACCOUNT_FILE_FOLDER_PATH;
private Set<AccountEntry> accountEntries; private Set<AccountEntry> accountEntries;
private Set<AccountEntry> connectedAccount; private Set<AccountEntry> connectedAccount;
@ -34,15 +35,20 @@ public class FileAccountDao implements IAccountDao {
private RandomString randomString; private RandomString randomString;
private ISerialFileManager<AccountEntry> fileManager; private ISerialFileManager<AccountEntry> fileManager;
public FileAccountDao() { public FileAccountDao(String ressourceFolderPathStr) {
ACCOUNT_FILE_FOLDER_PATH = ressourceFolderPathStr;
accountEntries = new HashSet<>(); accountEntries = new HashSet<>();
connectedAccount = new HashSet<>(); connectedAccount = new HashSet<>();
hashTool = new SHA256HashTool(); hashTool = new SHA256HashTool();
randomString = new RandomString(10); randomString = new RandomString(10);
fileManager = SimpleSerialFileManagerFactory.getFileManager(AccountEntry.class, ACCOUNT_FILE_NAME); fileManager = SimpleSerialFileManagerFactory.getFileManager(AccountEntry.class, getAccountFilePathStr());
loadFromFile(); loadFromFile();
} }
private String getAccountFilePathStr(){
return ACCOUNT_FILE_FOLDER_PATH + File.separator + ACCOUNT_FILE_NAME;
}
/** /**
* Recherche une correspondance entre un objet {@link Account} et les objets {@link AccountEntry} contenu dans * Recherche une correspondance entre un objet {@link Account} et les objets {@link AccountEntry} contenu dans
* la collection {@code entries}. La correspondance se base sur la valeur renvoyée par {@link Account#getUsername()} * la collection {@code entries}. La correspondance se base sur la valeur renvoyée par {@link Account#getUsername()}

View File

@ -30,12 +30,14 @@ import java.util.Map;
public class NoRevertFileSaleDao implements ISaleDao { public class NoRevertFileSaleDao implements ISaleDao {
private static final String SALE_LOG_FILE_NAME = "sale_log.txt"; private static final String SALE_LOG_FILE_NAME = "sale_log.txt";
private final String SALE_LOG_FILE_FOLDER_PATH;
private static final long DEFAULT_SALE_ID = 0; //équivaut à la valeur du premier id - 1 private static final long DEFAULT_SALE_ID = 0; //équivaut à la valeur du premier id - 1
private StockService stockService; private StockService stockService;
private long nextSaleId; private long nextSaleId;
private ISaleRenderer renderer; private ISaleRenderer renderer;
NoRevertFileSaleDao(StockService stockService) { NoRevertFileSaleDao(StockService stockService, String ressourceFolderPathStr) {
SALE_LOG_FILE_FOLDER_PATH = ressourceFolderPathStr;
this.stockService = stockService; this.stockService = stockService;
this.renderer = getRenderer(); this.renderer = getRenderer();
nextSaleId = readLastSaleIdFromFile()+1; nextSaleId = readLastSaleIdFromFile()+1;
@ -96,7 +98,7 @@ public class NoRevertFileSaleDao implements ISaleDao {
*/ */
private long readLastSaleIdFromFile(){ private long readLastSaleIdFromFile(){
long id = DEFAULT_SALE_ID; long id = DEFAULT_SALE_ID;
if(FileUtil.exist(SALE_LOG_FILE_NAME)){ if(FileUtil.exist(getLogFilePath())){
try(ReversedLinesFileReader rlfr = new ReversedLinesFileReader(new File("SALE_LOG_FILE_NAME"))){ try(ReversedLinesFileReader rlfr = new ReversedLinesFileReader(new File("SALE_LOG_FILE_NAME"))){
boolean stop = false; boolean stop = false;
do{ do{
@ -131,7 +133,7 @@ public class NoRevertFileSaleDao implements ISaleDao {
} }
private void logSale(Sale sale, Date date, long saleId){ private void logSale(Sale sale, Date date, long saleId){
try(FileOutputStream fos = new FileOutputStream(SALE_LOG_FILE_NAME); try(FileOutputStream fos = new FileOutputStream(getLogFilePath());
PrintWriter pw = new PrintWriter(fos)){ PrintWriter pw = new PrintWriter(fos)){
pw.append(renderer.render(sale, date, saleId)); pw.append(renderer.render(sale, date, saleId));
@ -166,4 +168,8 @@ public class NoRevertFileSaleDao implements ISaleDao {
private interface ISaleRenderer{ private interface ISaleRenderer{
String render(Sale sale, Date date, long saleId); String render(Sale sale, Date date, long saleId);
} }
private String getLogFilePath(){
return SALE_LOG_FILE_FOLDER_PATH + File.separator + SALE_LOG_FILE_NAME;
}
} }

View File

@ -11,7 +11,7 @@ import com.pqt.server.module.stock.StockService;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
//TODO ajouter logs //TODO Issue #6 : ajouter logs
/** /**
* Cette classe correspond au service de validation des commandes de produits. * Cette classe correspond au service de validation des commandes de produits.
@ -34,8 +34,8 @@ public class SaleService {
private ISaleDao dao; private ISaleDao dao;
private ISaleFirerer eventFirerer; private ISaleFirerer eventFirerer;
public SaleService(StockService stockService) { public SaleService(StockService stockService, String ressourceFolderPathStr) {
dao = new NoRevertFileSaleDao(stockService); dao = new NoRevertFileSaleDao(stockService, ressourceFolderPathStr);
eventFirerer = new SimpleSaleFirerer(); eventFirerer = new SimpleSaleFirerer();
} }

View File

@ -5,6 +5,7 @@ import com.pqt.server.tools.entities.SaleContent;
import com.pqt.server.tools.io.ISerialFileManager; import com.pqt.server.tools.io.ISerialFileManager;
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory; import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
import java.io.File;
import java.lang.IllegalStateException; import java.lang.IllegalStateException;
import java.util.*; import java.util.*;
@ -19,17 +20,22 @@ import java.util.*;
*/ */
public class FileStockDao implements IStockDao { public class FileStockDao implements IStockDao {
//TODO to modify private static final String STOCK_FILE_NAME = "stock.pqt";
private static final String STOCK_FILE_NAME = "G:\\temp\\stock.pqt"; private final String STOCK_FILE_FOLDER_PATH;
private ISerialFileManager<Product> fileManager; private ISerialFileManager<Product> fileManager;
private long nextProductId; private long nextProductId;
private Random random; private Random random;
private Map<Long, Product> products; private Map<Long, Product> products;
public FileStockDao() { private String getStockFilePathStr(){
return STOCK_FILE_FOLDER_PATH + File.separator + STOCK_FILE_NAME;
}
public FileStockDao(String ressourceFolderPathStr) {
STOCK_FILE_FOLDER_PATH = ressourceFolderPathStr;
random = new Random(); random = new Random();
fileManager = SimpleSerialFileManagerFactory.getFileManager(Product.class, STOCK_FILE_NAME); fileManager = SimpleSerialFileManagerFactory.getFileManager(Product.class, getStockFilePathStr());
loadFromFile(); loadFromFile();
generateNextProductId(); generateNextProductId();
} }

View File

@ -7,7 +7,7 @@ import com.pqt.server.tools.entities.SaleContent;
import java.util.List; import java.util.List;
//TODO ajouter logs //TODO Issue #6 : ajouter logs
/** /**
* Cette classe correspond au service de gestion du stock de produits. Il est en charge de la persistance des * Cette classe correspond au service de gestion du stock de produits. Il est en charge de la persistance des
@ -27,8 +27,8 @@ public class StockService {
private IStockDao dao; private IStockDao dao;
public StockService() { public StockService(String ressourceFolderPathStr) {
dao = new FileStockDao(); dao = new FileStockDao(ressourceFolderPathStr);
} }
public List<Product> getProductList() { public List<Product> getProductList() {

View File

@ -5,6 +5,9 @@ import com.pqt.core.communication.IMessageToolFactory;
import com.pqt.core.entities.messages.Message; import com.pqt.core.entities.messages.Message;
import com.pqt.server.controller.IMessageHandler; import com.pqt.server.controller.IMessageHandler;
import com.pqt.server.controller.SimpleMessageHandler; import com.pqt.server.controller.SimpleMessageHandler;
import com.pqt.server.servlets.exceptions.BadPqtServerSetupException;
import com.pqt.server.tools.io.ISerialFileManager;
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
@ -13,18 +16,15 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
//TODO ajouter logs //TODO Issue #6 : ajouter logs
@WebServlet(name = "QueryServlet", urlPatterns = "/") @WebServlet(name = "QueryServlet", urlPatterns = "/")
public class QueryServlet extends HttpServlet { public class QueryServlet extends HttpServlet {
private final IMessageToolFactory messageToolFactory; private IMessageToolFactory messageToolFactory;
private final IMessageHandler msgHandler; private IMessageHandler msgHandler;
public QueryServlet() { public QueryServlet() {
super(); super();
this.messageToolFactory = new GSonMessageToolFactory();
this.msgHandler = new SimpleMessageHandler();
} }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@ -36,20 +36,38 @@ public class QueryServlet extends HttpServlet {
} }
private void executeServletProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private void executeServletProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
if (this.getServletContext().getRealPath("/WEB-INF/classes") == null) {
response.getWriter().write(new BadPqtServerSetupException("Real path of ressource folder is null. Current PQT server only works with web server that unpack webapps' WAR files.").toString());
} else {
if (messageToolFactory == null)
this.messageToolFactory = new GSonMessageToolFactory();
if (request.getQueryString() != null && !request.getQueryString().isEmpty() && request.getParameter("message")!=null) { if (msgHandler == null) {
try { /*
Message resp = msgHandler.handleMessage(messageToolFactory.getObjectParser(Message.class).parse(request.getParameter("message"))); * Le chemin passé en paramètre correspond au chemin réel vers le dossier contenant tous les fichiers de ressources
* Cela ne fonctionne que si le webserver utilisé unpackage le war du serveur.
*/
this.msgHandler = new SimpleMessageHandler(this.getServletContext().getRealPath("/WEB-INF/classes"));
}
if (request.getQueryString() != null && !request.getQueryString().isEmpty() && request.getParameter("message") != null) {
try {
Message resp = msgHandler.handleMessage(messageToolFactory.getObjectParser(Message.class).parse(request.getParameter("message")));
response.getWriter().write(messageToolFactory.getObjectFormatter(Message.class).format(resp)); response.getWriter().write(messageToolFactory.getObjectFormatter(Message.class).format(resp));
}catch(Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
response.getWriter().write(String.format("%s : %s", e.getClass().getName(), e.getMessage())); response.getWriter().write(String.format("%s : %s", e.getClass().getName(), e.getMessage()));
response.getWriter().write("StackTrace :"); response.getWriter().write("StackTrace :");
e.printStackTrace(response.getWriter()); e.printStackTrace(response.getWriter());
}
} else {
response.getWriter().write("Query message was not correctly made : " + request.getQueryString());
}
} }
}else{ }catch (Throwable e){
response.getWriter().write("Query message was not correctly made : "+request.getQueryString()); e.printStackTrace();
response.getWriter().write(e.toString());
} }
} }
} }