| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 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 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
16 |
public class AuthenticationTag extends TagSupport { |
| 49 |
|
|
| 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 |
|
|
| 59 |
|
|
| 60 |
16 |
private String methodPrefix = "get"; |
| 61 |
16 |
private String operation = ""; |
| 62 |
|
|
| 63 |
|
|
| 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 |
|
} |