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