1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| public class MonPhaseListener extends DelegatingPhaseListenerMulticaster implements ServletContextListener,Serializable{
private static final long serialVersionUID = 1L;
public MonPhaseListener() {
}
@Override
public void beforePhase(PhaseEvent pe) {
if (pe.getPhaseId() == PhaseId.RESTORE_VIEW) {
System.out.println("Nouvelle requête en cours ...");
}
System.out.println("avant - " + pe.getPhaseId().toString());
}
@Override
public void afterPhase(PhaseEvent pe) {
// System.out.println("après - " + pe.getPhaseId().toString());
if (pe.getPhaseId() == PhaseId.RESTORE_VIEW) {
System.out.println("Fin d’analyse de la requête.\n");
FacesContext fc = pe.getFacesContext();
this.redirectPage(fc);
}
}
/**
* Rediriger les pages en verifiant les contraintes de licence et config
* @param fc
*/
public void redirectPage(FacesContext fc){
String currentPage = fc.getViewRoot().getViewId();
System.out.println(currentPage);
//teste que c la page de login
boolean loginPage = (currentPage.contains("connection.xhtml") || currentPage.contains("licence.xhtml"));
//teste si c'est une page xhtml
boolean xhtmlPage = (currentPage.contains(".xhtml"));
// ConnectionController connectController = null;
//si c'est une page differente de login
if (!loginPage && xhtmlPage) {
boolean isLogged = false;
System.out.println("test con");
WebApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext((ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext());
ConnectionController connectController= ctx.getBean(ConnectionController.class);
NavigationHandler nh = fc.getApplication().getNavigationHandler();
if (connectController != null) {
System.out.println("Connection OK");
/**** LES VARIABLES DE connectController SONT REINITIALISEES******/
isLogged = connectController.isIsLoggedIn();
int status = connectController.verifyContrainstBeforeToOpenPage();
switch (status) {
case GlobalFonctions.GO_TO_CONNECT:
nh.handleNavigation(fc, null, "client.deconnect");
break;
case GlobalFonctions.GO_TO_LICENCE:
nh.handleNavigation(fc, null, "license");
break;
// case GlobalFonctions.GO_TO_CONFIG:
// nh.handleNavigation(fc, null, "configuration");
// break;
case GlobalFonctions.GO_TO_PAGE:
this.checkSecurePage(currentPage, fc);
connectController.afficheLibellePage();
break;
}
} else {
System.out.println("Connection NULL");
nh.handleNavigation(fc, null, "client.deconnect");
}
}
}
/**
* Verifier les contraintes de securite des users s'ils ont acces la page demande
* @param currentPage
* @param fc
*/
public void checkSecurePage(String currentPage, FacesContext fc) {
NavigationHandler nh = fc.getApplication().getNavigationHandler();
//c'est bien mais verifions que c le menu qui lui appartient
String[] myDecomposePage = new String[2];
myDecomposePage = currentPage.split("\\.");
String chemin = myDecomposePage[0];
Map<String, String> colMenu = (HashMap<String, String>) fc.getExternalContext().getSessionMap().get(GlobalFonctions.MENUS_USER);
if (colMenu != null && !colMenu.isEmpty()) {
String page = colMenu.get(chemin);
System.out.println("ma page=" + page);
fc.getExternalContext().getRequestMap().put("page", page);
//enlevons principal et pagerror
if (!chemin.equalsIgnoreCase("/licence") && !chemin.equalsIgnoreCase("/principal") && !chemin.equalsIgnoreCase("/errorpage") &&
!chemin.equalsIgnoreCase("/profil")) {
if (colMenu.containsKey(chemin) == false) {
nh.handleNavigation(fc, null, "pageerror");
}
}
} else {
System.out.println("Pas de Menus Operationelles");
nh.handleNavigation(fc, null, "client.deconnect");
}
}
@Override
public PhaseId getPhaseId() {
//je prend juste les vues finales
return PhaseId.RESTORE_VIEW;
}
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
} |
Partager