001
014
015 package com.liferay.portal.kernel.bean;
016
017 import com.liferay.portal.kernel.util.HtmlUtil;
018
019 import java.lang.reflect.InvocationHandler;
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022
023
038 public class AutoEscapeBeanHandler implements InvocationHandler {
039
040 public AutoEscapeBeanHandler(Object bean) {
041 _bean = bean;
042 }
043
044 public Object getBean() {
045 return _bean;
046 }
047
048 public Object invoke(Object proxy, Method method, Object[] args)
049 throws Throwable {
050
051 String methodName = method.getName();
052
053 if (methodName.startsWith("set")) {
054 throw new IllegalAccessException(
055 "Setter methods cannot be called on an escaped bean");
056 }
057
058 if (methodName.endsWith("isEscapedModel")) {
059 return true;
060 }
061 else if (methodName.endsWith("toEscapedModel")) {
062 return proxy;
063 }
064
065 Object result = null;
066
067 try {
068 result = method.invoke(_bean, args);
069 }
070 catch (InvocationTargetException ite) {
071 throw ite.getTargetException();
072 }
073
074 if (method.getAnnotation(AutoEscape.class) != null) {
075 result = HtmlUtil.escape((String)result);
076 }
077
078 return result;
079 }
080
081 private Object _bean;
082
083 }