Package com.evasion.common.security.component

Source Code of com.evasion.common.security.component.AuthenticationTag

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

import java.io.IOException;
import javax.faces.component.FacesComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.util.TextUtils;

/**
*
* @author sebastien.glon
*/
@FacesComponent(value = "com.evasion.common.security.component.AuthenticationTag")
public class AuthenticationTag extends Component {

    public AuthenticationTag() {
        super();
    }

    @Override
    public String getFamily() {
        return "com.evasion";
    }

    @Override
    public String getRendererType() {
        return null;
    }

    protected enum PropertyKeys {

        property;

        String toString;

        PropertyKeys(String toString) {
            this.toString = toString;
        }

        PropertyKeys() {
        }

        @Override
        public String toString() {
            return ((toString != null) ? toString : super.toString());
        }
    }

    public String getProperty() {
        return (java.lang.String) getStateHelper().eval(PropertyKeys.property);
    }

    public void setProperty(String property) {
        getStateHelper().put(PropertyKeys.property, property);
        handleAttribute("property", property);
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Object result = null;
        ResponseWriter writer = context.getResponseWriter();
        if (getProperty() != null) {
            if ((SecurityContextHolder.getContext() == null)
                    || (SecurityContextHolder.getContext().getAuthentication() == null)) {
                return;
            }

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

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

            try {
                BeanWrapperImpl wrapper = new BeanWrapperImpl(auth);
                result = wrapper.getPropertyValue(getProperty());
            } catch (BeansException e) {
                throw new javax.faces.FacesException(e);
            }
            writer.write(TextUtils.escapeEntities(String.valueOf(result)));
        }
    }
}
TOP

Related Classes of com.evasion.common.security.component.AuthenticationTag

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.