com.jga.view.seguridad.RolManagedBean.java Source code

Java tutorial

Introduction

Here is the source code for com.jga.view.seguridad.RolManagedBean.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jga.view.seguridad;

import com.jga.business.interfaces.UsuarioBeanRemote;
import com.jga.sistema.entidades.Rol;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author Josue
 */
@ManagedBean
@ApplicationScoped
public class RolManagedBean {

    private List<Rol> roles;

    @EJB
    private UsuarioBeanRemote usuarioBean;

    /**
     * Creates a new instance of RolManagedBean
     */
    public RolManagedBean() {
        this.roles = new ArrayList<>();
    }

    @PostConstruct
    public void init() {
        refrescar();
    }

    public List<Rol> getRoles() {
        return roles;
    }

    public void refrescar() {
        try {
            usuarioBean.LoadRoles(getSecurityRoles());
        } catch (EJBException ex) {
            Exception ne = (Exception) ex.getCause();
            Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ne);
        } catch (Exception ex) {
            Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.roles = usuarioBean.GetRoles();

    }

    protected List<Rol> getSecurityRoles() {

        HttpServletRequest origRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        ServletContext sc = origRequest.getServletContext();
        InputStream is = sc.getResourceAsStream("/WEB-INF/web.xml");

        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(is);

            Element webApp = doc.getRootElement();

            // Type safety warning:  dom4j doesn't use generics
            List<Element> roleElements = webApp.elements("security-role");
            for (Element roleEl : roleElements) {
                roles.add(new Rol(roleEl.element("role-name").getText()));
            }
        } catch (EJBException ex) {
            Exception ne = (Exception) ex.getCause();
            Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ne);
        } catch (Exception ex) {
            Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return roles;
    }
}