Coverage Report - org.acegisecurity.taglibs.authz.AuthenticationTag
 
Classes in this File Line Coverage Branch Coverage Complexity
AuthenticationTag
75% 
100% 
4.125
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.taglibs.authz;
 17  
 
 18  
 import org.acegisecurity.Authentication;
 19  
 
 20  
 import org.acegisecurity.context.SecurityContext;
 21  
 import org.acegisecurity.context.SecurityContextHolder;
 22  
 
 23  
 import org.acegisecurity.userdetails.UserDetails;
 24  
 
 25  
 import java.io.IOException;
 26  
 
 27  
 import java.lang.reflect.InvocationTargetException;
 28  
 import java.lang.reflect.Method;
 29  
 
 30  
 import java.util.HashSet;
 31  
 import java.util.Set;
 32  
 
 33  
 import javax.servlet.jsp.JspException;
 34  
 import javax.servlet.jsp.tagext.Tag;
 35  
 import javax.servlet.jsp.tagext.TagSupport;
 36  
 
 37  
 
 38  
 /**
 39  
  * An {@link javax.servlet.jsp.tagext.Tag} implementation that allows convenient access to the current
 40  
  * <code>Authentication</code> object.<p>Whilst JSPs can access the <code>SecurityContext</code> directly, this tag
 41  
  * avoids handling <code>null</code> conditions. The tag also properly accommodates
 42  
  * <code>Authentication.getPrincipal()</code>, which can either be a <code>String</code> or a
 43  
  * <code>UserDetails</code>.</p>
 44  
  *
 45  
  * @author Ben Alex
 46  
  * @version $Id: AuthenticationTag.java 1784 2007-02-24 21:00:24Z luke_t $
 47  
  */
 48  16
 public class AuthenticationTag extends TagSupport {
 49  
     //~ Static fields/initializers =====================================================================================
 50  
 
 51  1
     private static final Set methodPrefixValidOptions = new HashSet();
 52  
 
 53  
     static {
 54  1
         methodPrefixValidOptions.add("get");
 55  1
         methodPrefixValidOptions.add("is");
 56  1
     }
 57  
 
 58  
     //~ Instance fields ================================================================================================
 59  
 
 60  16
     private String methodPrefix = "get";
 61  16
     private String operation = "";
 62  
 
 63  
     //~ Methods ========================================================================================================
 64  
 
 65  
     public int doStartTag() throws JspException {
 66  8
         if ((null == operation) || "".equals(operation)) {
 67  1
             return Tag.SKIP_BODY;
 68  
         }
 69  
 
 70  7
         validateArguments();
 71  
 
 72  6
         if ((SecurityContextHolder.getContext() == null)
 73  
             || !(SecurityContextHolder.getContext() instanceof SecurityContext)
 74  
             || (((SecurityContext) SecurityContextHolder.getContext()).getAuthentication() == null)) {
 75  1
             return Tag.SKIP_BODY;
 76  
         }
 77  
 
 78  5
         Authentication auth = SecurityContextHolder.getContext().getAuthentication();
 79  
 
 80  5
         if (auth.getPrincipal() == null) {
 81  1
             return Tag.SKIP_BODY;
 82  4
         } else if (auth.getPrincipal() instanceof UserDetails) {
 83  3
             writeMessage(invokeOperation(auth.getPrincipal()));
 84  
 
 85  2
             return Tag.SKIP_BODY;
 86  
         } else {
 87  1
             writeMessage(auth.getPrincipal().toString());
 88  
 
 89  1
             return Tag.SKIP_BODY;
 90  
         }
 91  
     }
 92  
 
 93  
     public String getMethodPrefix() {
 94  24
         return methodPrefix;
 95  
     }
 96  
 
 97  
     public String getOperation() {
 98  4
         return operation;
 99  
     }
 100  
 
 101  
     protected String invokeOperation(Object obj) throws JspException {
 102  3
         Class clazz = obj.getClass();
 103  3
         String methodToInvoke = getOperation();
 104  3
         StringBuffer methodName = new StringBuffer();
 105  3
         methodName.append(getMethodPrefix());
 106  3
         methodName.append(methodToInvoke.substring(0, 1).toUpperCase());
 107  3
         methodName.append(methodToInvoke.substring(1));
 108  
 
 109  3
         Method method = null;
 110  
 
 111  
         try {
 112  3
             method = clazz.getMethod(methodName.toString(), (Class[]) null);
 113  0
         } catch (SecurityException se) {
 114  0
             throw new JspException(se);
 115  1
         } catch (NoSuchMethodException nsme) {
 116  1
             throw new JspException(nsme);
 117  2
         }
 118  
 
 119  2
         Object retVal = null;
 120  
 
 121  
         try {
 122  2
             retVal = method.invoke(obj, (Object[]) null);
 123  0
         } catch (IllegalArgumentException iae) {
 124  0
             throw new JspException(iae);
 125  0
         } catch (IllegalAccessException iae) {
 126  0
             throw new JspException(iae);
 127  0
         } catch (InvocationTargetException ite) {
 128  0
             throw new JspException(ite);
 129  2
         }
 130  
 
 131  2
         if (retVal == null) {
 132  0
             retVal = "";
 133  
         }
 134  
 
 135  2
         return retVal.toString();
 136  
     }
 137  
 
 138  
     public void setMethodPrefix(String methodPrefix) {
 139  2
         this.methodPrefix = methodPrefix;
 140  2
     }
 141  
 
 142  
     public void setOperation(String operation) {
 143  8
         this.operation = operation;
 144  8
     }
 145  
 
 146  
     protected void validateArguments() throws JspException {
 147  7
         if ((getMethodPrefix() != null) && !getMethodPrefix().equals("")) {
 148  7
             if (!methodPrefixValidOptions.contains(getMethodPrefix())) {
 149  1
                 throw new JspException("Authorization tag : no valid method prefix available");
 150  
             }
 151  
         } else {
 152  0
             throw new JspException("Authorization tag : no method prefix available");
 153  
         }
 154  6
     }
 155  
 
 156  
     protected void writeMessage(String msg) throws JspException {
 157  
         try {
 158  0
             pageContext.getOut().write(String.valueOf(msg));
 159  0
         } catch (IOException ioe) {
 160  0
             throw new JspException(ioe);
 161  0
         }
 162  0
     }
 163  
 }