Package com.evasion.common

Source Code of com.evasion.common.Utils

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.common;

import com.evasion.entity.security.User;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.util.TextUtils;

/**
*
* @author sebastien.glon
*/
public final class Utils {

    /**
     * Constructeur de classe utilitaire.
     */
    private Utils() {
    }

    public static boolean isEmpty(final String var) {
        return (var == null || var.equals(""));
    }

    public static String getUserName() {
        Object result = null;
        if ((SecurityContextHolder.getContext() == null)
                || (SecurityContextHolder.getContext().getAuthentication() == null)) {
            return null;
        }

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (auth.getPrincipal() == null) {
            return null;
        }

        try {
            BeanWrapperImpl wrapper = new BeanWrapperImpl(auth);
            result = wrapper.getPropertyValue("principal.username");
        } catch (BeansException e) {
            throw new javax.faces.FacesException(e);
        }
        TextUtils.escapeEntities(String.valueOf(result));
        return (String) result;
    }

    public static boolean isDebugMode() {
//@TODO créer une gestion de debug pour les messages et le debug jsf.
        return false;
    }

    private static GrantedAuthority[] authorities() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            return auth.getAuthorities();
        } else {
            return null;
        }
    }

    public static boolean ifNotGranted(String var) {
        GrantedAuthority[] roles = authorities();
        // si balise ifNotGranted est absente ou vide et pas de roles dans la session alors true
        boolean result = (var == null);
        // si balise ifNotGranted est non vide alors la parcourir.
        if (var != null) {
            String[] list = var.split(",");
            int sizeList = list.length;
            int count = 0;
            for (String req : list) {
                if (roles != null) {
                    for (GrantedAuthority roleUser : roles) {
                        if (!roleUser.getAuthority().equals(req)) {
                            count++;
                            break;
                        }
                    }
                } else {
                    if (!req.equals("ROLE_ANONYMOUS")) {
                        result = true;
                    } else {
                        result = false;
                        break;
                    }
                }
            }
            result = sizeList == count;
        }
        return result;
    }

    public static boolean ifAllGranted(String var) {
        GrantedAuthority[] roles = authorities();
        boolean result = (var == null);
        if (var != null) {
            String[] list = var.split(",");
            int sizeList = list.length;
            int count = 0;
            for (String req : list) {
                if (roles != null) {
                    for (GrantedAuthority roleUser : roles) {
                        if (roleUser.getAuthority().equals(req)) {
                            count++;
                            break;
                        }
                    }
                } else {
                    if (req.equals("ROLE_ANONYMOUS")) {
                        count++;
                    } else {
                        break;
                    }
                }
            }
            result = (sizeList == count);
        }
        return result;
    }

    public static boolean ifAnyGranted(String var) {
        boolean result = var == null;
        GrantedAuthority[] roles = authorities();
        if (var != null) {
            String[] list = var.split(",");
            for (String req : list) {
                if (roles != null) {
                    for (GrantedAuthority roleUser : roles) {
                        if (roleUser.getAuthority().equals(req)) {
                            result = true;
                            break;
                        }
                    }
                } else {
                    result = req.equals("ROLE_ANONYMOUS");
                }
                if (result) {
                    break;
                }
            }
        }
        return result;
    }

    public static void setSessionAttribut(String key, Object object) {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        request.getSession().setAttribute(key, object);
    }

    public static Object getSessionAttribut(String key) {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        return request.getSession().getAttribute(key);
    }

    protected static ClassLoader getCurrentClassLoader(Object defaultObject) {

        ClassLoader loader = Thread.currentThread().getContextClassLoader();

        if (loader == null) {
            loader = defaultObject.getClass().getClassLoader();
        }

        return loader;
    }

    public static String getMessageResourceString(String key, Object params[]) {
        FacesContext context = FacesContext.getCurrentInstance();
        String bundleName = context.getApplication().getMessageBundle();
        Locale locale = context.getViewRoot().getLocale();
        return getMessageResourceString(bundleName, key, params, locale);
    }

    public static String getMessageResourceString(String bundleName, String key,
            Object params[],
            Locale locale) {

        String text = null;

        ResourceBundle bundle =
                ResourceBundle.getBundle(bundleName, locale,
                getCurrentClassLoader(params));

        try {
            text = bundle.getString(key);
        } catch (MissingResourceException e) {
            text = "?? key " + key + " not found ??";
        }

        if (params != null) {
            MessageFormat mf = new MessageFormat(text, locale);
            text = mf.format(params, new StringBuffer(), null).toString();
        }

        return text;
    }
}
TOP

Related Classes of com.evasion.common.Utils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.