001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *      http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.apache.shiro.ee.faces.tags;
015
016import javax.el.ELException;
017import javax.faces.FacesException;
018import javax.faces.component.UIComponent;
019import javax.faces.view.facelets.FaceletContext;
020import javax.faces.view.facelets.TagConfig;
021import java.io.IOException;
022
023import lombok.extern.slf4j.Slf4j;
024
025/**
026 * Base TagHandler for Tags that check for authentication.
027 */
028@Slf4j
029public abstract class AuthenticationTagHandler extends SecureTagHandler {
030    public AuthenticationTagHandler(TagConfig config) {
031        super(config);
032    }
033
034    @Override
035    public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, ELException {
036        if (showTagBody()) {
037            this.nextHandler.apply(ctx, parent);
038        }
039    }
040
041    protected abstract boolean checkAuthentication();
042
043    protected boolean showTagBody() {
044        if (checkAuthentication()) {
045            if (log.isTraceEnabled()) {
046                log.trace("Authentication successfully verified.  " + "Tag body will be evaluated.");
047            }
048            return true;
049        } else {
050            if (log.isTraceEnabled()) {
051                log.trace("Authentication verification failed.  " + "Tag body will not be evaluated.");
052            }
053            return false;
054        }
055    }
056}